Thursday, February 18, 2016

OpenSSL RSA commands to encrypt/decrypt a message in terminal

It is possible to use OpenSSL commands to:

(1) generate an RSA private/public key pair.
(2) encrypt and decrypt a message using the private/public keys generated.

Some explanations:
(1) Generate an RSA private key first. The public key can then be obtained from the private key.
(2) Use different keys of the same key pair for encrypting/decrypting. For example, encrypt a message with the public key and then decrypt the encrypted message with the private A cryptosystem using this public-private key mechanism is known as asymmetric because different keys are used for encrypting/decrypting.

The following terminal commands have been tested on a Raspberry Pi and a Mac.

For Raspberry Pi, commands may need an extra sudo word in front of openssl or executed in the desktop folder.

1. Generate an RSA private and public key pair in PEM format:

Generate a 1024-bit private key:

openssl genrsa -out private_key.pem 1024

Obtain a public key from the private key:

openssl rsa -in private_key.pem -pubout -out public_key.pem



2. Create a message.txt file and edit its content:

sudo nano message.txt



3. Encrypt message.txt with public_key.pem using this command:

openssl rsautl -in message.txt -encrypt -pubin -inkey public_key.pem > encrypted.txt



4. Decrypt encrypted.txt with private_key.pem using this command:


No comments:

Post a Comment