Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

share|improve this question

migrated from stackoverflow.com Aug 10 '09 at 10:26

This question came from our site for professional and enthusiast programmers.

3 Answers 3

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.

share|improve this answer

If you have gpg installed, this is an industrial-strength encryption method.

gpg --encrypt -r recipient@example.com >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 recipient@example.com to decrypt the message.

share|improve this 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
share|improve this answer
    
+1 for openssl since it's more commonly installed than gpg is –  Doug Harris Aug 10 '09 at 14:08

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.