0
votes

So I am using pycryptodome to encrypt a message using a secret key with AES. I want to then, as a test, decrypt the encrypted message using AES with the same secret key. I have done that here, but the result of the decrypted message is not the same as the encrypted message. Maybe I am misunderstanding how AES works, but I would assume that I should be able to decrypt a message encrypted with AES if I have the same secret key, but it would appear that I'm wrong. How do I make this work properly?

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

1 Answers

2
votes

I realized that I need more than just the original secret key to create a cipher that can decrypt messages encrypted using the original cipher. The original cipher I created has an attribute "iv" that I need to use in the constructor of the new cipher in order to be able to use it to decrypt properly, by doing this instead:

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