Public/Private Key RSA Encryption

Public-key cryptography made easy (contd.)

C05348A3-9AB8-42C9-A6E0-81DB3AC59FEB
           

I previously wrote about Python Encryption, this is a follow-up article on additional features of this tool.

Having someone send you an encrypted value

If you want someone to send you encrypted details, you can have them do so without requiring them to use the rsa_crypto library. You can for instance ask people to use the Online RSA Encryption, Decryption And Key Generator Tool(Free) website. Under "RSA Encryption", have them paste the content of your RSA Public key in "Enter Public/Private key"(don't include the "-----BEGIN PUBLIC KEY-----" and "-----END PUBLIC KEY-----" lines), which you can obtain using:

cat /home/ubuntu/rsa_public.pem

Under "Select Cipher Type" select "RSA/ECB/OAEPWithSHA-1AndMGF1Padding". Type the value you want to encrypt under "Enter Plain Text to Encrypt", click "Encrypt" and have them send you the content of the "Encrypted Output (Base64):" field.

It's that easy! 

Decrypting the encrypted value

You can also decrypt the value using that same website...but I don't know who runs it and I would never paste my private key on any website I don't fully trust. I would personally decrypt the values on a system with the rsa_crypto library installed (see Python Encryption for additional details on how to get started). Just edit the .rsa_values.conf file and add the value that was sent to you in the file, for instance as an option named database_password in the DEV section:

nano /home/ubuntu/.rsa_values.conf
[DEV]
database_password = <paste_your_value_here>

To decrypt the value using the default encryption key, use:

rsa_crypto get -s DEV -o database_password
Using key: /home/ubuntu/rsa_private.pem
get section: DEV option:database_password
Reading from /home/ubuntu/.rsa_values.conf
<decrypted_value_here>

Fairly straightforward!

Encrypting and Decrypting files

The rsa_crypto library can also be used to encrypt and decrypt entire files by using the "-f" option. To encrypt a file named my_file, simply use:

rsa_crypto encrypt -f my_file 
Using key: /home/ubuntu/rsa_public.pem
/home/ubuntu/my_file.enc

ls -alh
total 1.2G
...
rw-r--r-- 1 ubuntu ubuntu 6.0M Mar 11 03:50 my_file
-rw-rw-r-- 1 ubuntu ubuntu 6.0M Jun 22 11:51 my_file.enc

Note that the original file is still present, the encrypted file has the same name but with a .enc extension.

To decrypt the file, you can use:

rsa_crypto decrypt -f my_file.enc 
Using key: /home/ubuntu/rsa_private.pem
/home/ubuntu/my_file

Note that the decrypted file will overwrite the original if it is present without any warning, please test before using this option.

Posted Comments: 0

Tagged with:
encryption