Search code examples
pythonencryptionopensslaes

Why is AES encrypted cipher of the same string with the same key always different?


I have a file called plain.txt. Inside the file I have:

Hello Hello Hello Hello

I am using this command to encrypt it:

openssl enc -aes-128-cbc -salt -k "Hello" -in plain.txt -out encrypted.bin

Then I print the encrypted value like this:

buff = open("encrypted.bin")
cipher = buff.read()
buff.close()
print b64encode(cipher)

But it is always different value. Shouldn't the cipher be always the same? I am using the same file and the same password to encrypt it. These are my terminal outputs:

Richard-Knops-MacBook-Pro:python_test richardknop$ openssl enc -aes-128-cbc -salt -k "Hello" -in plain.txt -out encrypted.bin
Richard-Knops-MacBook-Pro:python_test richardknop$ python test.py U2FsdGVkX1+AmoQiIkYAxIYanLr/kbjMfEJPPLfeE/wtyxScvAKzb7K38ZxoI097
Richard-Knops-MacBook-Pro:python_test richardknop$ openssl enc -aes-128-cbc -salt -k "Hello" -in plain.txt -out encrypted.bin
Richard-Knops-MacBook-Pro:python_test richardknop$ python test.py U2FsdGVkX19vPD+OoiK7iSgYJiPMxuKGNWWrLlfBS0c3yCJkuv7QIBGEo2Q86UsV
Richard-Knops-MacBook-Pro:python_test richardknop$ openssl enc -aes-128-cbc -salt -k "Hello" -in plain.txt -out encrypted.bin
Richard-Knops-MacBook-Pro:python_test richardknop$ python test.py U2FsdGVkX1+3I8EC7u3lrcVPyD/JV12NAecWvTPXGga0Nh2cwqLAtGCDhLK6MI9g
Richard-Knops-MacBook-Pro:python_test richardknop$ 

Solution

  • Because the "salt" varies each time. This prevents, for example, rainbow table type attacks on the encrypted values. See http://en.wikipedia.org/wiki/Salt_(cryptography)