【发布时间】:2018-04-04 21:16:58
【问题描述】:
我正在尝试在 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')
有谁知道我可以怎样做才能使这段代码正常工作?
【问题讨论】:
-
这会起作用(对于 python2),因为您的数据是 3 个 16 字节的块,否则您必须填充。
标签: python encryption cryptography aes