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

元气小坏坏 提交于 2019-12-02 05:12:55
podiluska

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)

The reason you are getting different encrypted string is " enc -aes-128-cbc". CBC stands for Cipher Block Chaining. So, for 2nd block, the encrypted output of first block acts ac IV, so each time you get different string. for more details google "AES in CBC mode"

You get different outputs on each run because new salt is generated each time you run the command. In order to provide the same salt for each consecutive run use -S salt option, i.e.

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

The reason is that the actual key which is used for encryption is driven from your passphrase and the SALT. Then definitely the ciphertext will be different even if you still use the same password because the SALT is different.

Openssl uses salt by default to mitigate dictionary attacks. If you don't want to use it then use same salt as suggested by other answers, or add nosalt option as follow:

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

You can see the ciphertext in hex using xxd

xxd encrypted.bin
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!