I am attempting to follow the example in this question: Where is the salt on the OpenSSL AES encryption? but I'm having some trouble decrypting using the key and initialization vector.
When I encrypt as follows, using "abc" as the passphrase:
$ echo -n Polaco | openssl enc -aes-256-cbc -a -p
enter aes-256-cbc encryption password:
Verifying - enter aes-256-cbc encryption password:
salt=0EEC20170C1B2A76
key=33945888AB044FE34F69289E3186FEA7DB914BF2ED37C2DE123117FB494ACDD8
iv =0C4CAA4A27FE3BFB05399AF217D24357
U2FsdGVkX18O7CAXDBsqdsqiOcaoPpa2OcFAtk2jQGY=
I can decrypt the resulting text using the same passphrase:
$ echo U2FsdGVkX18O7CAXDBsqdsqiOcaoPpa2OcFAtk2jQGY= | openssl enc -d -a -aes-256-cbc -p
enter aes-256-cbc decryption password:
salt=0EEC20170C1B2A76
key=33945888AB044FE34F69289E3186FEA7DB914BF2ED37C2DE123117FB494ACDD8
iv =0C4CAA4A27FE3BFB05399AF217D24357
Polaco
But when I try to decrypt using the key and initialization vector instead of the passphrase, it fails:
$ echo U2FsdGVkX18O7CAXDBsqdsqiOcaoPpa2OcFAtk2jQGY= | openssl enc -d -a -aes-256-cbc -p -K 33945888AB044FE34F69289E3186FEA7DB914BF2ED37C2DE123117FB494ACDD8 -iv 0C4CAA4A27FE3BFB05399AF217D24357
salt=1250FC9CF97F0000
key=33945888AB044FE34F69289E3186FEA7DB914BF2ED37C2DE123117FB494ACDD8
iv =0C4CAA4A27FE3BFB05399AF217D24357
bad decrypt
4294956672:error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt:evp_enc.c:531:
Note that the salt extracted from the encrypted text is wrong.
I'm using an x86_64 machine, running Cygwin on Windows 10, if it matters.
$ openssl version
OpenSSL 1.0.2n 7 Dec 2017
I feel like I must be missing something basic.
SOLVED by @mvy The problem was that a salt is randomly generated by default, but when you are specifying the key and iv for decryption, there should not be a salt. I need to suppress the salt using the -nosalt option.
$ echo -n Polaco | openssl enc -aes-256-cbc -nosalt -p -out /tmp/pol1
enter aes-256-cbc encryption password:
Verifying - enter aes-256-cbc encryption password:
key=900150983CD24FB0D6963F7D28E17F72EA0B31E1087A22BC5394A6636E6ED34B
iv =2EFFA65AF1C5EB20572E2F9896B90FEB
$ openssl enc -d -aes-256-cbc -nosalt -p -in /tmp/pol1 -K 900150983CD24FB0D6963F7D28E17F72EA0B31E1087A22BC5394A6636E6ED34B -iv 2EFFA65AF1C5EB20572E2F9896B90FEB
key=900150983CD24FB0D6963F7D28E17F72EA0B31E1087A22BC5394A6636E6ED34B
iv =2EFFA65AF1C5EB20572E2F9896B90FEB
Polaco