Encrypting and Decrypting a file using GPG key pair

GNU Privacy Guard (GPG) is a tool to provide encryption and signing services for transferring data securely. This post is about generating a public-private key pair for encrypting and decrypting a message. GPG generates a public key of the size preferred by you for a given email id, and a pass-phrase which is the private key.

Generating the Key pair

It is very simple to generate a key pair in GPG. Follow the instructions

$ gpg --gen-key

Then you get the following

gpg (GnuPG) 1.4.11; Copyright (C) 2010 Free Software Foundation, Inc.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Please select what kind of key you want:
   (1) RSA and RSA (default)
   (2) DSA and Elgamal
   (3) DSA (sign only)
   (4) RSA (sign only)
Your selection? 1  [Select any option]
RSA keys may be between 1024 and 4096 bits long.
What keysize do you want? (2048) 2048
Requested keysize is 2048 bits
Please specify how long the key should be valid.
         0 = key does not expire
        = key expires in n days
      w = key expires in n weeks
      m = key expires in n months
      y = key expires in n years
Key is valid for? (0) 2w [You can select any based on the time limit you want]
Key expires at Wed 16 Jan 2013 05:22:13 PM IST
Is this correct? (y/N) y

You need a user ID to identify your key; the software constructs the user ID
from the Real Name, Comment and Email Address in this form:
    "Heinrich Heine (Der Dichter) "

Real name: Priya
Email address: priya1111@yahoo.co.in
Comment: (Trial Key Pair)
Invalid character in comment
Comment: Trial Key Pair
You selected this USER-ID:
    "Priya (Trial Key Pair) "

Change (N)ame, (C)omment, (E)mail or (O)kay/(Q)uit? o
You need a Passphrase to protect your secret key.

gpg: problem with the agent - disabling agent use
We need to generate a lot of random bytes. It is a good idea to perform
some other action (type on the keyboard, move the mouse, utilize the
disks) during the prime generation; this gives the random number
generator a better chance to gain enough entropy.
gpg: key 68A4DDFE marked as ultimately trusted
public and secret key created and signed.

gpg: checking the trustdb
gpg: 3 marginal(s) needed, 1 complete(s) needed, PGP trust model
gpg: depth: 0  valid:   3  signed:   0  trust: 0-, 0q, 0n, 0m, 0f, 3u
gpg: next trustdb check due at 2013-01-04
pub   2048R/68A4DDFE 2013-01-02 [expires: 2013-01-16]
      Key fingerprint = E2C2 22B4 F94E 5569 F5FE  48D1 94E5 A4F4 68A4 DDFE
uid                  Priya (Trial Key Pair)
sub   2048R/370B0DB3 2013-01-02 [expires: 2013-01-16]

The pass-phrase you entered is your private key.

Exporting the public key

gpg --export priya1111@gmail.com > priya1111-pub.gpg

This file will contain the public key. If you want to get the Public key in ascii, give

gpg --armor --export priya1111@gmail.com > priya1111-pub-asc.gpg

Importing Public Key

gpg --import priya1111@gmail.com <filename>

Encrypting a file

Consider you want to encrypt the file sun.txt and send it to Priya
Give the command as follows

$ gpg --recipient priya_key --armor --encrypt sun.txt

Here priya-key contains the public key of priya1111@gmail.com. The above commands generates a file
sun.txt.asc which is the encrypted file. You can send this file through gmail securely as it
is in the encrypted format

Decrypting a file

You can decrypt the file with your private key.

gpg --decrypt sun.txt.asc > sun.txt

You will get the decrypted file.

There are ways to add signature to your file using the public key. You can even add photo ID as a part of the signature. You can refer the following link if interested.
click here


3 thoughts on “Encrypting and Decrypting a file using GPG key pair

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s