Gpg Key-Pair Encryption and Decryption

Using GPG we can generate private and public keys which can be used to encrypt and decrypt files. Here, the private key should be kept confidential. Even if the public key is made available, the message cannot be decrypted.

Consider a person A wants to send a secret message to person B. B’s public key is known to A and A encrypts the message using this. However, this encrypted message can be decrypted only by B since B alone knows the private key.

Let’s see how to generate a key pair and thereby performing encryption and decryption.

Step 1: Key pair generation

$ gpg --gen-key
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
RSA keys may be between 1024 and 4096 bits long.
What keysize do you want? (2048) 3000
Requested keysize is 3000 bits
rounded up to 3008 bits
Please specify how long the key should be valid.
         0 = key does not expire
      <n>  = key expires in n days
      <n>w = key expires in n weeks
      <n>m = key expires in n months
      <n>y = key expires in n years
Key is valid for? (0) 5
Key expires at Wednesday 23 January 2013 07:01:08 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) <heinrichh@duesseldorf.de>"

Real name: sowmya
Email address: sowmya@gmail.com
Comment: This is my OS assignment
You selected this USER-ID:
"sowmya (This is my OS assignment) <sowmya@gmail.com>"

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

The Passphrase that we provide is our private key.

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.
.....+++++
.......+++++
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.

Not enough random bytes available.  Please do some other work to give
the OS a chance to collect more entropy! (Need 171 more bytes)
...+++++

Not enough random bytes available.  Please do some other work to give
the OS a chance to collect more entropy! (Need 63 more bytes)
.+++++
gpg: key 014D1F5E 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:   1  signed:   0  trust: 0-, 0q, 0n, 0m, 0f, 1u
gpg: next trustdb check due at 2013-01-23
pub   3008R/014D1F5E 2013-01-18 [expires: 2013-01-23]
      Key fingerprint = 26AE 91CA E577 C4C4 F168  16D5 4A83 6C5D 014D 1F5E
uid                  sowmya (This is my OS assignment) <sowmya@gmail.com>
sub   3008R/D39FA9A6 2013-01-18 [expires: 2013-01-23]

014D1F5E is our public key.

Step 2: Exporting the key

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

Importing other’s public key

gpg --import FileName

Send encrypted message

gpg --recipient 'public_key' --armor --encrypt secret.txt

Decrypt the encrypted message

Consider a file ‘secret.txt’ which is encrypted as secret.txt.asc. To decrypt secret.txt.asc:


$ gpg --decrypt secret.txt.asc > secret.txt

You need a passphrase to unlock the secret key for
user: "sowmya (This is my OS assignment) <sowmya@gmail.com>"
3008-bit RSA key, ID D39FA9A6, created 2013-01-18 (main key ID 014D1F5E)

gpg: encrypted with 3008-bit RSA key, ID D39FA9A6, created 2013-01-18
      "sowmya (This is my OS assignment) <sowmya@gmail.com>"

References: http://linux.101hacks.com/unix/gpg-command-examples/

Advertisements

Tagged: , ,

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s