- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章Python3对称加密算法AES、DES3实例详解由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
本文实例讲述了python3对称加密算法aes、des3。分享给大家供大家参考,具体如下:
python3.6此库安装方式,需要pip3 install pycryptodome.
如有site-packages中存在crypto、pycrypto,在pip之前,需要pip3 uninstall crypto、pip3 uninstall pycrypto,否则无法安装成功.
c:\windows\system32>pip3 install pycryptodome collecting pycryptodome downloading https://files.pythonhosted.org/packages/0f/5d/a429a53eacae3e13143248c3868c76985bcd0d75858bd4c25b574e51bd4d/pycryptodome-3.6.3-cp36-cp36m-win_amd64.whl (7.9mb) 100% |████████████████████████████████| 7.9mb 111kb/s installing collected packages: pycryptodome successfully installed pycryptodome-3.6.3 。
这里顺带说一下pycrypto,这个库已经有很久没有人维护了,如果需要安装此库,需要先安装 vc++ build tools 。
然后将 ~\buildtools\vc\tools\msvc\14.15.26726\include 目录下的 stdint.h 拷贝到 c:\program files (x86)\windows kits\10\include\10.0.17134.0\ucrt 下。(win10 需管理员权限) 。
接着将同目录下的 inttypes.h 中的 #include <stdint.h> (第十四行),改成 #include "stdint.h".
然后使用 pip3 install pycrypto,就能直接安装了.
注:如果不是业务需要,请尽可能使用 pycryptodome.
aes:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
import
crypto.cipher.aes
import
crypto.random
import
base64
import
binascii
def
auto_fill(x):
if
len
(x) <
=
32
:
while
len
(x)
not
in
[
16
,
24
,
32
]:
x
+
=
" "
return
x.encode()
else
:
raise
"密钥长度不能大于32位!"
key
=
"asd"
content
=
"abcdefg1234567"
x
=
crypto.cipher.aes.new(auto_fill(key), crypto.cipher.aes.mode_ecb)
a
=
base64.encodebytes(x.encrypt(auto_fill(content)))
b
=
x.decrypt(base64.decodebytes(a))
print
(a)
print
(b)
a
=
binascii.b2a_base64(x.encrypt(auto_fill(content)))
b
=
x.decrypt(binascii.a2b_base64(a))
print
(a)
print
(b)
key
=
"dsa"
iv
=
crypto.random.new().read(
16
)
# 向量,必须为16字节
content
=
"1234567abcdefg"
y
=
crypto.cipher.aes.new(auto_fill(key), crypto.cipher.aes.mode_cbc, iv)
c
=
binascii.b2a_base64(y.encrypt(auto_fill(content)))
z
=
crypto.cipher.aes.new(auto_fill(key), crypto.cipher.aes.mode_cbc, iv)
d
=
z.decrypt(binascii.a2b_base64(c))
print
(c)
print
(d)
|
运行结果:
b'jr/eiup32klhc3ypzz1cyg==\n' b'abcdefg1234567 ' b'jr/eiup32klhc3ypzz1cyg==\n' b'abcdefg1234567 ' b'j+ul9kqd0hnuihw3z9td7a==\n' b'1234567abcdefg ' 。
des3:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
import
crypto.cipher.des3
import
base64
import
binascii
def
auto_fill(x):
if
len
(x) >
24
:
raise
"密钥长度不能大于等于24位!"
else
:
while
len
(x) <
16
:
x
+
=
" "
return
x.encode()
key
=
"asd"
content
=
"abcdefg1234567"
x
=
crypto.cipher.des3.new(auto_fill(key), crypto.cipher.des3.mode_ecb)
a
=
base64.encodebytes(x.encrypt(auto_fill(content)))
print
(a)
b
=
x.decrypt(base64.decodebytes(a))
print
(b)
a
=
binascii.b2a_base64(x.encrypt(auto_fill(content)))
b
=
x.decrypt(binascii.a2b_base64(a))
print
(a)
print
(b)
|
运行结果:
b'/ee3nfkenqek/qmnd1mjog==\n' b'abcdefg1234567 ' b'/ee3nfkenqek/qmnd1mjog==\n' b'abcdefg1234567 ' 。
附:aes的工厂模式封装cipher_aes.py(封装crypto.cipher.aes) 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
'''
created on 2018年7月7日
@author: ray
'''
import
crypto.cipher.aes
import
crypto.random
import
base64
import
binascii
class
cipher_aes:
pad_default
=
lambda
x, y: x
+
(y
-
len
(x)
%
y)
*
" "
.encode(
"utf-8"
)
unpad_default
=
lambda
x: x.rstrip()
pad_user_defined
=
lambda
x, y, z: x
+
(y
-
len
(x)
%
y)
*
z.encode(
"utf-8"
)
unpad_user_defined
=
lambda
x, z: x.rstrip(z)
pad_pkcs5
=
lambda
x, y: x
+
(y
-
len
(x)
%
y)
*
chr
(y
-
len
(x)
%
y).encode(
"utf-8"
)
unpad_pkcs5
=
lambda
x: x[:
-
ord
(x[
-
1
])]
def
__init__(
self
, key
=
"abcdefgh12345678"
, iv
=
crypto.random.new().read(crypto.cipher.aes.block_size)):
self
.__key
=
key
self
.__iv
=
iv
def
set_key(
self
, key):
self
.__key
=
key
def
get_key(
self
):
return
self
.__key
def
set_iv(
self
, iv):
self
.__iv
=
iv
def
get_iv(
self
):
return
self
.__iv
def
cipher_mode_ecb(
self
):
self
.__x
=
crypto.cipher.aes.new(
self
.__key.encode(
"utf-8"
), crypto.cipher.aes.mode_ecb)
def
cipher_mode_cbc(
self
):
self
.__x
=
crypto.cipher.aes.new(
self
.__key.encode(
"utf-8"
), crypto.cipher.aes.mode_cbc,
self
.__iv.encode(
"utf-8"
))
def
encrypt(
self
, text, cipher_method, pad_method
=
"
", code_method="
"):
if
cipher_method.upper()
=
=
"mode_ecb"
:
self
.cipher_mode_ecb()
elif
cipher_method.upper()
=
=
"mode_cbc"
:
self
.cipher_mode_cbc()
cipher_text
=
b"
".join([self.__x.encrypt(i) for i in self.text_verify(text.encode("
utf
-
8
"), pad_method)])
if
code_method.lower()
=
=
"base64"
:
return
base64.encodebytes(cipher_text).decode(
"utf-8"
).rstrip()
elif
code_method.lower()
=
=
"hex"
:
return
binascii.b2a_hex(cipher_text).decode(
"utf-8"
).rstrip()
else
:
return
cipher_text.decode(
"utf-8"
).rstrip()
def
decrypt(
self
, cipher_text, cipher_method, pad_method
=
"
", code_method="
"):
if
cipher_method.upper()
=
=
"mode_ecb"
:
self
.cipher_mode_ecb()
elif
cipher_method.upper()
=
=
"mode_cbc"
:
self
.cipher_mode_cbc()
if
code_method.lower()
=
=
"base64"
:
cipher_text
=
base64.decodebytes(cipher_text.encode(
"utf-8"
))
elif
code_method.lower()
=
=
"hex"
:
cipher_text
=
binascii.a2b_hex(cipher_text.encode(
"utf-8"
))
else
:
cipher_text
=
cipher_text.encode(
"utf-8"
)
return
self
.unpad_method(
self
.__x.decrypt(cipher_text).decode(
"utf-8"
), pad_method)
def
text_verify(
self
, text, method):
while
len
(text) >
len
(
self
.__key):
text_slice
=
text[:
len
(
self
.__key)]
text
=
text[
len
(
self
.__key):]
yield
text_slice
else
:
if
len
(text)
=
=
len
(
self
.__key):
yield
text
else
:
yield
self
.pad_method(text, method)
def
pad_method(
self
, text, method):
if
method
=
=
"":
return
cipher_aes.pad_default(text,
len
(
self
.__key))
elif
method
=
=
"pkcs5padding"
:
return
cipher_aes.pad_pkcs5(text,
len
(
self
.__key))
else
:
return
cipher_aes.pad_user_defined(text,
len
(
self
.__key), method)
def
unpad_method(
self
, text, method):
if
method
=
=
"":
return
cipher_aes.unpad_default(text)
elif
method
=
=
"pkcs5padding"
:
return
cipher_aes.unpad_pkcs5(text)
else
:
return
cipher_aes.unpad_user_defined(text, method)
|
使用方法:
加密:cipher_aes(key [, iv]).encrypt(text, cipher_method [, pad_method [, code_method]]) 。
解密:cipher_aes(key [, iv]).decrypt(cipher_text, cipher_method [, pad_method [, code_method]]) 。
key:密钥(长度必须为16、24、32) 。
iv:向量(长度与密钥一致,ecb模式不需要) 。
text:明文(需要加密的内容) 。
cipher_text:密文(需要解密的内容) 。
cipher_method:加密方法,目前只有"mode_ecb"、"mode_cbc"两种 。
pad_method:填充方式,解决 java 问题选用"pkcs5padding" 。
code_method:编码方式,目前只有"base64"、"hex"两种 。
来段调用封装类 cipher_aes 的 demo_cipher_aes.py,方便大家理解:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
import
cipher_aes
key
=
"qwedsazxc123321a"
iv
=
key[::
-
1
]
text
=
"我爱小姐姐,可小姐姐不爱我 - -"
cipher_method
=
"mode_cbc"
pad_method
=
"pkcs5padding"
code_method
=
"base64"
cipher_text
=
cipher_aes(key, iv).encrypt(text, cipher_method, pad_method, code_method)
print
(cipher_text)
text
=
cipher_aes(key, iv).decrypt(cipher_text, cipher_method, pad_method, code_method)
print
(text)
'''
运行结果:
uxhf+mosko4xa+jgoyzjvyh9n5nvrcwehbwm/a977cmgqzg+fye0gel5/m5v9o1o
我爱小姐姐,可小姐姐不爱我 - -
'''
|
希望本文所述对大家python程序设计有所帮助.
原文链接:https://blog.csdn.net/qq_42486920/article/details/80850974 。
最后此篇关于Python3对称加密算法AES、DES3实例详解的文章就讲到这里了,如果你想了解更多关于Python3对称加密算法AES、DES3实例详解的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
我正在尝试用 C 编写一个简单的加密程序。我的目标是将 abc(它可以是任何单词)翻译成 123。然后乘以 2 得到 246 然后再次翻译成文本,然后在屏幕上写 bdf。这是我的算法,它不能正常工作。
互联网上有很多关于加密的答案,但我一直无法找到我正在寻找的东西:使用 c# 提供的工具对字符串和文本文件进行加密的简单强加密。 我的主要问题是我不知道如何将 IV 保存到文本文件的开头或如何创建随机
C#“RSACryptoServiceProvider”加密算法是 Microsoft-CryptoAPI 的一部分吗? 通过在 .Net 中使用 CryptoAPI 而不是“RSACryptoSer
我正在编写适用于“流”的 XXTEA 加密算法的实现,即可以像这样使用:crypt mykey 输出。 其中一个先决条件是它根本无法访问文件(它只读取固定大小的 block ,直到找到 EOF)。该
嘿,这是我老师给我的一个加密例子。但是,我仍然搞不懂(Int)Message[i]是什么意思?那么程序在这条线上做什么呢?
嘿,这是我老师给我的一个加密例子。但是,我仍然搞不懂(Int)Message[i]是什么意思?那么程序在这条线上做什么呢?
嘿,我研究这个已经有一段时间了,我记得我的兄弟引导我完成了这个完全相同的算法。 基本上,它只是添加键中字符和短语的 ascii 值。 我可以用这个加密它: def encrypt(key, strin
我正在尝试将 BouncyCaSTLe 与 android 一起使用来实现 ECDH 和 EL Gamal。我已经添加了 bouncycaSTLe jar 文件 (bcprov-jdk16-144.j
我需要加密一个数字,我和这个加密的值将提供给客户询问 key ,所以我想尽量减少数字的数量并使它们都可以打印。 所以我希望结果要么全是数字,要么全是十六进制字符。 我正在使用的当前加密方法(对于非数字
关闭。这个问题是opinion-based .它目前不接受答案。 想改善这个问题吗?更新问题,以便可以通过 editing this post 用事实和引文回答问题. 5年前关闭。 Improve t
我正在制作 note taking web app它会在您键入时连续保存笔记。我正在发送差异以防止通过线路发送过多数据,这在注释为纯文本时工作正常。 然而,我添加了对加密笔记的支持,所以笔记只会以加密
我正在寻找一种与 JavaScript 和 Java 兼容的安全对称 key 加密算法。 我已经尝试实现一个,但我遇到了一些编码问题。 最佳答案 您不想使用 JavaScript 加密,especia
目录 1 MD5 1.1 算法定义 1.2 MD5算法不安全的原因 1.2.1 字典表很大 1
在查看一些旧的 Coldfusion 代码时,我发现了几个通过加密/解密函数使用 CFMX_COMPAT 算法加密的数据实例。 找了一阵子,一直没找到这是什么算法。文档提到它现在是最不安全的方法,但我
谁能告诉我 iOS 上 SUP 用来加密底层数据库的加密算法是什么? 它是否依赖于 SQLite EE(加密扩展),如果是,使用了 4 种可用算法中的哪一种? 我的猜测是某种 AES,但官方文档中没有
我一直有这个问题。 The specified cryptographic algorithm is not supported on this platform. Exception Details
我正在为一个项目实现 3Des 加密算法。 我使用此代码作为示例: http://www.java2s.com/Code/Java/Security/TripleDES.htm 它工作正常,但我需要发
我正在尝试使用 adobe type 1 字体加密算法来加密文本文件。但是,我不知道如何用汇编语言正确地实现算法。如果可以,请帮助我。 下面是adobe type 1字体加密算法: unsigned
如何添加/创建具有特定加密算法的 c/c++ OpenSSL 静态库?假设我不想实现 md5。如何创建没有 md5 的库? 最佳答案 在配置脚本中你可以指定no-选项,它会在没有你指定的密码的情况下构
我检查了 TripleDES。它的 block 大小是 64 位。 有没有针对8位 block 大小的算法? 谢谢 编辑:我不打算将其用于完美保护,而是用于以防万一的情况,即看到代码的人不应该找到明文
我是一名优秀的程序员,十分优秀!