Reputation: 7533
I am trying to encrypt
and decrypt
text from command line using openssl
. I am able to encrypt
the text but I am not able to decrypt
it back.
Here is how I am encrypting my text from command line:
➜ ~ echo -n 'Foo Bar' | openssl enc -aes-128-ecb -K 123456789 | openssl enc -base64
Above command gives me following output:
RxmxBbcIFm5ZMiQIBYDr4Q==
Here is how I am trying to decrypt my text:
➜ ~ echo -n 'RxmxBbcIFm5ZMiQIBYDr4Q==' | openssl enc -aes-128-ecb -K 123456789 | openssl enc -base64 -d
When I run the above command, I am not getting any output displayed on the screen:
➜ ~
Any idea how to decrypt the code from terminal?
Thanks
Upvotes: 0
Views: 2037
Reputation: 299265
This part:
openssl enc -aes-128-ecb -K 123456789
outputs raw data. When you try to decrypt it, you're adding -base64
. The output isn't base64, so it fails. In your particular case, what you seem to mean is this:
echo -n 'Foo Bar' | openssl enc -aes-128-ecb -K 123456789 | openssl enc -aes-128-ecb -d -K 123456789
To encrypt and output base64, you want:
echo -n 'Foo Bar' | openssl enc -aes-128-ecb -K 123456789 -base64
To decrypt and accept base64, you want:
echo 'RxmxBbcIFm5ZMiQIBYDr4Q==' | openssl enc -d -aes-128-ecb -K 123456789 -base64
As user1516873 notes, to use base64 input, you need a trailing newline, so no -n
, but that wasn't the major problem in your code. It only applies to Base64, not generally to encryption or decryption. You could also use -A
and not pass a newline. -base64
is the same as -a
, which is expecting a multi-line base64 block, while -A
expects it all on one line (so doesn't need the \n).
Upvotes: 1
Reputation: 5173
Problem in -n flag in echo
command. openssl required \n symbol for successful decryption.
compare
$ echo 'Foo Bar' | openssl enc -e -aes-128-ecb -base64 -K 123456789
eoGjHSco3ee2nOjibu7a3g==
and
echo -n 'Foo Bar' | openssl enc -e -aes-128-ecb -base64 -K 123456789
RxmxBbcIFm5ZMiQIBYDr4Q==
so it affects both, encryption and decryption.
$echo eoGjHSco3ee2nOjibu7a3g== | openssl enc -d -base64 -aes-128-ecb -K 123456789
Foo Bar
$
and
echo RxmxBbcIFm5ZMiQIBYDr4Q== | openssl enc -d -base64 -aes-128-ecb -K 123456789
Foo Bar$
Upvotes: 1