Wednesday 18 January 2017

How to encrypt and decrypt files using OpenPGP encryption


Below are some basic simple and useful OpenPGP commands on linux for newbies without going too much into details like encryption algorithms, key management, etc.

First step for encryption is generating the keys:
gpg2 --gen-key

The below command is used for listing the keys once they are created:
gpg2 --list-secret-keys

We need to export the public key to a file to be able to send it out:
gpg --armor --export you@domain.com > mypublickey.asc

Or we can export to the public key to a key server if we have one:
gpg --keyserver search.keyserver.net --send-key you@domain.com

We can export the private key using below command:
gpg2 --armor --export-secret-keys your_email@domain.com > myprivatekey.asc

One the other end,  we need to import the public key first:
gpg2 --import senderpublickey.asc

Next step is to encrypt a file using public key on the other end:
gpg2 --encrypt file.txt --output file.gpg

Once file encrypted using public key, it will be sent back to us and we can decrypt using private key:
gpg2 --output file.txt --decrypt file.gpg

No comments:

Post a Comment