Python中的AES-128 CBC解密 [英] AES-128 CBC decryption in Python

查看:1837
本文介绍了Python中的AES-128 CBC解密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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


AttributeError:'str'对象没有属性'decode'


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

 从itertools进口产品Crypto。密码导入AES 
导入Crypto.Cipher.AES

密钥=('2b7e151628aed2a6abf7158809cf4f3c')。decode('hex')
IV =('000102030405060708708090a0b0c0d0e0f')。decode(' hex')
plaintext1 =('6bc1bee22e409f96e93d7e117393172a')。decode('hex')
plaintext2 =('ae2d8a571e03ac9c9eb76fac45af8e51')。decode('hex')
plaintext346 =('30 .decode('hex')
密文= AES.new(密钥,AES.MODE_CBC,IV)
密文= cipher.encrypt(纯文本1 +纯文本2 +纯文本3)
(密文).encode ('hex')
解密= AES.new(密钥,AES.MODE_CBC,IV)
纯文本=解密。解密(密文)
(纯文本).encode('hex')

但是它给了我以下错误:


ValueError:IV必须为16个字节长


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



<$ p来自itertools的$ p> 从Crypto导入产品
.Cipher导入AES
导入Crypto.Cipher.AES

key =('2b7e151628aed2a6abf7158809cf4f3c')
IV =('000102030405060708090a0b0c0d0e0f')
plaintext1 =('6bc1bee22e409f96e93d7e117393172a')
plaintext2 =('ae2d8a571e03ac9c9eb76fac45af8e51')
bc3c9e0 = a
bc3c11e0e)
a3c81e5 = $ a $ b30 new(key,AES.MODE_CBC,IV)
密文= cipher.encrypt(纯文本1 +纯文本2 +纯文本3)
(密文).encode('hex')
解密= AES.new( key,AES.MODE_CBC,IV)
plaintext = decipher.decrypt(密文)
(明文).encode('hex')

任意时间您知道如何使此代码正常工作吗?

解决方案

您正在使用Python 3,而不是Python 2。不能在Python 3中的字符串上使用 decode(),因为它们已经是文本了,所以逐字节编解码器,例如'hex'不能用这种方式。



请使用 binascii 模块:

 从binascii导入hexlify,unhexlify 

key = unhexlify('2b7e151628aed2a6abf7158809cf4f3c')
IV = unhexlify('000102030405060708090090a0b0c0d0e0f')
plaintext1 = unhexlify('6bc1bee22e409f96e93d7e117393172a')
plaintext2 = unhexlify('ae2d8a571e03ac9c9eb76fb1b1c1b1b3c1b1b3c3b1c3b1b3c3b1c3b1c3b1c3b1c3b1c3b1c3b1c3b1c3b1c3b1b3c3b1b3c8b1b3c8bc8b6bbbbbbbb人$ c>

  ciphertext_hex = hexlify(密文)
#...
plaintext_hex = hexlify(纯文本)

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



演示:

 >>从Crypto.Cipher导入AES 
>> import Crypto.Cipher.AES
>>从binascii import hexlify,取消hexlify
>> key = unhexlify(’2b7e151628aed2a6abf7158809cf4f3c’)
>> IV = unhexlify(’000102030405060708090a0b0c0d0e0f’)$​​ b $ b>> plaintext1 = unhexlify(’6bc1bee22e409f96e93d7e117393172a’)
>> plaintext2 = unhexlify(’ae2d8a571e03ac9c9eb76fac45af8e51’)
>> plaintext3 = unhexlify(’30c81c46a35ce411e5fbc1191a0a52ef’)
>> cipher = AES.new(key,AES.MODE_CBC,IV)
>> ciphertext = cipher.encrypt(plaintext1 + plaintext2 + plaintext3)
>> hexlify(密文)
b’7649abac8119b246cee98e9b12e9197d5086cb9b507219ee95db113a917678b273bed6b8e3c1743b7116e69e22229516’
>>> decipher = AES.new(key,AES.MODE_CBC,IV)
>> plaintext = decipher.decrypt(密文)
>> plaintext == plaintext1 + plaintext2 + plaintext3#测试解密是否成功
True
>> hexlify(plaintext)
b’6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52ef’

$ b

I'm trying to implement this code in python (I'm new to python) and it gives me the following error:

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

If we remove .decode ('hex') only to avoid such error:

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')

but it gives me the following error:

ValueError: IV must be 16 bytes long

since the algorithm would need the .decode ('hex') that I had to remove

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')

Does anyone have any idea how I could do to make this code work?

解决方案

You are using Python 3, not Python 2. You can't use decode() on strings in Python 3, they are already text, so bytes-to-bytes codecs such as 'hex' can't be applied that way.

Use the binascii module instead:

from binascii import hexlify, unhexlify

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

and

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

So to decode from a hex string to bytes, use binascii.unhexlify(), and to encode back to hex, use binascii.hexlify(). Note that you you can't convert data in-place, you do have to store the result back in a variable (or print out the value, etc.).

Demo:

>>> 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中的AES-128 CBC解密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆