安装所需包
pip install pycryptodome
编写代码
import base64
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad
def encrypt(key, text):
aes = AES.new(key.encode("utf-8"), AES.MODE_ECB)
pad_pkcs7 = pad(text.encode('utf-8'), AES.block_size, style='pkcs7')
# 加密函数,使用pkcs7补全
res = aes.encrypt(pad_pkcs7)
# 转换为base64
msg = str(base64.b64encode(res), encoding="utf-8")
return msg
key是加密的密码
text是待加密字符串