PGP, or Pretty Good Privacy, is a commonly used and very secure encryption program using public key cryptography. Through PGP, you can encrypt information such as messages, documents and files in a manner so that only the recipient can decrypt and open them.
The goal of this tutorial is to get you up and running with PGP through terminal and familiar with its operation.
An Encryption Primer
There are two types of encryption, symmetric and asymmetric (also called public key). Symmetric encryption uses one key to encrypt and decrypt messages. Often, symmetric encryption works out very well as there are extremely strong algorithms available that secure data very quickly. However, symmetric encryption carries the major drawback that a key must be transported securely to the recipient. In regards to communicating through the internet, this is not really feasible.
Asymmetric encryption however, uses two keys, a public and private one. When using asymmetric encryption, you offer your public key to everyone while keeping your private key secret. Messages that are encrypted with your public key can only be decrypted with your private key. Because of this, you can send and receive encrypted messages with another party without having to transport an encryption key through secure means.
The other benefit of asymmetric encryption is that key pairs can be used to authenticate messages too. This is done by encrypting information with your private key. Upon receiving a message, the recipient will use your public key to decrypt it. Encrypting any information with a private key for authentication purposes is referred to as creating a digital signature.
For example. I have my public key posted here as well on multiple public key servers. Whenever someone wants to send me an encrypted message, they use my public key to encrypt it and their private key to sign it. Once this is done, the only way to decrypt the message is to use my private key, which I keep heavily secured. Upon receiving the encrypted message, I decrypt it with my private key and authenticate it with the senders public key.
Easy enough, lets get started.
Installing the Software
1. PGP comes in many implementations, however the one you will be using in this tutorial is GPG (GNU Privacy Guard) as it follows the OpenPGP standard, is completely free and offers an easy to use Mac OS X installer.
Get the Mac GPG installer for your version of Mac OS X from sourceforge.
What are PGP Implementations?
PGP was at one time a commercial product by PGP Incorporated. However, realizing that a strong open source encryption mechanism was so important, the encryption community and PGP Inc. worked together to make the OpenPGP standard. Through OpenPGP, compatible variations of PGP could be created by third parties and distributed freely.
Learn more about OpenPGP here.
2. Get Paranoid (optional)! You would not be reading this if you trusted everyone, so stay in that frame of mind. The next step is to verify the MD5 checksum for the file you just downloaded. This way, you are sure the installer came from Mac GPG and not a man in the middle. Navigate your terminal to the downloaded image and type the following (where the file name and MD5 checksum match for your particular version):
What did I just do?
Files can be digested to create a string relatively unique to them, and one such digestion algorithm is MD5 (Machine Digest 5). Next to the download link for the installer on Mac GPG’s webpage , you will also see a 32 character string, this is the MD5 digest for the disk image available for download.
In terminal, we use the md5 command to digest GnuPG1.4.8.dmg. We then grep it for the MD5 string posted on Mac GPG’s website. If it matches, it will echo the value. If not, it will return a blank.
Learn more about MD5 here.
3. Now that we know the disk image is legitimate, we can mount it and install Mac GPG. This is a simple process as everything is done through an installation wizard. Just follow the steps and you will be up and running in no time.
Generating a Key
Now that you have GPG installed, you will start generating your key pair and adding it to your key chain. All of this will be done through the shell, so open a terminal and get ready for some typing.
1. Start the process for generating your key pair. Upon doing so, you will be asked to choose between three key types, pick the default DSA and Elgamal type by pressing enter.
NOTE: As this is your first time generating keys, GPG will warn you about creating a ~/.gnupg directory and exit. Just run the command again and you will be good to go.
What is DSA and Elgamal?
DSA (Digital Signature Algorithm) and Elgamal (Also called ELG) are the encryption algorithms used by GPG. DSA, as the name suggests, is used for authentication while Elgamal is used for encrypting data.
2. Choose your Elgamal encryption (ELG-E) key size. GPG uses 1024 bit keys for DSA, however allows you to choose your Elgamal key length. The default 2048 bit length should be plenty, so press enter.
3. Choose your key pair’s expiration date. Unless you are really paranoid, the default non-expiring key should be fine. Press enter.
4. Confirm that your key will not expire by typing “y” and pressing enter.
5. Create an identity for your key. There are three parts, your name, email address and comment. The comment for your key is optional, and is mostly used when you choose to have more then one key pair to keep track of. Fill in your information, type “O” and press enter.
6. Choose your passphrase. In order to keep your private key secure (else someone could open your computer and read it), PGP will encrypt it with symmetric encryption. The passphrase you are about to give will be used as the encryption key. Everytime you want to use your private key, PGP will ask for your passphrase. If it is correct, PGP will decrypt your private key and use it. Choose something long (hence the term pass phrase) and press enter. You will then be asked to confirm your passphrase. Do so and press enter again.
What if I forget my passphrase?
If passphrases were easily cracked, they would not be all that useful. As such, your only real option is to make a new key pair.
Later in this tutorial, you will go through the process of making a revocation certificate, so you can “turn off” your public key if this were to happen.
Learn more about passphrases here.
7. GPG will then start to print some gibberish while it generates your key pair. During this period, GPG will also ask you to do something with your computer such as typing on the keyboard, moving your mouse or using your hard disk drive. This serves the purpose of creating more entropy and helps in generating a better key.
What is entropy and why does it generate a better key?
In computing terms, entropy is randomness. Computers are actually not that good at making random decisions, however people are. By moving the mouse and pressing keys, you are making your computer “randomly” process instructions and thus, generate a stronger key pair.
Randomness is very important for generating encryption keys. If there were a pattern for creating encryption keys, then there would be a reversed pattern for creating decryption keys, which would be a big problem. A lot of work has gone into making random number generators for encryption systems, but banging on the keyboard is probably as random as you can get. At least for the moment.
8. After your key pair is generated, GPG will then print the following information to the screen. The basic gist is that GPG does the following:
- Creates a trust database called ~/.gnupg/trustdb.gpg. Within this database, GPG will store how well you trust public keys you receive from others.
- Marks your public key with the highest level of trust (ultimately), as it assumes that you can completely trust any key you personally generate.
- Reports that a public and secret (private) key are created and authenticated (signed) to you.
- Goes through the motions of how it assigned trust. Assigning trust is a bit beyond this tutorial, however you can learn more here and here.
- Prints information about the newly created public key including its fingerprint and subkey (to be discussed soon).
9. List the public keys in your key chain (in which you should only have one at this time, yours). Take special note of your key id (A7327C0E in this example) as you will need it later. You can always list your keys again in the future if needed.
10. Finally, go ahead and list the private keys in your key chain too. (in which you should also only have one).
Managing Your Keys
Now that you have a key pair, the next step is managing your keys properly. You will do this by sharing your public key with others and securely backing up your private key.
1. Create a directory in your home called keymat, so you have a place to work in for this tutorial.
2. Output your public key as an ASCII armored file (replacing email@example.com with the email address for your key).
What is ASCII armor?
ASCII armor is a term used with PGP for encoding binary data into text. Although this “armor” does not make your data any more secure against brute-force attacks, it does protect your data when sent over a series of communication paths.
For example, if you wanted to send your public key through email, mail servers and spam filters may not be able to process your key in binary format and as such, throw it out. With ASCII armor however, it is treated as normal text and can survive its fight through the internet.
PGP can encode any encrypted file with ASCII armor which keeps your transmission options open.
Learn more about ASCII armor here.
3. After a short wait, you should have a publickey.asc file in your keymat directory. Take a look at it.
4. There it is, your public key. You can now upload it to your website, give it to your friends, email it to your coworkers, print it out and post it on your front door. This is what people need to contact you, and only you, securely.
5. Next, send your public key to a key server, so that people interested in contacting you securely have an easy way to access it. For this, you will need your key’s id (replacing A7327C0E with the id for your key).
6. Good to go. Once your key is propagated to a series of PGP key servers, anyone can go to a key lookup website, type the key’s information (key id, owners name or email address) and get it. Two examples of key lookup sites are keyserver.pgp.com and pgp.mit.edu. Check them out and see if anyone you know has a public key available.
How do key servers work?
There are many PGP key servers throughout the world, however you only need to send your public key to one, the default for GPG being subkeys.pgp.net. After one key server receives your key, it should propagate it to many other key servers, some of which having handy key lookup websites.
Learn more about key servers here.
7. Once your key is propagated throughout a series of key servers, you can only have it disabled by generating and uploading a revocation certificate. This is very useful if your private key were to become compromised. There is a catch 22 however. If you want to revoke it because you forget your passphrase, you will need to generate a revocation certificate. But to generate a revocation certificate, you will need your passphrase. As such, a good practice is to pre-emptively generate a revocation certificate so you have it if this ever happens. Do this now (replacing A7327C0E with the id for your key).
8. GPG will ask the reason for generating a revocation certificate, as your key is not compromised, superseded and is still in use, just give no reason by typing “0” and pressing enter.
9. Next, give a description why you are making this revocation certificate. I would just be honest on this one.
10. GPG will then echo your reason for revocation and ask if you are sure. Type “y” and press enter. You will then be prompted for you passphrase. Type it in and press enter.
11. After a short wait, you should have a revokecert.asc file in your keymat directory. Take a look at it.
12. You now have your public key accessible to others and your revocation certificate on your computer. The next step is to make a backup of your private key. This is very important, as if you were loose your private key, you will be unable to retrieve any information encrypted by your public key. For security purposes, you will also symmetrically encrypt your private key backup with a passphrase and then output it as an ASCII armored file (replacing A7327C0E with the id for your key).
13. After entering your passphrase twice, you will now have a privatekey.pgp.asc file in your keymat directory. Take a look at it.
14. Good to go, your private key is backed up as an encrypted file, your public key is available to others and you have a contingency plan if you forget your passphrase. Now, you should save all three of these files (publickey.asc, privatekey.pgp.asc and revokecert.asc) in a safe place. What I do is have them burned onto a cd, printed on paper and stored in a safe within my house. After all three files are backed up, you should then destroy the private key backup and revocation certificate on your computer (I recommend the Mac OS X Secure Empty Trash feature).
Importing and Verifying Public Keys
Your public key is now available throughout the internet and people interested in sending you an encrypted message have easy access to it. The next step is to go through the motions of importing a public key and verifying its authenticity.
PGP also offers an easy way to verify public keys as every one also has a 40 character “fingerprint”. As a fingerprint is much shorter then an ASCII armored public key and has only 16 possible characters instead of 64, it is much easier to memorize and confirm in a voice conversation. Lets see how all this works.
1. Once you have downloaded a public key from a friend or co-worker, you can add it to your key chain by doing the following:
NOTE: Importing your own public key is not neccessary, however we will do so in this tutorial just so you can go through the motions and become familiar with the proccess.
2. Importing a file is one way to get a public key into your key chain, however there is an easier way. You already uploaded your key to a public key server so that others can easily get a hold of it. Now, try getting the key used in this tutorial by searching for it. For this tutorial, you will use the key’s unique id, however entering a name or email address will make the server perform a search and return any relevant results.
3. There it is. From here, you can type “1” and press enter. Also, keep note of the key’s id (A7327C0E) as you will need it very soon.
4. You now know how to import keys from files and key servers. Next, you will generate and use the key’s fingerprint to verify it with the owner through another channel. Try it by verifying the Mac GPG Tutorial key with its fingerprint.
How are PGP Fingerprints Created
Fingerprints are created by digesting your public key with SHA1 (Secure Hashing Algorithm 1), a digestion algorithm similar to the MD5 algorithm talked about earlier. Although a SHA1 digest of your public key cannot be anywhere near as unique as your public key, mathematics are on your side in terms of being unique to any fingerprint an imposer could create through PGP.
Learn more about SHA here.
5. GPG makes everything easy for you as it adds a space every four characters and a double space in the middle of the string. If the fingerprint for the key you have matches what your friend or co-worker say, then it should be legitimate. For the Mac GPG Tutorial key, match the fingerprint with what the example shows above and if it checks out, feel free to sign it.
6. GPG will then ask you if you are sure, type “y” and press enter. Then, verify your passphrase in order to sign this key. The reason why you need your passphrase is because your ~/.gnupg/trustdb file is also encrypted to ensure that malicious users do not assign levels of trust to keys for you. Enter your passphrase and press enter.
7. You now have a signed public key in your key chain. Signing keys you trust is always a good practice as anyone can import keys into your key chain, but only you can assign trust to them. Be very weary of using any public keys that you have not signed and always try to verify any new keys with their owners by using their fingerprint.
Encrypting and Decrypting Messages
Now that you are setup and familiar with GPG, we can now start using it to encrypt and decrypt messages. In order to keep this part of the tutorial simple, you will only be sharing information with yourself. Once you get the hang of it, you can then start sending encrypted messages to any other PGP user.
1. Make a new directory in your home called crypto, so you have a place to work in for this tutorial.
2. Make a new plain text file using your favorite text editor, type a message and save it in your crypto directory as plain.txt. I wrote the following:
3. Now, encrypt this file with your public key and sign it with your private key. This will facilitate that you are sending the file to yourself, and ensuring it came from yourself. You will also output the ciphered text with ASCII armor into cipher.asc. As you are using your private key to digitally sign this message, you will be asked for your passphrase. Just type it in and press enter (replacing firstname.lastname@example.org with the email address for your key).
DSA for authentication and Elgamel for encryption
When you generated your key pair, you noticed that you actually created two keys, one 1024 bit DSA key and one 2048 bit Elgamel sub key. As you are encrypting and signing your message, GPG will use your DSA private key to sign the message and your Elgamel public sub key to encrypt it. When you decrypt this message, the process is reversed by using your Elgamel private sub key for decryption and DSA public key for authentication. Currently, all PGP implementations which adhere to the OpenPGP standard (like GPG) use these two cryptographic algorithms.
4. After a short wait, you should now have a cipher.asc file in your crypto directory. Take a look.
5. Good to go, you now have an encrypted message that you can send secretly to, well, yourself. Lets decrypt it into a file called decrypted_plain.txt and see what we get back. You will need to type your passphrase for this too, as you are using your private key to decrypt cipher.asc.
6. GPG will now decrypt the file and also let you know that the signature is good. Now, take a look at your decrypted_plain.txt file and see if everything went well.
7. Fantastic, you just sent your first secure message to yourself. That’s all there is to it.
What else can I encrypt and decrypt?
You can use GPG for more then encrypting short messages. Binary files, source code, Word documents, pictures, video, anything can be encrypted to keep it from prying eyes. The format which you choose to output depends on how you wish to send the information. A good rule of thumb is to output encrypted plain text (which would include markup like HTML and source code) with ASCII armor and output encrypted binary as binary (by not using the -a option). However, if you wanted to send a picture to someone via email or some other messaging system (even printing it out and sending it via snail mail), then ASCII armor would be the best output to do it in.
Keep in mind, you can encrypt entire directories too. All you have to do is zip or tar the directory and encrypt the archive. This is especially handy if you want to backup sensitive documents for yourself, as you can encrypt the archive with your public key and store them anywhere (even GMail or Amazon S3) without worry.
You went through a lot information in this tutorial. However, you should now have a very good foundation on how cryptography works and some best practices on how to use it. The best way to incorporate cryptography into your life is to get into the habit of using it. As such, get your friends, family and coworkers familiar with PGP too. As time goes on, securing your information will be as second nature as running spell check before you send an email.