Secure Data Communications Using GNU Privacy Guard (GPG)


Bhaskar S 05/03/2009


Introduction

Alice is on vacation and suddenly at work there are some issues with one of her systems running a critical application. Alice wants her peer Bob to look at the issue on her behalf. Alice is the only one who knows the password to unlock the system. How does she communicate that password to Bob ? Alice can email the password to Bob. Eve is a hacker and she knows how to intercept any email and extract the content.

One way to prevent Eve from eavesdropping would be that Alice and Bob agree on a secret key for all communication. In this case, to send the password to Bob, Alice would first encrypt the password with the secret key and send the encrypted password to Bob. Since Bob knows the secret key he shares with Alice, he can decrypt the encrypted password to get the actual password. Even if Eve intercepts the encrypted password, she would not be able to do anything with it. This technique is called Symmetric Key Encryption. However, there is a drawback with this approach. What if Alice wants to communicate in a secure manner with many others ? Alice will have to agree on different secret key with each of them and maintain the secret keys.

There a more elegant way of for Alice to communicate securely with Bob. This technique is called Asymmetric Key Encryption. In this technique, a mathematically generated pair of keys are involved – one is a Private Key and the other is a Public Key. The public key is known to the world, while the private key is kept secret. To send the password securely to Bob, Alice encrypts the password with Bob's public key and sends the content to Bob. On receiving the email, Bob decrypts the content with his private key. This gives Bob the original password to unlock the system. Important point here is that if data is encrypted with a public key, it can only be decrypted with the corresponding private key. Even if Eve intercepts the message she has no way of getting the secret information.

GNU Privacy Guard (GPG) is a free software from GNU Free Software Foundation that allows us to securely communicate using either the Symmetric or Asymmetric Key Encryption we just described above. It is available on various operating platforms like Linux, Windows, Mac OS X, etc.

For this article, we will be using GPG in the both the Windows and Linux environment. We will have Alice using the Windows Environment while Bob using the Linux Environment.

Also, lets assume that Alice wants to send the contents of the file secret.txt to Bob:

C:\>type secret.txt


Output.1

Hi Bob ,

Here is the password for the system: Super.Secret.Box

Rgds
Alice

Using GPG

We will first explore secure data communication using Symmetric Key Encryption.

Let us assume that Alice and Bob agree on the secret key: Alice.Bob.Secret

To encrypt the file secret.txt, Alice runs the following command (in Windows):

C:\>gpg --symmetric -o enc_secret.out secret.txt


Output.2

Enter passphrase: <enter the secret Alice and Bob share>
Repeat passphrase: <again enter the secret Alice and Bob share>

This will generate an encrypted file called enc_secret.out, which Alice can send to Bob. The encrypted file contains gibberish which will not make any sense to Eve if she intercepted this file.

To decrypt the file enc_secret.out, Bob runs the following command (in Linux):

$ gpg -d -o dec_secret.out enc_secret.out


Output.3

gpg: CAST5 encrypted data 
gpg: encrypted with 1 passphrase
Enter passphrase: <enter the secret Alice and Bob share>

This will generate a decrypted file called dec_secret.out:

$ cat dec_secret.out


Output.4

Hi Bob, 

Here is the password for the system: Super.Secret.Box

Rgds
Alice

Bob has successfully received the contents of the file secret.txt that Alice sent.

We will now explore secure communication using Asymmetric Key Encryption.

First, we will generate the asymmetric key pairs (public and private keys) for Alice (in Windows):

C:\>gpg --gen-key


Output.5

gpg (GnuPG) 1.4.9; Copyright (C) 2008 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) DSA and Elgamal (default)
(2) DSA (sign only)
(5) RSA (sign only)
Your selection? 1
DSA keypair will have 1024 bits.
ELG-E keys may be between 1024 and 4096 bits long.
What keysize do you want? (2048)
Requested keysize is 2048 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)
Key does not expire at all
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: Alice
Email address: alice@localhost
Comment:
You selected this USER-ID:
"Alice <alice@localhost>"

Change (N)ame, (C)omment, (E)mail or (O)kay/(Q)uit? O
You need a Passphrase to protect your secret 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.
+++++++++++++++++++++++++.+++++++++++++++.++++++++++.+++++.+++++++++++++++.+++++
+++++.++++++++++.++++++++++++++++++++++++++++++.+++++++++++++++++++++++++>++++++
++++.>+++++.....................................................................
...............................+++++^^^
gpg: C:/Documents and Settings/alice/Application Data/gnupg\trustdb.gpg: trustdb created
gpg: key E1E028F1 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
pub 1024D/E1E028F1 2009-05-04
Key fingerprint = 67D3 7F90 4087 D212 7EB6 FA12 1D14 CCA8 E1E0 28F1
uid Alice <alice@localhost>
sub 2048g/BEDAB8E4 2009-05-04

Next, we will generate the asymmetric key pairs (public and private keys) for Bob (in Linux):

$ gpg –-gen-key


Output.6

gpg (GnuPG) 1.4.6; Copyright (C) 2006 Free Software Foundation, Inc. 
This program comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to redistribute it
under certain conditions. See the file COPYING for details.

Please select what kind of key you want:
(1) DSA and Elgamal (default)
(2) DSA (sign only)
(5) RSA (sign only)
Your selection? 1
DSA keypair will have 1024 bits.
ELG-E keys may be between 1024 and 4096 bits long.
What keysize do you want? (2048)
Requested keysize is 2048 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)
Key does not expire at all
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: Bobby
Email address: bob@localhost
Comment:
You selected this USER-ID:
"Bobby <bob@localhost>"

Change (N)ame, (C)omment, (E)mail or (O)kay/(Q)uit? O
You need a Passphrase to protect your secret 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.
+++++...+++++++++++++++.++++++++++.+++++++++++++++....+++++.+++++..+++++++++++++++
++++++++++++++++++++.+++++++++++++++++++++++++++++++++++++++++++++++++++++++>...++
++++++++>.+++++.................................................................................
..................................................................................
......................................+++++^^^^
gpg: key 381DE629 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
pub 1024D/381DE629 2009-05-04
Key fingerprint = 217C 6343 8835 B698 7329 A15A 2B4B C0DA 381D E629
uid Bobby <bob@localhost>
sub 2048g/4B9DCFCD 2009-05-04

On Windows, Alice can check all the Public Keys as follows:

C:\>gpg –list-key


Output.7

C:/Documents and Settings/alice/Application Data/gnupg\pubring.gpg 
---------------------------------------------------------------------
pub 1024D/E1E028F1 2009-05-04
uid Alice <alice@localhost>
sub 2048g/BEDAB8E4 2009-05-04

As we can see there is only one Public Key that belongs to Alice. Similarly, Alice can check all the Private Keys as follows:

C:\>gpg –list-secret-key


Output.8

C:/Documents and Settings/alice/Application Data/gnupg\secring.gpg 
---------------------------------------------------------------------
sec 1024D/E1E028F1 2009-05-04
uid Alice <alice@localhost>
ssb 2048g/BEDAB8E4 2009-05-04

As we can see there is only one Private Keys that belongs to Alice.

On Linux, Bob can check all the Public Keys as follows:

$ gpg –list-key


Output.9

/home/bob/.gnupg/pubring.gpg 
---------------------------------
pub 1024D/381DE629 2009-05-04
uid Bobby <bob@localhost>
sub 2048g/4B9DCFCD 2009-05-04

As we can see there is only one Public Key that belongs to Bob. Similarly, Bob can check all the Private Keys as follows:

$ gpg –list-secret-key


Output.10

/home/bob/.gnupg/secring.gpg 
---------------------------------
sec 1024D/381DE629 2009-05-04
uid Bobby <bob@localhost>
ssb 2048g/4B9DCFCD 2009-05-04

As we can see there is only one Private Keys that belongs to Bob.

Now, both Alice and Bob need to exchange their Public Keys with each other.

On Windows, Alice will extract her Public Key to a text file as follows:

C:\>gpg --armor -o alice_key.txt --export alice@localhost

The contents of the text file alice_key.txt would be as follow:

Output.11

-----BEGIN PGP PUBLIC KEY BLOCK----- 
Version: GnuPG v1.4.9 (MingW32)

mQGiBEn+PzURBAD8M+85RrsUFtxwNZ0VlFmX9dJT1GUigTaMaZGe6law66du82jJ
u6IqS9oo/wLXaShZilTGEuqh7cUH+xtjr6VW3YtnwXYNtHn0WgosPR2dLo6MIbgo
6v7yDzQKzeWRyUuTqu6P1xqpFGew8Khsd8D6KaNSYjEvv52roybJCH9OjwCg9e4J
EMZUIxPc/ki1cRerqL2dgKcEAOtpJQno1qf5zzDuRnhH67IrU9H0cIrNgKXC8hTZ
jXb4vTyM+NkSRdagOXkDRDmOld3T/eaK+fI5s9VZDN2LMlTtdR59dIy5NwVSc3mR
h9oJk8VTEde8WLRBcwlZ96mhf7tfj1BTyRe9WkY3tqRP2QjLBZTCZgvm1lSd/OHu
dBuIA/41DN5wrjV/yL7R6RyoUvp31e00SdKxUdba7NU0c4aVsWa/w/nMwTkTTgtD
MZb52Vrfu6Hm7EQ7NPFW8o1wx5dh6c8XnKg4kR17QLducDBxDkfO/VRF2sWgKiQq
bkHFqtJddGQUwQWFoyhcuxJO5m2Irug44w/oYnr3hRPfk2tw0bQXQWxpY2UgPGFs
aWNlQGxvY2FsaG9zdD6IYAQTEQIAIAUCSf4/NQIbAwYLCQgHAwIEFQIIAwQWAgMB
Ah4BAheAAAoJEB0UzKjh4CjxbFsAn1d9inVycwdnvkywndLLO4CbZNZCAJ44Guwt
Dp2k8gezu45FdaA1HtZwPLkCDQRJ/j81EAgAhwZMjW1dKajgHMcjupJB0xJJe7xx
cE+3VkTnxGvk9NtkdSIhoq8LC20LUOmn7qPjOdYxfeeSTSnKpmlv4xjHuR/cJo+b
sAzLTG0srPo2Kw4OY4VBBMI+RrQCwmyke26hnknHkI6DSIg0zXLyzBaahs6ed6og
x8uyA0MRXY5F6el+6Ae4vqRp3u3Oq0phnWEaq6GCqUDOCWPU6hchGh3A0D+H89G8
2N9HN9hkNQGu7OdF/A90nLXPG/kiYTzsWO4TV/zLgKV34NZNzb7+KeupzOuS/UDC
HGGX5Wycr3Qc7tj6fKStyz/NCASiPTncDPz6ntCc6nV2WHy4Lk3Po0vaxwADBQf/
efHFbtC2u6TN/TYhh28Smg/qcLgDYWE7u5GVRx6q70+AInNWzyhbtygG/PHqTjGX
3lx3hLKS6kswuAZcwFRG5HG1ukZjx2wKs8jS1jX5q63WGPOB3nvU2/n3NQTgCtbg
Z5T4w8mQH5QlCz9rQwLmfEdJFQ62EpvtzMOPLgibxrt65hOEDFCc819oSUoOwKTS
QYP+apH8891sB3axJzd+zP5Su5nf0/83lG0n5UZzg9e28YUWO2jxxkkQvZsrqCe3
8n7RxKr0RxEERgkLTnvl74EM7WHw3dBhCliEnWi8+1msNfUUNbPwCNrtPW3p42Nc
hg8XBgWZVv4E/BHn1sFPl4hJBBgRAgAJBQJJ/j81AhsMAAoJEB0UzKjh4CjxUmAA
njyKc+G+2PEqbw/4H5qNiEToWZPyAJ9QnoX9D5T7cbGKiib6TPqlYczxZA==
=R7Iu
-----END PGP PUBLIC KEY BLOCK-----

Similarly, on Linux, Bob will extract his Public Key to a text file as follows:

$ gpg --armor -o "bob_key.txt" --export bob@localhost

The contents of the text file bob_key.txt would be as follow:

Output.12

-----BEGIN PGP PUBLIC KEY BLOCK----- 
Version: GnuPG v1.4.6 (GNU/Linux)

mQGiBE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aNWkyNauLhswIeJ+LG7MMAAwYH/0vE
p69eJXvcc48s2GzR8vAg4r3tnxM/qgFb+QI2rZ7V0Cgm/EktrflhEWYqBYZprbKj
l92MgoCp6SjHgCnhyidF5wl8biTHtNg9UN658yu3k/OzwNf+0OnL+jTvXICTAGxV
lVQxvtoY+c3kbd1MgZ1flKhINW3ajbwpi8zju2g+of4UqyzFLmdBNa4LJQbZaTEA
RpUQ4BYWHeU0RCYHhZ5PpxJt6pEdI9Svd+0wRN4SuuvIAZ2uob99p5ukNe5xLZXr
xxFi8941CIYIH9wXDkpCjhUyYSArL3/15ol07ZaU61Wi3IOGxiNLQNYqi1SMxgQK
Ut+I65eX/2yZOcUFsUyISQQYEQIACQUCSf5HxAIbDAAKCRArS8DaOB3mKV/0AJ9W
MbESkcNqJAZWQMspr0M0DQI59ACfU0+oB3J10l77XN3jZ617tOB3G68=
=ir+j
-----END PGP PUBLIC KEY BLOCK-----

Alice can now email her Public Key text file alice_key.txt to Bob. Similarly, Bob can now email his Public Key text file bob_key.txt to Alice.

On Windows, Alice will import Bob's Public Key text file bob_key.txt as follows:

C:\>gpg --import bob_key.txt


Output.13

gpg: key 381DE629: public key "Bobby <bob@localhost>" imported 
gpg: Total number processed: 1
gpg: imported: 1

Similarly, on Linux, Bob will import Alice's Public Key text file alice_key.txt as follows:

$ gpg –import alice_key.txt


Output.14

gpg: key E1E028F1: public key "Alice <alice@localhost>" imported 
gpg: Total number processed: 1
gpg: imported: 1

At this point, both Alice and Bob have each others Public Keys.

To encrypt the file secret.txt, Alice runs the following command (in Windows):

C:\>gpg --recipient bob@localhost -o enc_secret.out --encrypt secret.txt


Output.15

gpg: 4B9DCFCD: There is no assurance this key belongs to the named user 

pub 2048g/4B9DCFCD 2009-05-04 Bobby <bob@localhost>
Primary key fingerprint: 217C 6343 8835 B698 7329 A15A 2B4B C0DA 381D E629
Subkey fingerprint: F7DD 2639 7FD1 8B73 BAB5 FE11 65C5 7633 4B9D CFCD

It is NOT certain that the key belongs to the person named
in the user ID. If you *really* know what you are doing,
you may answer the next question with yes.

Use this key anyway? (y/N) y

This will generate an encrypted file called enc_secret.out, which Alice can send to Bob.

To decrypt the file enc_secret.out, Bob runs the following command (in Linux):

$ gpg -d -o dec_secret.out enc_secret.out


Output.16

You need a passphrase to unlock the secret key for 
user: "Bobby <bob@localhost>"
2048-bit ELG-E key, ID 4B9DCFCD, created 2009-05-04 (main key ID 381DE629)

Enter passphrase: <Bob will enter his passphrase>

gpg: encrypted with 2048-bit ELG-E key, ID 4B9DCFCD, created 2009-05-04
"Bobby <bob@localhost>"

This will generate a decrypted file called dec_secret.out:

$ cat dec_secret.out


Output.17

Hi Bob, 

Here is the password for the system: Super.Secret.Box

Rgds
Alice

Bob has successfully received the contents of the file secret.txt that Alice sent.

This should give you an idea how one can send sensitive information securely to another via the Internet.