所以我一直试图找出一种使用AES.MODE_CBC加密和解密简单文本的简单方法,我似乎无法找到一个关于它的好教程。我希望有人可以帮助我。我能够加密消息但是当谈到解密时,我似乎无法找到一种简单的方法。
from Crypto.Cipher import AES
from Crypto import Random
import base64
def padding(msg):
return msg + (((16-len(msg) % 16)) * '\x00')
def CBC():
block_size=16
# secret key
key = b'Sixteen byte key'
# input message
msg='Attack at dawn'
# Encrypt
iv = Random.new().read(AES.block_size)
encrypt_mode = AES.new(key, AES.MODE_CBC, iv)
cipher_text = base64.b64encode(iv + encrypt_mode.encrypt((padding(msg))))
print cipher_text
# Decrypt
cipher_text = base64.b64decode(cipher_text)
encrypt_mode = AES.new(key, AES.MODE_CBC, iv)
plain_text = iv + encrypt_mode.decrypt(cipher_text)
print plain_text
CBC()
答案 0 :(得分:2)
你需要删除&在解密期间使用而不是前缀IV:
iv, ciphertext = ciphertext[:AES.block_size], ciphertext[AES.block_size:]