root@e8c1b013eb50:/# # let's make encryption first
root@e8c1b013eb50:/# echo -n 123 | openssl enc -aes-256-cbc -e -K 7 -iv 1 -a -p
hex string is too short, padding with zero bytes to length
hex string is too short, padding with zero bytes to length
salt=73C782D6357F0000
key=7000000000000000000000000000000000000000000000000000000000000000
iv =10000000000000000000000000000000
TUnoQ0azKuL6oto9DNDhHQ==
root@e8c1b013eb50:/#
root@e8c1b013eb50:/# # let's try to decrypt
root@e8c1b013eb50:/# echo -n "TUnoQ0azKuL6oto9DNDhHQ==" | openssl enc -aes-256-cbc -d -K 7 -iv 1 -a -p
hex string is too short, padding with zero bytes to length
hex string is too short, padding with zero bytes to length
salt=73470479277F0000
key=7000000000000000000000000000000000000000000000000000000000000000
iv =10000000000000000000000000000000
bad decrypt
139807510111552:error:0606506D:digital envelope routines:EVP_DecryptFinal_ex:wrong final block length:../crypto/evp/evp_enc.c:599:
root@e8c1b013eb50:/#
root@e8c1b013eb50:/# # oops, let's add extra newline for input
root@e8c1b013eb50:/# echo "TUnoQ0azKuL6oto9DNDhHQ==" | openssl enc -aes-256-cbc -d -K 7 -iv 1 -a -p
hex string is too short, padding with zero bytes to length
hex string is too short, padding with zero bytes to length
salt=73E7CADCB67F0000
key=7000000000000000000000000000000000000000000000000000000000000000
iv =10000000000000000000000000000000
123root@e8c1b013eb50:/# # see, it works with extra newline
root@e8c1b013eb50:/#
Let's check it out
- Encrypt
123
intoTUnoQ0azKuL6oto9DNDhHQ==
, here I believe it isTUnoQ0azKuL6oto9DNDhHQ==
rather thanTUnoQ0azKuL6oto9DNDhHQ==\n
, cipher data has no reason happens ending with\n
, and, even if cipher data is ending with\n
, here it is base64 encoded, so... Am I correct? - Since the encryption result is
TUnoQ0azKuL6oto9DNDhHQ==
, it has to beTUnoQ0azKuL6oto9DNDhHQ==
which goes to decryption process, but it failed with block length issue - If we provide extra
\n
, then decrypt went fine
Here is the point
If we encrypt A
into B
Then we should decrypt B
into A
That's what we define symmetric encryption
Not something like A
into B
, B\n
into A
So much confused, thanks in advance.
-a
option, whereby openssl requires the base64 string to be terminated with the\n
when feeding it a base64 input with this option. If you decode the base64 (without the\n
terminator) first, then feed the decoded bytes to openssl, it works, e.g.:echo -n "TUnoQ0azKuL6oto9DNDhHQ==" | base64 -d | openssl enc -aes-256-cbc -d -K 7 -iv 1 -p