此加密和解密的秘钥的长度必须是16位、24位或者32位

import base64
import os
from Crypto.Cipher import AES

# AES key must be either 16, 24, or 32 bytes long
aes_key = "12345678901234567890123456789012"

# 加密
def aes_encrypt(data):
key=aes_key #加密时使用的key,只能是长度16,24和32的字符串
BS = AES.block_size
pad = lambda s: s + (BS - len(s) % BS) * chr(BS - len(s) % BS)
cipher = AES.new(key)
encrypted = cipher.encrypt(pad(data)) #aes加密
result = base64.b64encode(encrypted) #base64 encode
return result

# 解密
def aes_decrypt(data):
key=aes_key
unpad = lambda s : s[0:-s[-1]]
cipher = AES.new(key)
result2 = base64.b64decode(data)
decrypted = unpad(cipher.decrypt(result2))
return decrypted

encrypted_text = aes_encrypt("{'token':'123456', 'name':'Victor','age':1, 'gender':1, 'id':123, 'company':'injiajia.com'}")
print("加密输出:")
print(encrypted_text)

decrypted_text = aes_decrypt(encrypted_text)
print("解密输出:")
print(decrypted_text)

输出

"""
加密输出:
b'S4t7N9JtmCExLVW+k1oe0qMIvlSUjlXusCNAcrqTa6erPXZlZ6cDeFpoPrtWeyy0E2eG+d9cAm/XpVF+Cx8dDt8kbY8IXDXZ5ksdkK/nVvatNBrvzD4HhcJHPtwDrreR'

解密输出:
b"{'token':'123456', 'name':'Victor','age':1, 'gender':1, 'id':123, 'company':'injiajia.com'}"
"""