Hak5
Save 10% at GoDaddy.com with coupon code HAK

Pretty Good Privacy

From Hak5

Jump to: navigation, search
This article (or section) may need to be wikified.
Please help improve this article, especially its introduction, section layout, and relevant internal links.
This article has been tagged since February 2007.

Contents

Introduction

What is PGP?

PGP stands for "pretty good privacy". It is an implementation of several encryption techniques that have been brought together into a single application that you can use to send secure messages and files to other people over the internet.

The first release of pgp was in 1991 by Phil Zimmermann. PGP is no longer free and is maintained by pgp corporation. However pgp is now an open standard and there are other implementations of this. The most popular free version is the GNU privacy guard, or gpg. Version 1.4.5 is the version that will be featured in this tutorial.

Some Basic Concepts on Cryptography

This is a basic tutorial, so no discussions about anything like Z=(Y^X)modN however there are a few basic concepts on cryptography that you will probably need to understand to make proper use of pgp.

The following is only a VERY basic introduction.

Private Key Cryptography

The basic principal behind public key cryptography is that it uses a symmetric cipher to scramble data. Whatever the technique, the key points are that you need a key to encrypt your data and then you use that SAME key to decrypt it. You need to know that key once the data is encrypted in order to decrypt it again. If you don't have the key then you can't decrypt your data.

Examples of this type if cryptography include AES(also known as Rijndael), Serpent, Blowfish and RC4.

Public Key Cryptography

This type of cryptography uses two keys during the encryption process, a public key and a private key. The public key is offered publicly to everyone who wants it. The private key is kept private. If an imaginary character Alice, wanted to send a message to Bob, she would have to get Bob's public key from a website or some other place. She would then encrypt the message using Bob's public key and send it to Bob. Bob would then decrypt it using his private key. The important thing to note is that Alice does NOT use her public or private keys but instead uses Bob's public key to encrypt the message that she wants to send to Bob.

The most commonly used public key cipher is RSA.

Cryptographic Hashes

The third key piece in cryptography isn't an encryption method like the other two but a method of creating hashes of text or data that are difficult to forge.

First though, what is a hash? A hash is a little piece of data that is tied to a usually much larger piece of data. A way of explaining them is to use an analogy from mathematics, using our 2 friends from before. Suppose Alice wanted to tell Bob how many mp3s she has on her ipod. If she were talking to him over a a phone line that was very distorted and difficult to hear she and Bob mightn't be absolutely sure that the correct number was given to Bob, since he might have heard wrong when he was being told that she had 1542 songs on her ipod. What she could do is take that number and divide it by 9. She would have a remainder of between 0 and 8. In this case 1542/3 gives a remainder of 3. She could tell Bob, that she had 1542 songs and that the hash for this number is 3. If Bob took the number that he was given and also did the same thing, where he divided the number by 3, he would be fairly certain that the number he was told was accurate. Cryptographic hashes work on a similar principal, but are much more complicated and it is extremely difficult to find 2 pieces of data that will both return the same hash. It's important to note that hashes work only one way, you can get a hash from a piece of data, but getting the original data is impossible.

Common hash methods include MD5 and SHA

Getting Started

Getting GPG

The gpg site is http://www.gnupg.org/ you can get the source and windows binaries from there. GPG is usually installed by default on most distributions of Linux or on the BSDs, but you can probably install binary packages from your distributor.

Using GPG

Key Generation

The first thing that you're going to want to do is create a key pair for yourself this is pretty easy, the command is

gpg --gen-key 

this takes you through a list of options that lead to a keypair

  • the first option "select what kind of key you want" the best option is (1) DSA and Elgamal.
  • the second option, "what keysize do you want?" lets you choose your keysize. 2048 is the default, this will take a few seconds to generate on most computers and is secure enough for basically all applications. A 4096 bit key will take a few minutes to generate, but will otherwise perform just the same as a shorter one.
  • "please specify how long the key should be valid" Here you probably want to type in "5y" (without the talking marks) for a keypair that will be valid for 5 years.
  • "Real name" here you want to type in your name
  • "Email address" this is your email address
  • "Comment" if you would like to add an extra comment, such as "John Smith pgp key" but people usually get what a pgp key is for, so no need to state the obvious

you are then presented with the information that you entered, which you can correct if necessary

if you select "o" for okay, you will be prompted for a passphrase that you have to enter twice. This passphrase will encrypt your public key with a symmetric cipher so that only somebody with the password will be able to access your private key and decrypt your messages gpg will then generate a keypair, which should take a few seconds for a 2048 bit key. You can move your mouse around to speed things up if necessary.

you can use

gpg --list-keys

to list all the keys in your keyring

Sending Encrypted Messages

Now that you have a keypair you want to use it to send encrypted messages to somebody else who is also using pgp software. However, to do so, you're going to need to get that other person's public key from a keyserver or somewhere else. You can access MIT's keyserver from http://pgp.mit.edu/ In the search field, type "Leo Laporte". At the top of that list should be leo's most recent pgp key. Click on the keyID to download leo's public key. Select only the part of the text beginning with "-----BEGIN PGP PUBLIC KEY BLOCK-----" and ending with "-----END PGP PUBLIC KEY BLOCK-----" and save it to a text file named "leo.txt" in the folder in which you were working with gpg earlier. From your shell you can now use

gpg --import leo.txt

to import leo's public key into your keyring. You no longer need the file leo.txt, since it was copied over to your keyring. you can use the command from earlier "gpg --list-keys" to list the keys in you keyring. You should see that leo's has been added.

you're probably going to want to edit some of the settings for this new key, to do so use the following command

gpg --edit-key user

where "user" is anything that can be used to identify the recipient, that can be their keyID, although it's usually easiest to just use their name, surrounded by talking marks if they have used both their names, or their email address. Email addresses are embedded in a person's public key, so gpg looks for the address that's attached to a key. The above command takes you to a prompt, you can use "help" to list available options. Use the command "trust". This gives you a list of options that describe a scale of how well you trust the public key. Use option 3, that you only trust this marginally, then use quit to get back to your shell prompt.

Type in a small message in a text file, save it to text_file.txt To encrypt a message for leo, use the command

gpg --output message.gpg --recipient "Leo Laporte" --encrypt text_file.txt

The above command takes the file text_file.txt and encrypts it for Leo to read. It outputs the message as message.gpg There are variations on the above that could have been performed in order to suit particular need. The first of this is that --recipient "Leo Laporte" could have included a longer list of recipients. It's possible to encrypt messages so that multiple people can read them. If you has several people that you wanted to read your message, you could have used --recipient "Leo Laporte" john@smith.com A8F99067 this would have allowed you to create a message that would have been readable to 3 different people. Remember that there are multiple ways that you can choose recipients. You could have also decided to leave the recipient blank

gpg --output message.gpg --encrypt text_file.txt

this takes you to a prompt that allows you to choose recipients one at a time. When you are finished, just enter nothing for the last recipient.

While the above gives you a file that you can attach to an email, most people use a gpg embedded into their email clients, it's much easier if you send them a version of the encrypted message that is a regular text file, use the --armor option for that

gpg --armor --output message.gpg.asc --recipient "Leo Laporte" --encrypt text_file.txt

note the added .asc on the output file. This is helpful to tell you that you have created a text version of the encrypted message. You can now open message.gpg.asc with a regular text editor and copy the encrypted message into the body of an email to send to a friend.

Signing Messages

An important feature that pgp offers is the ability to sign messages. If everyone has access to your friend's public key, anyone can send him an encrypted message. Signing messages allows your friend to be certain that it came from you. First, how does this work: An important concept in public key cryptography is that there is a public key and a private key. These two keys both work in exactly the same way. If you encrypt a message with one, you use the other to decrypt that message. There is no difference between which one can encrypt and which can decrypt. We choose to call one a public key and the other private so that we can keep one safe and we can give the other one out to our friends and whoever else wants to see. Giving them names help us protect the security of the private key. You should NEVER send out your private key to anybody if you don't want to compromise the security pgp offers you. What this allows us to do is to encrypt a hash of a message with our private keys, and then allow our friends to decrypt that hash with our public key. Since only we have the private key, only we can create messages that decrypt properly with our private keys. Hence recipients can be sure that messages are authentic. It's possible to only sign messages, so that they can be read by everyone, and whoever wants to can be sure of the origin of those messages. You can also add privacy by encrypting the message as well as signing. Signing a message is done using the following

gpg --clearsign massage.txt

the following gives a file message.txt.asc which is the same the original message, it has not been encrypted, but it is wrapped in an ascii signature. This can now be sent as an email or usenet message and recipients can be sure of its authorship. This is preferred over the following method because it's easier to cut and paste around than the following method If you would prefer a separate file as a signature, you can use the following

gpg --output sig.gpg --sign message.txt

the signature sig.gpg can but sent in a zip file along with the original massage. Using this method, you shouldn't try to cut and paste the original message.txt into an email, becuse unless you get every single character right, including not missing the few times you pressed enter at the end of your document, it's going to throw off the signature and declair your message as not being authentic.

You would verify a signed file or message using the following

gpg --verify sig.gpg

where sig.gpg is either the clearsigned message, if it was given to you as a complete message wrapped in a signature, or the signature file, assuming the message and the signature file are in the same directory.

Finally, to send messages that are both encrypted and signed, use the following

gpg --armor --recipient "Leo Laporte" --sign --output message.gpg.asc --encrypt message.txt

this command is basically the same as to encrypt, but with the additional "--sign" flag. This will output a file that is encrypted and has an embedded signature, it will not have a separate signature file like plaintext messages that are signed. Messages that are signed and encrypted have their signatures verified during decryption, so there are no additional commands required to verify messages that have been both signed and ecrypted. You should usually sign all messages that are also encrypted.

Final Notes

If you have read through the tutorial you may be disappointed to find out that you will likely never actually have to use any of the above commands. Most popular email clients, as well as the popular UNIX desktops, have easy to use graphical applications which act as front ends to gpg. They give you all of the above functionality with much less effort. Often not requiring to save files and open up a shell, instead being able to decrypt and verify messages straight form the email client itself.