Python 艾斯元';不能正确解密
因此,我使用pycryptodome使用AES密钥对消息进行加密。然后,作为测试,我想使用具有相同密钥的AES对加密消息进行解密。我在这里已经这样做了,但是解密消息的结果与加密消息的结果不同。也许我误解了AES的工作原理,但我会假设如果我有相同的密钥,我应该能够解密用AES加密的消息,但看起来我错了。我如何使其正常工作Python 艾斯元';不能正确解密,python,encryption,cryptography,aes,pycryptodome,Python,Encryption,Cryptography,Aes,Pycryptodome,因此,我使用pycryptodome使用AES密钥对消息进行加密。然后,作为测试,我想使用具有相同密钥的AES对加密消息进行解密。我在这里已经这样做了,但是解密消息的结果与加密消息的结果不同。也许我误解了AES的工作原理,但我会假设如果我有相同的密钥,我应该能够解密用AES加密的消息,但看起来我错了。我如何使其正常工作 finalCipher = AES.new(sKey, AES.MODE_CFB) message = input() #Encrypt the message using
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))
我意识到我需要的不仅仅是原始密钥来创建一个可以解密使用原始密码加密的消息的密码。我创建的原始密码有一个属性“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))
我意识到我需要的不仅仅是原始密钥来创建一个可以解密使用原始密码加密的消息的密码。我创建的原始密码有一个属性“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才需要唯一。我们通常将其预编到密文中,并在解密之前将其切分。现在了解到,您需要对密文进行编码和解码,以便像base64一样进行存储和传输。请记住,IV不是秘密。仅当与CFB模式和相同键一起使用时,In才需要唯一。我们通常在密文前加上前缀,并在解密前将其切掉。