from Crypto.Cipher import AES
from Crypto.Util.Padding import unpad
from base64 import b64decode
# AES解密函数
def decrypt_aes(ciphertext, key, iv):
# 将base64编码的密文解码为字节串
ciphertext = b64decode(ciphertext)
# 创建AES解密器对象
cipher = AES.new(key, AES.MODE_CBC, iv)
# 解密并去除填充
plaintext = unpad(cipher.decrypt(ciphertext), AES.block_size)
return plaintext.decode('utf-8')
# 示例使用
ciphertext = "your_base64_encoded_ciphertext_here"
key = b'your_32_byte_key_here' # 密钥必须是16, 24或32字节长(分别对应AES-128, AES-192或AES-256)
iv = b'your_16_byte_iv_here' # 初始化向量必须是16字节长
try:
decrypted_text = decrypt_aes(ciphertext, key, iv)
print("解密后的文本:", decrypted_text)
except Exception as e:
print("解密失败:", str(e))
Crypto.Cipher 模块中导入 AES 类,用于进行AES加密和解密操作。Crypto.Util.Padding 用于处理填充,base64 用于解码Base64编码的密文。decrypt_aes 函数接收三个参数:密文、密钥和初始化向量(IV)。它首先将Base64编码的密文解码为字节串,然后创建一个AES解密器对象,并使用该对象解密数据。最后,它去除填充并返回解密后的明文。decrypt_aes 函数来解密密文。请注意,你需要替换 your_base64_encoded_ciphertext_here、your_32_byte_key_here 和 your_16_byte_iv_here 为实际的值。如果你有任何问题或需要进一步的帮助,请告诉我!
上一篇:python 循环语句
下一篇:python多行注释快捷键
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站