【发布时间】:2021-01-30 15:13:20
【问题描述】:
当试图解密我的明文时,它给了我一个值错误。
mesg = b'b235dd55aae34e97a054b05c09777e18'
decipher = AES.new(key,AES.MODE_CBC,iv)
plaintext = decipher.decrypt(mesg)
truetext = unpad(plaintext,block_size=16)
print(hexa(truetext).decode())
输出表明
ValueError: Padding is incorrect.
即使我自己使用加密明文
plaintext = b"hello world"
ciphertext = cipher.encrypt(pad(plaintext,16))
print(hexa(ciphertext).decode())
这是我的简单加密/解密的样子
#Pycryptodome
#unable to decrypt, padding problem
from Crypto.Cipher import AES
from Crypto.Random import get_random_bytes
from Crypto.Util.Padding import pad, unpad
from binascii import hexlify as hexa
key = get_random_bytes(16)
iv = get_random_bytes(16)
cipher = AES.new(key,AES.MODE_CBC,iv)
plaintext = b"hello world"
ciphertext = cipher.encrypt(pad(plaintext,16))
print(hexa(ciphertext).decode())
mesg = b'b235dd55aae34e97a054b05c09777e18'
decipher = AES.new(key,AES.MODE_CBC,iv)
plaintext = decipher.decrypt(mesg)
truetext = unpad(plaintext,block_size=16)
print(hexa(truetext).decode())
【问题讨论】:
-
您生成一个随机密钥并尝试用它解密一个加密的 (
mesg) 值。尝试解密ciphertext而不是mesg -
我想查看/尝试解密已编码的密文。我正在上课并想尝试它,作为实际执行之前的测试运行,似乎我可能需要尝试其他人正在使用的库。
-
在程序开始时,您将生成一个带有
key = get_random_bytes(16)的随机密钥。每次运行程序时,您都会获得一个新的随机密钥。为了能够解密消息,您需要使用与加密时相同的密钥进行解密。当您对要解密的消息进行硬编码并同时生成一个新密钥时,无法解密硬编码的消息 - 因为它是使用先前运行的另一个密钥加密的。您需要保存用于加密的密钥,以便您有 if 用于解密。
标签: python encryption aes padding