当前位置: 首页 > 知识库问答 >
问题:

Python中的AES-128 CBC解密

黄鸣
2023-03-14

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

attribute error:“str”对象没有属性“decode”

如果我们删除 .decode(“十六进制”)只是为了避免这样的错误:

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

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

共有1个答案

沈成天
2023-03-14

您使用的是蟒蛇 3,而不是蟒蛇 2。你不能在Python 3中的字符串上使用decode(),它们已经是文本,所以字节到字节的编解码器(如“十六进制”)不能以这种方式应用。

改用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)

因此,要将十六进制字符串解码为字节,请使用<code>binascii。unaxlify(),要将其编码回十六进制,请使用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解密在OpenSSL上加密的文件,但我不了解pycrypto的选项。 下面是我在OpenSSL中所做的 > openssl enc-aes-256-cbc-a-盐-pbkdf2-iter 100000-在"clear.txt"-在"crypt.txt"-传递:"my密码" openssl enc-d-aes-256-cbc-a-pbkdf2-iter 100000-输入“cry

  • 我使用AES方法对从txt文件调用的sentance进行加密。我使用了GCM模式并创建了一个特定的密钥。一切都在工作(代码如下)。 我尝试实现解密过程,也就是说,我只有密钥(HexMyKeyvalue)和加密消息(HexEncryptedOriginalMessage value)并且我想对其进行解密。但问题是我错过了一些东西... 我写了下面的代码,但我有错误消息。 TypeError:decr

  • 问题内容: 我有以下用于解密的Python脚本: 我正在尝试使用Node 生成使用该脚本生成原始值的值。这是我的尝试: 我只能修改Node.js脚本,但是我不确定它出了什么问题。 问题答案: 仅 在 将共享密钥用于加密 后 ,才需要将其编码为Base64 : 其他问题: 假定共享机密是密码而不是密钥,这就是为什么它将使用错误的密钥派生(与OpenSSL兼容)的原因。 Node.js的加密模块会自动

  • 问题内容: 基于Golang关于CFB解密的文档,我写了一个最小的工作示例来解密使用AES CFB加密的字符串,然后使用python3编码的base 64。 当邮件在Golang中加密(使用Golang doc示例中的加密功能)时,golang解密工作正常。但是,当我使用python crypto包在python脚本中加密消息时,我无法在golang脚本中成功解密它。我没有得到正确的字节。 两种A

  • 在用于aes加密和解密的python脚本中 但是相同的iv、消息和密钥在python和js中产生不同的加密消息, JavaScript与python解密兼容的问题是什么? 两者都使用了aes.mode_cbc,并且假设两者都使用了Pkcs7填充。硬编码iv现在是随机生成的