gpt4 book ai didi

python - Python 中的 AES-128 CBC 解密

转载 作者:太空狗 更新时间:2023-10-30 00:34:17 25 4
gpt4 key购买 nike

我试图在 python 中实现此代码(我是 python 的新手),它给了我以下错误:

AttributeError: 'str' object has no attribute 'decode'

如果我们删除 .decode ('hex') 只是为了避免这样的错误:

from itertools import product
from Crypto.Cipher import AES
import Crypto.Cipher.AES

key = ('2b7e151628aed2a6abf7158809cf4f3c').decode('hex')
IV = ('000102030405060708090a0b0c0d0e0f').decode('hex')
plaintext1 = ('6bc1bee22e409f96e93d7e117393172a').decode('hex')
plaintext2 = ('ae2d8a571e03ac9c9eb76fac45af8e51').decode('hex')
plaintext3 = ('30c81c46a35ce411e5fbc1191a0a52ef').decode('hex')
cipher = AES.new(key, AES.MODE_CBC, IV)
ciphertext = cipher.encrypt(plaintext1 + plaintext2 + plaintext3)
(ciphertext).encode('hex')
decipher = AES.new(key, AES.MODE_CBC, IV)
plaintext = decipher.decrypt(ciphertext)
(plaintext).encode('hex')

但它给了我以下错误:

ValueError: IV must be 16 bytes long

因为算法需要我必须删除的 .decode ('hex')

from itertools import product
from Crypto.Cipher import AES
import Crypto.Cipher.AES

key = ('2b7e151628aed2a6abf7158809cf4f3c')
IV = ('000102030405060708090a0b0c0d0e0f')
plaintext1 = ('6bc1bee22e409f96e93d7e117393172a')
plaintext2 = ('ae2d8a571e03ac9c9eb76fac45af8e51')
plaintext3 = ('30c81c46a35ce411e5fbc1191a0a52ef')
cipher = AES.new(key,AES.MODE_CBC,IV)
ciphertext = cipher.encrypt(plaintext1 + plaintext2 + plaintext3)
(ciphertext).encode('hex')
decipher = AES.new(key,AES.MODE_CBC,IV)
plaintext = decipher.decrypt(ciphertext)
(plaintext).encode('hex')

有人知道我该怎么做才能使这段代码正常工作吗?

最佳答案

您使用的是 Python 3,而不是 Python 2。您不能在 Python 3 中对字符串使用 decode(),它们已经是文本,因此字节到字节的编解码器,例如 'hex' 不能那样应用。

改用 binascii 模块:

from binascii import hexlify, unhexlify

key = unhexlify('2b7e151628aed2a6abf7158809cf4f3c')
IV = unhexlify('000102030405060708090a0b0c0d0e0f')
plaintext1 = unhexlify('6bc1bee22e409f96e93d7e117393172a')
plaintext2 = unhexlify('ae2d8a571e03ac9c9eb76fac45af8e51')
plaintext3 = unhexlify('30c81c46a35ce411e5fbc1191a0a52ef')

ciphertext_hex = hexlify(ciphertext)
# ...
plaintext_hex = hexlify(plaintext)

因此,要从十六进制字符串解码为字节,请使用 binascii.unhexlify() , 并编码回十六进制,使用 binascii.hexlify() .请注意,您不能就地转换数据,您必须将结果存储回变量中(或打印出值等)。

演示:

>>> from Crypto.Cipher import AES
>>> import Crypto.Cipher.AES
>>> from binascii import hexlify, unhexlify
>>> key = unhexlify('2b7e151628aed2a6abf7158809cf4f3c')
>>> IV = unhexlify('000102030405060708090a0b0c0d0e0f')
>>> plaintext1 = unhexlify('6bc1bee22e409f96e93d7e117393172a')
>>> plaintext2 = unhexlify('ae2d8a571e03ac9c9eb76fac45af8e51')
>>> plaintext3 = unhexlify('30c81c46a35ce411e5fbc1191a0a52ef')
>>> cipher = AES.new(key,AES.MODE_CBC,IV)
>>> ciphertext = cipher.encrypt(plaintext1 + plaintext2 + plaintext3)
>>> hexlify(ciphertext)
b'7649abac8119b246cee98e9b12e9197d5086cb9b507219ee95db113a917678b273bed6b8e3c1743b7116e69e22229516'
>>> decipher = AES.new(key,AES.MODE_CBC,IV)
>>> plaintext = decipher.decrypt(ciphertext)
>>> plaintext == plaintext1 + plaintext2 + plaintext3 # test if decryption was successful
True
>>> hexlify(plaintext)
b'6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52ef'

关于python - Python 中的 AES-128 CBC 解密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46904355/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com