6

I want to encrypt a bunch of strings using openssl. How do I pass plaintext in console to openssl (instead of specifying input file which has plaintext).

openssl man page has only these two options related to input/output:

-in <file>     input file
-out <file>    output file

Here is what I have tried so far:

This works fine,

openssl aes-256-cbc -a -K 00000000000000000000000000000000 -iv 00000000000000000000000000000000 -in plain.txt -out encrypted.txt

If I omit the -out parameter I get encrypted string in console,

openssl aes-256-cbc -a -K 00000000000000000000000000000000 -iv 00000000000000000000000000000000 -in plain.txt

But If I omit both -in and -out, I get an error - unknown option 'Encrypt ME',

openssl aes-256-cbc -a -K 00000000000000000000000000000000 -iv 00000000000000000000000000000000 "Encrypt ME"
11

Use this:

user@host:~$ echo "my string to encrypt" | openssl aes-256-cbc -e -a -K 00000000000000000000000000000000 -iv 00000000000000000000000000000000
a7svR6j/uAz4kY9jvWbJaUR/d5QdH5ua/vztLN7u/FE=
user@host:~$ echo "a7svR6j/uAz4kY9jvWbJaUR/d5QdH5ua/vztLN7u/FE=" | openssl aes-256-cbc -d -a -K 00000000000000000000000000000000 -iv 00000000000000000000000000000000
my string to encrypt

Or you could use command substitution:

user@host:~$ openssl aes-256-cbc -a -K 00000000000000000000000000000000 -iv \
00000000000000000000000000000000 -in <(echo "my string to encrypt") -out encrypted.txt
  • Thanks chaos. 'command substitution' is what I was looking for. – Nitish Parkar Aug 8 '14 at 9:37
  • In all of the answers here, echo is used without the "-n" option so it will include a trailing newline that will be included in the hash input. – Ben C Jan 16 '18 at 11:54
2

Enter multiline input, use ctrl+d to finish. e='e' will clear the env var, for privacy.

e=$(cat); echo "$e" | openssl enc -aes-256-cbc -e -a; e='e'
1

what about

echo encrypt Me | openssl aes-256-cbc -a -K 00000000000000000000000000000000 -iv 00000000000000000000000000000000

If your concerne is that a ps will show the plain text 'encrypt me', then you'd better sitck with file, beeing carefull to erase them.

  • Thanks. I am not concerned about command history but I will keep that in mind. – Nitish Parkar Aug 8 '14 at 9:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.