Can I encrypt a message (string) using a public key at the command prompt? Also, how can I decrypt the result afterwards?
04 Answers
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 4If you have gpg installed, this is an industrial-strength encryption method.
gpg --encrypt -r >tempfile
Type data at the console and press Ctrl+D to end the text. This will give you encrypted data in tempfile. To decrypt:
gpg --decrypt <tempfile
You will need the passphrase for to decrypt the message.
Generate a private/public key pair
$ openssl genrsa -out rsa_key.pri 2048; openssl rsa -in rsa_key.pri -out rsa_key.pub -outform PEM -puboutEncrypt the string using public key, and store in a file
$ echo "stockexchange.com" | openssl rsautl -encrypt -inkey rsa_key.pub -pubin -out secret.datUn-encrypt using private key
$ string=`openssl rsautl -decrypt -inkey rsa_key.pri -in secret.dat `; echo $string stockexchange.com
note:
crypt implements a one-rotor machine designed along the lines of the German Enigma, but with a 256-element rotor. Methods of attack on such machines are widely known, thus crypt provides minimal security.
But it's OK for demonstration purposes.
1