Python解密aes-128-cbc

要解密使用AES-128-CBC加密的数据,你可以使用Python中的cryptography库。以下是一个简单的示例:

from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.backends import default_backend
from base64 import b64decode

def decrypt_aes_128_cbc(key, iv, ciphertext):
    backend = default_backend()
    cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=backend)
    decryptor = cipher.decryptor()
    plaintext = decryptor.update(ciphertext) + decryptor.finalize()
    return plaintext

# 输入加密的密钥、初始化向量(IV)和密文
key = b'your_aes_key_here'
iv = b'your_iv_here'
ciphertext = b64decode('your_base64_encoded_ciphertext_here')

# 解密
plaintext = decrypt_aes_128_cbc(key, iv, ciphertext)
print("解密后的明文:", plaintext.decode('utf-8'))

在上述代码中,你需要替换your_aes_key_here、your_iv_here和your_base64_encoded_ciphertext_here为你的实际AES密钥、初始化向量和Base64编码的密文。解密后的明文将被打印出来。

请确保安装了cryptography库,你可以使用以下命令来安装:

pip install cryptography

 

posted @ 2024-02-18 01:33  不会飞的鲨鱼  阅读(547)  评论(0编辑  收藏  举报