python3 执行AES加密方法

时间:2024-08-26 21:07:02

cmd执行命令:pip install pycryptodome

 # -*- coding: utf-8 -*-
# __author__ = 'Carry' import base64
from Crypto.Cipher import AES # str不是16的倍数那就补足为16的倍数
def add_to_16(value):
while len(value) % 16 != 0:
value += '\0'
return str.encode(value) # 返回bytes key = '' # 密码
text = 'adadadffdbfhgjhgj' # 待加密文本
aes = AES.new(add_to_16(key), AES.MODE_ECB) # 初始化加密器
encrypted_text = str(base64.encodebytes(aes.encrypt(add_to_16(text))), encoding='utf-8').replace('\n', '') # 执行加密并转码返回bytes
print(encrypted_text)

执行完:

C:\Python36\python3.exe E:/study/py/py3_stu/2018-01-31/c.py
goUqddXOjbPejrOP+GtLvCWFke5JclH9fd6JrgCQE7w=

Process finished with exit code 0

到这个链接http://tool.chacuo.net/cryptaes去验证

python3 执行AES加密方法