Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如果密文与IV和密钥一起给出,如何使用CBC解密密文_Python_Encryption_Aes_Cbc Mode - Fatal编程技术网

Python 如果密文与IV和密钥一起给出,如何使用CBC解密密文

Python 如果密文与IV和密钥一起给出,如何使用CBC解密密文,python,encryption,aes,cbc-mode,Python,Encryption,Aes,Cbc Mode,我真的很接近能够解密给定的密文,但我一直遇到一个错误b'要在CBC模式下正确使用AES,消息必须填充为16字节的倍数。这门课还没有讲到。PKCS#7是一个不错的选择。看 有没有办法在不完全更改代码的情况下解决此错误 任何帮助都太好了 from Crypto.Cipher import AES from binascii import hexlify, unhexlify import pyaes import Crypto.Cipher.AES from itertools import pro

我真的很接近能够解密给定的密文,但我一直遇到一个错误b'要在CBC模式下正确使用AES,消息必须填充为16字节的倍数。这门课还没有讲到。PKCS#7是一个不错的选择。看

有没有办法在不完全更改代码的情况下解决此错误

任何帮助都太好了

from Crypto.Cipher import AES
from binascii import hexlify, unhexlify
import pyaes
import Crypto.Cipher.AES
from itertools import product
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
ciphertext1 = 0x2f81131d1bf1284dc5b54e0c5c9aff54
ciphertext2 = 0x5bd7ff8eb2364e9d7d806224117ac4fd
ciphertext3 = 0x18e89f1cad206f3778686f7e73717b8a
ciphertext4 = 0xdbcedbc1704da1291abf6cd44dbb86da
ciphertext5 = 0x433305df3876390b3fc68b1a3cd6b364
ciphertext6 = 0x7f853ebc67a62785abfa3b1b1c0c50aa
ciphertext7 = 0xc255967b03ba48eafddeb6b2b3ed973f
ciphertext8 = 0x6dc37cb08d173831ed852802b43d43c0
ciphertext9 = 0x5a7d39d8620f9d80234b58c08fd64405
ciphertext10 = 0x86cb0cd85fbd6b1f84087c92b05d0eb0
ciphertext11 = 0x63ceed0d6a5f307ec1387bd8594541d8
ciphertext12 = 0x499cc5d2f7703ccbc520034df53a5f10
ciphertext13 = 0x52e794464aef722bf0e71177f3e12183
ciphertext14 = 0x618413f7cc6911ecf707d3a985db1b90

c1 = ciphertext1.to_bytes(16, 'big')
c2 = ciphertext2.to_bytes(16, 'big')
c3 = ciphertext3.to_bytes(16, 'big')
c4 = ciphertext4.to_bytes(16, 'big')
c5 = ciphertext5.to_bytes(16, 'big')
c6 = ciphertext6.to_bytes(16, 'big')
c7 = ciphertext7.to_bytes(16, 'big')
c8 = ciphertext8.to_bytes(16, 'big')
c9 = ciphertext9.to_bytes(16, 'big')
c10 = ciphertext10.to_bytes(16, 'big')
c11 = ciphertext11.to_bytes(16, 'big')
c12 = ciphertext12.to_bytes(16, 'big')
c13 = ciphertext13.to_bytes(16, 'big')
c14 = ciphertext14.to_bytes(16, 'big')

#0x2f81131d1bf1284dc5b54e0c5c9aff54 
#0x5bd7ff8eb2364e9d7d806224117ac4fd
#0x18e89f1cad206f3778686f7e73717b8a
#0xdbcedbc1704da1291abf6cd44dbb86da
#0x433305df3876390b3fc68b1a3cd6b364
#0x7f853ebc67a62785abfa3b1b1c0c50aa
#0xc255967b03ba48eafddeb6b2b3ed973f
#0x6dc37cb08d173831ed852802b43d43c0
#0x5a7d39d8620f9d80234b58c08fd64405
#0x86cb0cd85fbd6b1f84087c92b05d0eb0
#0x63ceed0d6a5f307ec1387bd8594541d8
#0x499cc5d2f7703ccbc520034df53a5f10
#0x52e794464aef722bf0e71177f3e12183
#0x618413f7cc6911ecf707d3a985db1b90

ciphertext = c1 + c2 + c3 + c4 + c5 + c6 + c7 + c8 + c9 + c10 + c11 + c12 + c13 + c14
n = 16
padding_length = n - (len(ciphertext)% n)

#c = hex(ciphertext)
IV = 0x5468617473206d79204b756e67204675
biv = IV.to_bytes(16, 'big')
key = 0xec58dfa74641af52ad0d16e77d576623
bkey = key.to_bytes(16, 'big')


aes = AES.new(bkey, AES.MODE_CBC, biv)

finaltext = aes.decrypt(ciphertext)

print (finaltext)

这不是错误,此警告消息是解密数据的真实内容。这封邮件是用密文加密的。所以,若问题存在,那个么在加密端,而不是解密端。在加密之前,对明文应用填充。不在加密文本上。如果出现填充错误,您是如何获得加密值的?您的代码对我来说运行良好。错误消息只是纯文本,而不是错误