微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决如何使用CBC解密密文,其中密文与IV和密钥一起给出

我真的很想尝试解密给定的密文,但是我一直遇到错误b'要在CBC模式下正确使用AES,必须将消息填充为16字节的倍数。该课程尚未涵盖。 PKCS#7是一个不错的选择。参见https://en.wikipedia.org/wiki/Padding_(cryptography).'

有没有完全改变我的代码方法解决错误

任何帮助都会很棒!

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)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。