Introduction
A long time ago, in a galaxy far, far away, I remember when I first learned of public key encryption. I also remember it took me longer than I’d like to admit for exactly how it works to click.
Encryption tends to be one of those techs that scares people. Many assume it’s a technology accessible only to genius level mathematicians, spy agencies, or crypto nerd hackers in dark basements. Yep. Hollywood is mostly to blame for this misconception.
Public key encryption is in fact frequently used by many outside the realm of fantasy. Regular privacy conscious individuals, journalist communicating with sources and companies protecting sensitive data or trade secrets all have a vested interest in public key encryption.
This post is my attempt to help beginners grasp the very basics of public key encryption and its use.
(Most of the functions below are carried out using GPG via the (Linux) terminal or Windows command prompt.)
What is Public Key Encryption?
Keys are basically unique digital files, which, when used in conjunction with encryption software allows you to encrypt and decrypt data.
This is what your typical public key looks like:
Public key encryption uses two separate but related keys known as a key pair to encrypt and decrypt data. The two keys are known as a public and private key. The public key, as it’s named is - public, it’s the key that you make available to anyone/everyone. While the private key as suggested is the key that you keep private and reveal to no one.
Your public key is the part that you and everyone else can use to encrypt data (decrypt-able only by your private key).
Encrypting & Decrypting Data
Here’s an example of how public key encryption works.
Let’s say Edd Snowballs wants to send you a file that he wants only you to be able to read/open. You can email Edd your public key without any security fears since it’s only use is for the encryption of data. Once Edd encrypts his file using your public key, he can email it you safe in the knowledge that only your private key can decrypt the file and view its contents.
Once you receive Edds encrypted file. You can now go ahead and decrypt it using your private key.
Data encrypted via your public key can only be decrypted using your private key. As you’ve no doubt guessed, keeping private keys safe is paramount to the integrity of secure communications between parties using public key encryption. If you were to lose your private key, anything that has been encrypted via your public key will remain locked, comparable to locking something in a vault and losing the physical key.
Validating your Identity
Your private key can also be used to validate your identity. This is called ‘digital signing’. You can ‘sign’ a message or file prior to sending it using your private key;
The receiver can then use your public key to verify that the file was signed by your private key.
This prevents third-parties from impersonating you. Digital signing also has the added benefit of verifying that the integrity of the sent data has not been compromised, damaged, or tampered with in anyway.
This again highlights how important a role your private key plays in identifying you as well as the encryption/decryption process. Any party gaining access to your private key would not only be able to decrypt data intended for you, worse yet, they’d be able to impersonate you.
In short. KEEP YOUR PRIVATE KEY SAFE, and never, under (almost) any circumstances give it to anyone.
GPG
GnuPG (or GPG) is an open source alternative to PGP (Pretty Good Privacy).
GPG is installed by default on most Linux distributions and Windows users can access GPG via GPG4win (GNU Privacy Guard for Windows) for free.
You can check whether GPG is install on your system by opening a terminal on Linux (or command prompt in Windows) and typing gpg --version
.
The above shows that GnuPG version 1.4.12 is installed.
1.Generating a Key Pair
The following command generates your key pair:
Choose the following options when prompted:
- Please select what kind of key you want: (1) RSA and RSA (default)
- What keysize do you want? 4096
- Key is valid for? 0 (does not expire)
- Is this correct? (y/N) y
- Real name: your full name here
- Email address: your_email@address.com
- Comment: Optional comment that will be visible in your signature
- Change (N)ame, (C)omment, (E)mail or (O)kay/(Q)uit? O
- Enter passphrase: Enter a secure passphrase here (upper & lower case, digits, symbols)
After entering your passphrase the system will generate your keys using entropy. Entropy is basically a term used to describe the randomness involved in generating your outputted keys. This process utilizes random activity on your system such as keyboard, mouse and hard disk activity. Therefore using these as much as possible during key generation will reduce the time required to create your key pair.
Example output:
That’s it. You’ve now created a key pair. You should now immediately generate a revocation certificate.
2.Generating a Revocation Certificate
So we’ve established the importance of keeping your private key safe? Well what happens if you suffer the misfortune of losing your private key or it’s somehow compromised? How can you inform people that your public key is no longer valid, and to cease any further use of it? This is where Revocation Certificates come in.
Revocation certificates allow you to revoke the validity of your key pair. This makes revocation certificates just as important as your private key, and they should always be generate at the same time you create your key pair.
Type the following to generate your revocation certificate:
You’ll be prompted to enter a reason for generating the revocation certificate. Since we’re creating it ahead of time, any option you select here is fine. You’ll also be prompted to enter your passphrase, and a optional comment of some kind. After confirming these choices, your revocation certificate will be generated to the file my_rev_cert.asc
. Copy and store it in a secure location.
Example output:
If you ever need to revoke your key pair, you can do so by importing the revocation certificate:
Your key is now revoked.
You can also submit your revoked key to a keyserver to publicise it’s revocation status (section 4 explains keyservers):
Now anyone running a search for this key will see
*** KEY REVOKED ***
on the first line of the details and see that the key is no longer valid.
3.Exporting Your Key Pair
Now that you’ve generated your key pair and revocation certificate, you can export them to files for backup and safe keeping.
The above commands will export your key pair to two separate files, one your private key, and the other your public key. Keep your private key along with the revocation certificate you generated earlier in a safe place - Not on your laptop or workstation. It’s also good practice to store your private key and revocation certificate in separate locations.
4.Importing Public Keys
Let’s not forget that the whole purpose of public key encryption is to provide a means of secure communication between parties. Importing the public keys of others allows us to get started with this.
After downloading someone’s public key file, you import it using the following command.
Many people make their public keys available via KeyServers. These are essentially online databases of public keys. You can import public keys from keyservers directly via the command line.
The follow command will search for my public key on the MIT public keyserver:
The search result will give you the option of choosing which key you wish to import.
After you’ve imported someone’s public key, the next step is to verify and sign it.
5.Key Verification & Signing
Once you’ve imported a public key, signing it tells GPG that you’ve verified the key as being associated with the person it identifies, and that you trust they are who they claim to be. The best way to verify someone’s public key is to meet with them in person (although this may not always be feasible, practical, or even safe!). If meeting in person is not an option, the next best thing is to find an alternative means by which you can guarantee you’re communicating with the public keys rightful owner. You can then verify the keys ‘fingerprint’ with them.
So what’s a public key ‘fingerprint’? The fingerprint is a unique hash associated with, and generated from the public key itself. You can view a public keys fingerprint via the --fingerprint
option.
So having obtained a persons public key. You can contact them (say via phone if not in person) and verify that the fingerprint they give you matches the one you have, hence they are the rightful key owner.
N.B. When verifying public key fingerprints, be sure to verify the full fingerprint and not just the 32bit version (last 8 characters). See here https://evil32.com for why.
Ok. So you’ve imported someone’s public key and successfully verified it belongs to them. Now it’s time to sign their public key.
Use --sign-key
to sign someone’s public key:
That’s it. GPG now sees this public key as verified and trusted by you.
So after importing, verifying and signing someone’s public key. You can now export the signed key to file:
And/or send it to a keyserver:
A Note about Key Signing
Key signing and having your own public key signed by others contributes to what’s known as a Web of Trust (WoT). Signing the public key of someone you trust, allows others who trust you, to also trust the person who’s key you’ve signed, since others will know that (hopefully) you would not have signed it without carefully verifying their identity.
This works both ways. Someone who’s signed your public key is effectively showing others that they trust and have verified it belongs to you, thus making it more likely for those who trust that individual to also trust you (and your public key).
The above illustrates Edds wish to communicate with Joe. But, Edd has no means of meeting or contacting Joe to confirm that his public key actually belongs to him. Edd has however met with, and verified the identity of Laura who has signed Joe’s public key after meeting him in person and confirming his identity. Edd can now place a considerably higher level of trust in Joe’s public key than before, since he’s confident Laura would not have signed it without vetting Joe’s identity. Thus, Edd can now communicate with Joe via his trust in Laura’s signing policy.
Behaviour and policies such as this are the foundation upon which a Web of Trust is built. They enable you to verify, sign public keys, and communicate with those who you’re not able to personally verify.
Here’s an example of what a signed public key entry looks like on https://pgpkeys.mit.edu:
As you can see, Laura Poitras’ public key has been signed by three people. Arthur Klein, Bryan Belt and Prince Serna. Meaning all three have verified and trust that this public key indeed belongs to Laura Poitras.
You may have figured out that signing someone’s public key without verifying their identity is not only detrimental to others, but also has a negative impact on your own trust level. Since others may refuse to be associated with you (or sign your public key) due to your disregard for a proper signing policy.
6.Encrypting & Decrypting Data using GPG
Encrypting Files & Documents
The `--encrypt` option allows you to encrypt files and documents for intended recipients using their public key.Using Edd Snowballs’ public key you could encrypt a file before sending it to him:
You’ll now have an encrypted version of the file named file.zip.gpg
which can only be decrypted by Edd. The -r
recipient option uses Edds email address to identify his public key as the one to use when encrypting the file. You can also use the key ID (last 8 characters of the key fingerprint) to select a public key as opposed to the email address.
It’s possible to add multiple recipients when encrypting. This allows you to encrypt a file that more than one person (including yourself) can open.
Edd, Colin and Joe Bloggs would all be able to decrypt the file file.zip.gpg
generated by the above command.
Encrypting messages is very similar.
The msg.txt
text file above is encrypted using Joe Bloggs’ public key and signed (verified as coming from you) using your private key. The --armor
option forces the encrypted file output to a text-based (ASCII) format which you can then cut-and-paste into an email to Joe.
Decrypting Files & Documents
Decrypting files and messages you receive is as simple as:
The above commands will prompt you to enter your passphrase before decrypting the file or message.
Conclusion
This post only scratches the surface of GPG’s capabilities. There’s a wealth of advanced options not mentioned in this post. But hopefully if you’ve reached this far, you’ve at least gained a working knowledge of how public key encryption works, and how to use it (via GnuPG/GPG).
Spot any mistakes or have any suggestions on how this article can be improved? Please do leave a comment or drop me an email.
My next post will focus on some of the more advanced features of GPG.
Article originally posted on infinitysgame.com (my old domain). This repost is the unaltered original content except for formatting