【问题标题】:AES won't decrypt properlyAES 无法正确解密
【发布时间】:2021-05-16 20:40:16
【问题描述】:

所以我使用 pycryptodome 使用带有 AES 的密钥加密消息。然后,作为测试,我想使用具有相同密钥的 AES 解密加密消息。我在这里做了,但是解密消息的结果与加密消息的结果不一样。也许我误解了 AES 的工作原理,但我认为如果我有相同的密钥,我应该能够解密用 AES 加密的消息,但看起来我错了。我怎样才能使它正常工作?

 finalCipher = AES.new(sKey, AES.MODE_CFB)
 message = input()
 #Encrypt the message using the cipher
 enMessage = message.encode('utf-8')
 encMessage = finalCipher.encrypt(enMessage)
 print(encMessage) 

 #Create a new cipher to decrypt the encrypted message, using the same key
 otherCipher = AES.new(sKey, AES.MODE_CFB)
 print(otherCipher.decrypt(encMessage))

【问题讨论】:

    标签: python encryption cryptography aes pycryptodome


    【解决方案1】:

    我意识到我需要的不仅仅是原始密钥来创建可以解密使用原始密码加密的消息的密码。我创建的原始密码有一个属性“iv”,我需要在新密码的构造函数中使用它,以便能够使用它来正确解密,而是这样做:

     finalCipher = AES.new(sKey, AES.MODE_CFB)
     message = input()
     #Encrypt the message using the cipher
     enMessage = message.encode('utf-8')
     encMessage = finalCipher.encrypt(enMessage)
     print(encMessage) 
    
     #Create a new cipher to decrypt the encrypted message, using the same key
     otherCipher = AES.new(sKey, AES.MODE_CFB, finalCipher.iv)
     print(otherCipher.decrypt(encMessage))
    

    【讨论】:

    • 现在知道你需要对密文进行编码和解码,以便像base64一样存储和传输。
    • 请记住,IV 不是秘密。仅当与 CFB 模式和相同的键一起使用时,in 才需要是唯一的。我们通常将其添加到密文中,并在解密之前将其切掉。
    猜你喜欢
    • 2019-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-21
    • 1970-01-01
    • 2011-10-10
    相关资源
    最近更新 更多