Velvet Star Monitor

Standout celebrity highlights with iconic style.

updates

How can I encrypt a string in the shell?

Writer Matthew Barrera

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

0

4 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
4

If 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.

2
  1. 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 -pubout
  2. Encrypt the string using public key, and store in a file

    $ echo "stockexchange.com" | openssl rsautl -encrypt -inkey rsa_key.pub -pubin -out secret.dat
  3. Un-encrypt using private key

    $ string=`openssl rsautl -decrypt -inkey rsa_key.pri -in secret.dat `; echo $string
    stockexchange.com
1

man crypt(1)

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

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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