pip install pycryptodome
from Crypto.Cipher import AES
#from pyrsistent import b
# 秘钥和文本都是byte类型
# 拼接文本,文本长度需为16的倍数,不足则拼接空格
def splice(text):
while len(text) % 16 != 0:
text += b' '
return text
# 拼接秘钥,秘钥长度需为16的倍数,不足则拼接空格
def splice_key(key):
while len(key) % 16 != 0:
key += b' '
return key
if __name__ == '__main__':
key = b'12345' # 秘钥
aes = AES.new(splice_key(key), AES.MODE_ECB) # 根据秘钥初始化加密器
text = b'jiamiwenben' # 加密文本
encrypted_byte = aes.encrypt(splice(text)) # 使用加密器的加密方法对文本进行加密,返回加密结果(byte类型)
import base64
b64_string = str(base64.b64encode(encrypted_byte),'utf-8') # 将bytes装换成string,base64.b64decode(b64_string) 这句话将string装换成bytes
print("encrypted_byte: ", encrypted_byte)
print(b64_string)
# print(b'')
# # print (encrypted_byte.decode(encoding='utf-8'))
# print('x95!xc9x91xc41