Peter van der Linden's Guide to Linux : A Lesson in Encryption, Part 1
September 20th, 2005 by Peter van der Linden in
Editor's Note: The following is an excerpt from chapter 11, "Keeping Your Data Private", of Peter van der Linden's Guide to Linux, published August 2005 by Prentice Hall, ISBN 0-13-187284-2.
One of the qualities that distinguishes Linux from lesser operating systems is its superior support for your privacy and security. That means more than avoiding buggy virus-propagating applications, such as Microsoft's Outlook and Internet Explorer. Savvy people and organizations quite rightly want to protect their data files and e-mail from snooping eyes. One way to do so is by keeping important files in coded form, a practice known as encryption.
Don't think encryption is only for secret agents or computer gurus with ponytails down to the waist and witty t-shirts from thinkgeek.com. Putting an ordinary file on a PC is like leaving a letter face up on a desk. It's in plain view for anyone who passes by. Encryption is practical for ordinary people who want to keep their data private when they put it on a PC. It effectively puts that letter into a strong sealed envelope. You can do it, and it's an important ability to have.
Keeping your data and email truly private is an increasingly significant skill in these times. Privacy through encryption apparently is important enough that governments as diverse as those of France, Britain and Iran want to deny it to their citizens. In the US, the First Amendment to the Constitution prohibits the government from denying freedom of speech to its citizens. Or at least, the Constitution makes it more probable that such denial eventually will be overturned in the courts. Freedom of speech has been interpreted by the courts to include freedom of dance, of song, of cinema, and it includes the freedom to write down whatever random numbers you like.
This tutorial on Linux encryption is offered in three parts. Part 1, here, describes how public key encryption works and where to get the GnuPG encryption software. Part 2 walks through some examples of file encryption/decryption, and Part 3 explains how to send and read encrypted e-mail.
The terms GnuPG and GPG refer to the same thing, the GNU Privacy Guard program. The executable file name is gpg, and the name GnuPG is used in some of the documentation. The name is a spoof on a similar program called Pretty Good Privacy or PGP. PGP used to be an open-source program but later versions went commercial, so the GNU community reimplemented a free version and named it accordingly.
GNU Privacy Guard is software that scrambles and unscrambles files or e-mail on command. The purpose of scrambling (encrypting) a file is to keep its contents private from those who do not have the key to unencrypt it.
If unauthorized persons somehow get hold of an encrypted file, they will not be able to make sense of the contents. The content of an encrypted file is changed fundamentally, not merely mixed up. It is extremely difficult to retrieve the original contents from the scrambled version unless you have the secret key. If the information is important enough, governments or rich organizations may be able to do it by exhaustive searching, using many powerful computers. The average consumer or company does not have the resources to break GnuPG encryption.
For reasons relating to mathematical definitions, we refer to encryption algorithms, encrypting and decrypting, whereas non-mathematicians would say codes, encoding, and decoding. To the ordinary user, these three pairs of terms correspond to each other. The meanings of "code" in source code and the code used to encrypt a file were originally similar in philosophy, but there's no useful overlap, so separate them in your mind.
For hundreds of years, conventional codes have relied on using a single key. The key is a secret number or phrase that is used in the transformation of plain text into the coded message. The recipient of the message uses the same key to reverse the procedure and recover the plain text. Figure 1 depicts the classic approach to cryptography: a single key that is used for both coding and decoding.
People have added very clever refinements, such as changing the letter substitution with every letter in the message; the German Enigma code machine did this. But the basic approach has a single secret key used by both coder and decoder. Managing shared secret keys is very expensive and a source of considerable insecurity. It requires you to send a secret message (the key) before you can send a secret message.
GNU Privacy Guard uses a different encryption technique called public key encryption (PKE), invented comparatively recently. Each user of Public Key Encryption has two keys. The two keys are long numbers--thousands of digits long--that are related mathematically and form a pair that work together. No two people ever should have the same numbers. Your two numbers have a mathematical relationship between them, but knowing one number doesn't provide any practical way to learn the second number.
One key is used to encrypt messages for a specific user, and (amazingly) this key is published to the world. Hence it is called a public key. When anyone wants to send Biff a secret message, say, he or she uses Biff's public key to encrypt it, and no one else can make sense of that encrypted message.
The other key of the matched pair is employed by the user to decrypt the messages that were encrypted by someone using his public key. This decrypting key has to be kept secret. Hence, it is called a private key.
Anyone who wants to send you an encrypted message looks up your public key on your Web page, or from a floppy disk you sent them or from a PKE server. The encryption program crunches the secret message with your public key to produce an encrypted message. This encrypted message is a string of random-looking text. It is not possible for just anyone to decrypt it, even when they know your public key.
The only practical way to decrypt a message encrypted with your public key is with your private key. The two numbers were generated as a matched pair to ensure that they would have this "one encodes, the other decodes" quality. The GnuPG program does some intensive processing to convert the encrypted message plus your private key back into the original text. You must keep your private key well guarded, as anyone who has your private key is able to decrypt any secret files intended only for you.
You might think it would be clearer if your public and private key were called a locking key and an unlocking key, respectively. If it helps, by all means think about them that way. There's a reason why those terms aren't used, which I'll explain in the last article in this series.
To summarize, a public key converts plain text into random-looking encoded text. The public key doesn't have to be kept secret, and usually isn't. Only the corresponding private key can unlock the meaning--neither your public key nor anybody else's public or private key can recover the original text. Your private key has to be kept secret. Figure 2 portrays public key cryptography.

Figure 2. A public key is used to encrypt a file. The corresponding private key is used to decrypt the file.
You use public key encryption whenever you send your credit card details to an on-line store in a browser. The browser encrypts your details using the server's public key before sending them across the Internet. This is part of the secure HTTP protocol, known as HTTPS. The corresponding private key never leaves the server, so no eavesdroppers can decrypt your credit card data.
The mathematical qualities that PKE relies on have a beautiful symmetry to them, and PKE rocked the worlds of computer science and encryption when it appeared in the 1970s. Government scientists in Britain's GCHQ eavesdropping and phone-tapping center later claimed that they had invented the technique some years earlier, but kept it secret! Indeed, they did uncover some of the theory, but it was clear that the spies of GCHQ had not grasped its practical significance.
Before PKE, secret communications relied on the sender and receiver having the same key and keeping it secret. This was a real weakness. The big advance of PKE is that you no longer have to send someone a secret message (key) before you can send them a secret message. With PKE, the recipient openly publishes something that allows anyone to send him a secret message.
On a Debian-based release, this command installs GnuPG:
apt-get install gnupg
On an RPM-based distro, such as Red Hat or Fedora, search for "gnupg" here. Or, you can download the source from here and build it yourself.
Check that you have installed GnuPG correctly by running the program with the --help option. In a shell, type this command:
gpg --help
You should see about 120 lines of not terrifically helpful help information flash by, ending in a line that reads:
Please report bugs to <gnupg-bugs@gnu.org>.
If the program doesn't start or doesn't produce that help information, there is a problem with your GnuPG installation that needs to be fixed before you can move forward.
You need a key pair of your own to be able to encrypt and decrypt. There are a number of commercial organizations that will sell you a key pair and provide varying assurances that your key really belongs to you and not to some imposter. They call the key pair plus the assurance a certificate, and it exists in the form of a computer file.
Most people encoding and decoding for personal use don't need that level of trustability by the outside world. They create their own key using the GnuPG utility. There is a way to boost confidence in personally created keys by getting your friends to vouch for them electronically.
The GnuPG program is in active development, and the team makes several new releases a year. This article was developed with GnuPG version 1.4.1. You might find that you are working with a later version of GnuPG, and that the output does not precisely match the prompts or output shown here. That's fine. Make the small adjustments to map between the text and the GnuPG program. The fundamentals don't change.
Linux encryption software is not as polished as it should be. That's a common limitation of software produced by volunteers. However, most of the hurdles lie in the one-time setup part, not in daily use. We'll walk through the one-time setup here.
In this section, you'll create a pair of keys, one to publish and one to keep highly secret. Here is the one-time setup to get your pair of keys for use with GnuPG.
Create the pair by running the GnuPG program, with the command line option to generate keys:
gpg --gen-key
GnuPG runs interactively and asks a few questions. It's fine to accept the default suggestions made by GnuPG. Notice that part of the output, "We need to generate a lot of random bytes", appears twice. This is because that portion of the code is executed twice, once for each key. The entire sequence looks like this:
$ gpg --gen-key gpg (GnuPG) 1.4.1; Copyright (C) 2005 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 = key expires in n days w = key expires in n weeks m = key expires in n months y = key expires in n years Key is valid for? (0) 0 Key expires at Thu 07 Apr 2005 05:11:17 PM PDT 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) " Real name: Peter van der Linden (working on Linux) Email address: peter@gmail.com Comment: working on Linux You selected this USER-ID: "Peter van der Linden (working on Linux) " Change (N)ame, (C)omment, (E)mail or (O)kay/(Q)uit? O You need a Passphrase to protect your secret key. Enter passphrase: My secret phrase entered here Repeat passphrase: My secret phrase entered here 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: /root/.gnupg/trustdb.gpg: trustdb created gpg: key 6C7C81B2 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/6C7C81B2 2005-04-03 [expires: never] Key fingerprint = C2A9 6818 3158 C13B 457A 1409 11ED 2943 6C7C 81B2 uid Peter van der Linden (working on Linux) / sub 2048g/68F3472B 2005-04-03 [expires: never]/
As you might surmise, GnuPG was created by programmers who are more skilled at cryptography than at human interface design. The program takes only a few seconds to run, and you get three things back from it. The first two things are a pair of large numbers, the public and private keys. They are created for you and stored in binary form in files in the directory ~/.gnupg . These files are given permissions that prevent other users from reading or writing them. Burn a copy of that directory to CD now, and lock the CD safely away. This allows you to restore your key regardless of future disk or system failures. The third thing you get is a "fingerprint", also called a key ID, which is a 40-byte-long checksum of your newly created public key. You can use the fingerprint or even the last eight digits of it--6C7C 81B2 here--when talking to GnuPG as a shortcut for your public key, and it will retrieve and use the associated key for you.
The e-mail address and the passphrase you gave are associated with the keys and the fingerprint, and these also are used to identify and authenticate you. They call it a passphrase rather than a password to emphasize that it needs to be lengthy and secure. It's pointless to use GnuPG with a weak passphrase, something like "dog". If you can't remember a passphrase, pick any short sentence from any book and outline it with yellow highlighter so you always can find it again.
You must give the passphrase when you need to access the secret key to decrypt something. If you forget your passphrase, practically speaking, the files you encrypted never can be decrypted.
That completes the one-time setup. In Part 2, I describe how to use the GnuPG program to encrypt and decrypt files.
Subscribe now!
Breaking News
| Charter Trades Privacy for Pocketbook | 14 hours 19 min ago |
| SSL Glitch Unlocks Debian, Ubuntu, & Others | 1 day 13 hours ago |
| MySpace Cashes in Spam to the Tune of $234 Million | 1 day 15 hours ago |
| Google Shoos the Trustbusters Away | 2 days 12 hours ago |
Featured Video
Linux Journal Gadget Guy, Shawn Powers, takes us through installing Ubuntu on a machine running Windows with the Wubi installer.

Delicious
Digg
Reddit
Newsvine
Technorati






They call the key pair plus
On August 5th, 2006 landersan (not verified) says:
They call the key pair plus the assurance a certificate, and it exists in the form of a computer file.
Kolmerton
On August 5th, 2006 Kolmerton (not verified) says:
the universe include what you know and what you donot know . for example you only see this sky we all see but it is known that there are seven skyes created by allah.
If the program doesn't start
On April 6th, 2006 bloger (not verified) says:
If the program doesn't start or doesn't produce that help information, there is a problem with your GnuPG installation that needs to be fixed before you can move forward.
| .
On March 18th, 2006 Johnny (not verified) says:
http://maxpages.com/erin/Photos/
http://u-blog.net/Leire/
You must give the passphrase when you need to access the secret key to decrypt something. If you forget your passphrase, practically speaking, the files you encrypted never can be decrypted.
Correct
On March 18th, 2006 Tom (not verified) says:
Correct
Bi Curious shure
Key Expiration
On September 28th, 2005 Randy Kramer (not verified) says:
What happens when a key expires--it will no longer work to encrypt/decrypt documents, or other people will no longer trust it, both, or something else?
GPG as stream cypher?
On September 22nd, 2005 Anonymous (not verified) says:
Why would you want to use gpg as a stream cypher only, isn't its real strenght when used with a public keyserver to verify that a message also is from a particular sender? Is there a guide to how that's handled, as it would also deal with registring/revoking keys?
Crass ignorance or pro-debian astro-turfing?
On September 22nd, 2005 JFM (not verified) says:
RedHat, Fedora, Mandrake, Suse and any other rpm based distribution I am aware of has gnupg included in the distro and it is part of the default installation. Having hardware working out of the box and "obvious" software installed
by default that is the way in any decent distro and from the article I infer it is not the case in Debian. :-)
BTW: Was the Linux Journal editor sleeping? Catching such glaring errors in an article is supposedly part of his job!!!
Be patient if you see a messa
On September 21st, 2005 Anonymous (not verified) says:
Be patient if you see a message like this:-
Not enough random bytes available. Please do some other work to give
the OS a chance to collect more entropy! (Need 183 more bytes)
Give the OS a chance to get the random bytes it needs, and it will eventually complete. Also, do what it tells you and make the server do some work to reduce your waiting time.
If you are using Fedora 3 (as I was), you will need to create the ~/.gnupg directory otherwise the gpg --gen-key command will fail.
You're not supposed to do that.
On September 21st, 2005 Anonymous (not verified) says:
According to the GPG manual, gpg should not be used to generate keys on remote machines, even over ssh.
The practical reason why it doesn't work is that the remote machine, usually locked away in a server room somewhere with nobody typing on it, quickly runs out of entropy.
--linuxrocks123
If I'm not here, it means I've gone out to find myself. If I get back before I return, please keep me here.
Typical US imperialist attitude
On September 21st, 2005 Malcolm (not verified) says:
Typical US imperialist attitude to the world to make snide comments about other nations without backing them up with facts.
The US my have the first amendment but it also has software patents!
What are you talking about?
On September 21st, 2005 Bison (not verified) says:
What are you talking about?
he's talking about para 2 of this article
On September 22nd, 2005 Anonymous (not verified) says:
The snide comments about France and Britain. Never mind that "the land of the free" is governed by tyranical despot dictatorial maniac intent on enforcing his draconian laws on the people (to "make America safe"), or that he wasn't actually elected in the first place, but got there by default and general US apathy to voting.
All of this and the typcial American can't resist slipping in some jibe about other countries political systems and social liberties. It's the hight of hypocracy.
> The snide comments about Fr
On September 22nd, 2005 Bison (not verified) says:
> The snide comments about France and Britain.
The only comment I saw regarding France and Britain was this one:
"Privacy through encryption apparently is important enough that governments as diverse as those of France, Britain and Iran want to deny it to their citizens."
And... that's more or less true, isn't it?
In any case, I don't see how your caustic comments about George W. Bush or American voters are in any way related to data encryption in Europe. Perhaps it would be better to maintain a more level-headed approach while posting comments, lest people associate your views with your behavior in an unfavorable way. Just my 2 cents.
Laws have changed long ago...
On September 27th, 2005 french frog (not verified) says:
>The only comment I saw regarding France and Britain was this one:
>"Privacy through encryption apparently is important enough that
>governments as diverse as those of France, Britain and Iran want to
>deny it to their citizens."
>And... that's more or less true, isn't it?
Not true in France anymore...
Keysize upto 128bits are free of use (AES works fine with them)
Encryption is allowed for larger key size, you only have to make a decraration prior its use if it's a new tool...
Using authorized tools is not a problem whatever the key size (PGP)
American Imperialists blind to their own actions
On September 25th, 2005 Anonymous (not verified) says:
what? Like branding me a "terrorist"? Sure, go lock me up in your POW camps without even charging me, and justify your action under the "patriot act".
I'm not even European or British, so don't think this is somehow concerned with antiquated sour grapes. The notion is laughable, it happened over 200 years ago, get over yourselves. At least my country escaped from British Imperialism by vote, without a war. Though I conceed that now our current elected leader is little more than an American hand-puppet, but is up to our people to do something about it, as it should be up to Americans to get rid of their ineffective, tyranical leader.
Of course my comments are not on the topic of encryption in Europe. They are on the topic of Imperialist America. Obviously this went over your head, and trying to explain this repugnant attitude to an American Imperialist would be like trying to explain what water is to a fish, so I might as well stop now. Go drown in your own problems and be happy that America is so much better than the rest of the world.
American Leader
On September 28th, 2005 Chris Nystrom (not verified) says:
> it should be up to Americans to get rid of their ineffective,
> tyranical leader.
Hey! We like out ineffective, tyranical leader!
> Sure, go lock me up in your
On September 26th, 2005 Anonymous (not verified) says:
> Sure, go lock me up in your POW camps without even charging me, and justify your action under the "patriot act".
What POW camps are you talking about, and what makes you think that I want to lock you up in one?
You're entire comment is hysterical (in the literal sense).
>"Privacy through encryption
On September 23rd, 2005 Anonymous (not verified) says:
>"Privacy through encryption apparently is important enough that
>governments as diverse as those of France, Britain and Iran want to
>deny it to their citizens."
>
>And... that's more or less true, isn't it?
No, it isn't, at least in France. 128-bit symmetric keys are legal for everyone. Beyond that, you need an authorization from the SSI.
Europeans have ever been in l
On September 22nd, 2005 Anonymous (not verified) says:
Europeans have ever been in love with dictators and they can't forgive America of having rid Europe of them. But nowhere is the resentment so strong as in France and Germany. Now they were perfectly happy with Iraquis being gassed or thrown in plastic shredders just as long as Saddam guaranteed them lucrative contracts and they could sell outrageouly overcharged goods in teh oil for food scam. And they demonstrated for Irquis getting more of the same: the hunger, the savage tortures, the rapes, the mass killings. Pity they don't get a dose of the medecine they wanted to force unto others. Now they romantize about that "resistance" who kills chilkdren and construction workers as a way of forgetting that their grand-parents collaborated during WWII.
And let's not forget that in France no politician has lost a single vote for the machine gunning of a pacific demonstration in Ivory Coast or for helping the genociders in Rwanda. Not a single vote lost. For them they are just niggers.
>And let's not forget that in
On September 23rd, 2005 Anonymous (not verified) says:
>And let's not forget that in France no politician has lost a single
>vote for the machine gunning of a pacific demonstration in Ivory
>Coast or for helping the genociders in Rwanda. Not a single vote
>lost. For them they are just niggers.
Hey dude! How do you spell 'New Orleans'?
As a corrupt and incompetent
On September 24th, 2005 Anonymous (not verified) says:
As a corrupt and incompetent major who let six hundred busses unused, did nothing to provide the Dome with food, water and security and whose police, the police HE recruited vanished when needed. Ah, and he is Black as was most of his police.
Deep in bush's root.
On September 22nd, 2005 Anonymous (not verified) says:
Hey, man... whatch out! Your brain isn't that smooth any more. Better grab a bush (like the one they advertise on FOX) and scrub it clean. Nice and SMOOTH.
While you only collect your i
On September 22nd, 2005 Anonymous (not verified) says:
While you only collect your information from honest sources like the Pravda or Dan Rather. You know the guy who discovered an MS word document written in 197x. And thanks to such honest, reliable and truthful sources you can emit educated opinions about the world.
Nietzsche told that being able to drink dirty water when this is the water of truth was the first-step to become a super-human. He had it all wrong. In fact that is the first step to become merely a man while not doing it is the first step to become a larva. And now go watch Dan Rather.
The debian way
On September 20th, 2005 polarizers 2cent (not verified) says:
Nice article. And good choice to use debian.
Simple installation by just one line. That's
the debian way.
"The Debian Way" - another perspective
On September 21st, 2005 tonymac (not verified) says:
Isn't it also the Microsoft Way ( "1, Microsoft Way, Redmond , Wash. being the home address (-: ). Anyway the thing is that "1-click" or "1-line" install means also that you have to hack like crazy to find out what file went where and what it does - don't you think? - as in handing over control to somebody else.
The microsoft way
On September 22nd, 2005 Anonymous (not verified) says:
No. The "microsoft way" is to "Click Start, Run Program, then type E:\installshield". Or sometimes it's not E:. And it's not always Installshield. Or you could try Start, Settings>Control Panel and find the Add/Remove Programs icon, then wait whil its "building an application database" find the Add New programs button/tab (depending on which Winoze it it)... oh just forget it!
Both debian and RPM systems can tell you what package a file belongs to. In rpm, its rpm -qf thefile. I'm not sure what the command is for debian, but it's just as simple. And no hacking: I don't know the command but I do know I can type "info dpkg" on a debian box and have it described to me. How do I find it out in Windose?
The debian way
On September 21st, 2005 Pro Linux Guy (not verified) says:
I rather Mandriva..., much easier than Debian
One Mandriva Way
On September 24th, 2005 Zarluk (not verified) says:
Indeed... much more easier, almost like Windows -- bugs all over your beautiful desktop ;-)
Really
On September 22nd, 2005 Jarbelix (not verified) says:
The Mandriva is really much more easy of that the Debian, as much for Desktop as serving.
Nice article. And bad choice
On September 20th, 2005 Noah (not verified) says:
Nice article. And bad choice to not use gentoo.
Simple installation by just one line. That's
the Gentoo way.
:)
Nice Article
On September 20th, 2005 Farley (not verified) says:
Don't forget that Gentoo sucks. That's the Gentoo way.
Slack you all !
On September 26th, 2005 Anonymous (not verified) says:
Once you go SLACK you never go back ...
Ubuntu is wonderful
On September 29th, 2005 Anonymous (not verified) says:
See the light...
Come home to the Ubuntu way!
(And it has apt inherited from Debian!)
;)
lol good fun, thanks guys
FREEBSDmake install
On March 3rd, 2006 Anonymous (not verified) says:
FREEBSD
make install clean
done