Linux – How to encrypt a string in the shell


Can I encrypt a message (string) using a public key at the command prompt? Also, how can I decrypt the result afterwards?

Best Answer

Another option is openssl:

# generate a 2048-bit RSA key and store it in key.txt
openssl genrsa -out key.txt 2048

# encrypt "hello world" using the RSA key in key.txt
echo "hello world" | openssl rsautl -inkey key.txt -encrypt >output.bin

# decrypt the message and output to stdout
openssl rsautl -inkey key.txt -decrypt <output.bin