Python中的AES-128 CBC解密

5
我正在尝试在Python中实现此代码(我刚接触Python),但它给出了以下错误:
AttributeError: 'str'对象没有'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 必须有16个字节

因为该算法需要 .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')

有人知道我该怎么做才能让这段代码运行吗?


{btsdaf} - t.m.adam
1个回答

16

您正在使用的是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'

{btsdaf} - Martijn Pieters
只在负评上请求评论只会让你得到更多的负评...再来一个...等等...不对...算了... - rene
{btsdaf} - Martijn Pieters
1
{btsdaf} - rene
2
{btsdaf} - Martijn Pieters
{btsdaf} - Jon Ander Díez

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接