GnuPG/zh-cn

这篇指南将教给读者GnuPG——一个用于安全通讯的工具——的基本用法.

读者能从本指南中得到什么
本指南已经假定读者对公钥加密法、加密和数字签名都比较熟悉. 如果不是这样，请跳转到official GnuPG handbook, ，具体是第二章，然后回到这篇文章.

读者将学习如何安装GnuPG，创建一个密钥对，向密钥环添加密钥，并将密钥提交给密钥服务器. 读者还将学习如何签收，加密，验证和解密发送和接收的邮件和文件

其他软件
最基本的，你需要. 现在很多应用程序都在某种程度上支持gpg，所以在你的USE变量里加上“crypt”应该是个不错的主意. 如果你想使用支持gnupg的邮件客户端，你可以选择使用pine（ ）、mutt（ ）、Mozilla Thunderbird（ ）、evolution（一个类似于微软Outlook的GNOME软件），或者KDE自己的Kmail.

创建密钥
只需运行 就可以创建自己的密钥了. 它在第一次运行时会创建一些文件夹；再次运行可以创建密钥：

此时，你可以选择你想使用的密钥的类型. 大多数用户会选择默认的DSA和ElGamal类型. 接下来就是密钥的大小；需要注意，虽然密钥越大越好，但是可不要对DSA/ElGamal使用大于2048的值. 对于普通的电子邮件来说，2048已经足够了.

Gentoo developers (especially those working on ebuilds) are encouraged to use 4096-bit keys.

在指定密钥大小之后，需要指定的是密钥的有效期. 在这里，有效期却是越小越好，但是大多数用户会选择一个永远不会过期的密钥，或者指定有效期为2、3年.

Choosing key size

现在，你需要键入一些你自己的个人信息. 如果你想将自己的公钥送给其他人，那么在这里必须使用真实的电子邮件地址.

Entering user information

在这儿，请键入你的密码两次. 我推荐你使用一个比较复杂的密码. 因为，如果有人得到你的私钥并破解了你的密码，他们就可以发送冒充你的名义签名的消息，从而欺骗所有人相信这个邮件是你发送的.

接下来，GnuPG才会产生你的密钥. 动一下鼠标或者在后台播放一个mp3将有助于加速这个过程，因为这些动作都会帮助产生随机数据.

生成吊销证书
每当你创建了密钥，你必须同时创建这个密钥的吊销证书. 因为，一旦你的密钥发生不测（例如有人得到了你的密钥和密码），只有你提前这样做了，你才有能力吊销你的密钥使它无效.

使用 命令可以列出公钥环中的所有公钥. 你可以用它来查看你密钥的ID，然后生成该密钥的吊销证书. 现在，推荐你将.gnupg目录和吊销证书（ASCII形式的——）拷贝到比较安全的媒体（两张软盘或者一张CD-R，并且，请将其保存在比较安全的地方）当中. 请务必记住，将来你可以使用来吊销密钥使密钥失效.

导出密钥
输入 来导出你的密钥. 任何时候你都可以使用密钥ID或者其他可以唯一确定该密钥的东西（这里我们使用了电子邮件地址）作为参数. John现在得到了. 他可以将这个文件发送给朋友，或者贴到他自己的网页上去，以便人们可以安全的同他通讯.

导入密钥
为了将文件添加到你的公钥环中去，你必须先导入它，并且验证这个密钥的指纹才行. 在验证指纹之后，你还需要将它设为有效.

现在我们要将Luis Pinto（我的一位朋友）的公钥加入到我的公钥环之中去. 在给他打了个电话并且问到了他这个密钥的指纹之后，我仔细对照了一下该密钥的指纹和 命令的输出结果. 在确认这个密钥是真实的之后，我将其添加到我的公钥环之中. 这个密钥比较特殊，它将在2003年12月1日过期，所以我被问到是否要将我的签名设置为在同一天失效.

向密钥服务器发送密钥
现在你已经拥有了自己的密钥，要是能把它发送到遍布全球的密钥服务器上去就好了. 全世界有很多密钥服务器，大多数服务器之间会相互交换密钥. 这里我们试着将John Doe的密钥发送到subkeys.pgp.net服务器. 这样会用到HTTP协议，所以如果你在进行HTTP通讯的时候需要使用代理的话，别忘了设置它（ ）. 用来发送密钥的命令是 ，其中， 是密钥ID. 如果你并不需要HTTP代理，请去掉“--keyserver-options honor-http-proxy”选项.

Where 75447B14 is the ID of the key you would like to send. If a HTTP proxy is not needed then the  option can be removed.

你也可以将你签过名的其他人的密钥发送到密钥服务器上去. 比如，我们可以将Luis Pinto的密钥送上去. 这样的话，那些信任你的密钥的人可以使用你发送上去的签名来同样得信任Luis的密钥.

从密钥服务器取得密钥
现在，我们举例来查找一下Gustavo Felisberto的密钥，并且将这个密钥添加到John Doe的公钥环上去（趁你还没注意到本指南的作者就是Gustavo Felisberto，:)）.

From the server response it is possible to see few keys have been submitted to the key server, however only  is used. Now Larry the cow can get the key and sign it if he trusts it.

什么是GPG代理？
Sometimes working with certain applications requires the frequent use of a GPG key, which means that a passphrase must be frequently entered. In the past many applications supported a passphrase caching mechanism. This would make life easier for users because passphrases were automatically entered. However, this disallowed sharing this cache across programs (how secure would that be?) and forced applications to reinvent the wheel over and over again.

A GPG agent is a separate application that GPG uses to cache the passphrase in a standard and secure way. It allows applications to use GPG concurrently: if the passphrase is entered while working in one application, the other application can work with GPG without needing to unlock the key again — if the GPG Agent is configured to allow this, of course.

Gentoo provides a few GPG agent applications. The package contains what could be considered the reference one, and will be the primary choice used in this article.

Configuring gpg-agent and pinentry
GnuPG includes. Pinentry is a helper application that  uses to request the passphrase in a graphical window. It comes in many flavors including gtk2 and 3, qt5, tty and curses. It is recommended to always build the ncurses version. Set USE flags accordingly.

If was installed with more than one popup window type, it is possible to choose between the windows with the  command:

Eselect will change the symlink of to the appropriate pinentry helper. Now create a file called and add the following lines to define the default timeout of the passphrase (e.g. 30 minutes) and the application to be called when the passphrase needs to be retrieved (e.g. the GTK+ version of Pinentry).

Now configure GnuPG to use an agent when appropriate. Edit and add the following line:

Now the system is almost ready to use the GPG agent.

Changing pinentry for SSH logins
If you use the gpg-agent over SSH, a graphical pinentry password prompt will not come up in the login shell. This causes all operations that require a password to fail. If you add the following snipped in your, the gpg-agent uses a curses password prompt in your current SSH shell instead. The snipped does not affect the pinentry settings when using local shells.

Automatically starting the GPG agent
If KDE is used as the desktop environment, edit (system-wide, for KDE4 ) or  (local user,  in KDE4). Add the following command to the appropriate file to have KDE automatically start the GPG Agent:

Additionally, uncomment the following lines in (system-wide,  in KDE4) or add it to  (local user,  in KDE4):

When using a desktop environment other than KDE, put the above lines in if  is used to start X.org or  if XDM, GDM, KDM, etc. are used.

Encrypting and signing
Let's say that Larry has a file he wishes to send Luis. Larry can encrypt it, sign it, or encrypt and sign it. Encrypting means that only Luis will be able to open it. The signature tells Luis that it was really Larry who created the file.

To encrypt:

To sign:

To encrypt and sign:

This will create binary files. To create ASCII files, just add the  option to the beginning of the command.

Decrypting and verifying signatures
Suppose that Larry has received an encrypted file. The command used to decrypt it is. This will decrypt the document and verify the signature (if there is one).

Encrypting and decrypting without keys
It is possible to encrypt files using passwords instead of keys. The password itself will function as the key — it will be used as a symmetric cipher. The file can be encrypted using ; decrypting uses the same command mentioned above.

GnuPG will ask for a passphrase and a passphrase verification.

Advanced features
There are some nice advanced features in GnuPG. To find them, open the file.

Uncomment the two lines above. With this modification, any time GnuPG needs to check a signature and does not find the public key on the local keyring it will contact the key server at keys.gnupg.net in an attempt to fetch the public key from the server.

Another nice command is. This will contact the key server defined in the configuration file and refresh the public keys in the local keyring from there. It is capable of searching for revoked keys, new IDs, and new signatures on keys. It is a wise idea to run this command once or twice a month; if a user revokes their key this can provide a notification the key can no longer be trusted.

About email signatures
95% of the time GnuPG is used with email by signing/encrypting outgoing messages or reading signed/encrypted messages.

There are two ways two sign/encrypt a email with GnuPG, the old way and the new way. In the old way messages would appear in plain text, with no possible formatting and attached files would be unsigned/unencrypted. Here is an example of a message signed the old way:

Messages this way are not good in today's world, where there are nice GUIs and email readers that understand HTML.

To solve this an addition to the MIME (Multipurpose Internet Mail Extensions) was created. This adds a field to the email that tells the mail reader that the full content of the message is signed and/or encrypted. The problem with this is that not all mail readers support such features. Some even mess up the content (Microsoft's Outlook is famous for not working with this).

Kgpg
Kgpg is a wonderful GUI for GnuPG. The main screen provides an area to paste text to sign or encrypt. The reverse is also true: ASCII armored text to be decrypted can also be entered.

Seahorse
Seahorse aims to be a GnuPG GUI interface for the GNOME desktop. The software has been evolving fast, but it still lacks many important features that can be found in Kgpg or the command line version.

KMail
If the  USE flag is set, KMail will be compiled with gpg support, and will be able to encrypt and decrypt inline PGP mails automatically as well as encrypting OpenPGP/MIME mails. To decrypt OpenPGP/MIME mails (which most users want) a GPG agent must be running.

To verify if KMail is properly configured navigate to. A GpgME-based backend should be listed and the OpenPGP checkbox should be checked. If it is listed but grayed out, click on. If the GpgME-based backend remains grayed out, KMail is not working properly.

When unable to get KMail to behave, see the official KMail PGP page for more information.

Claws-Mail
This mail client is very fast with big mailboxes, has all the nice features one wants in mail readers and works well with GPG. The only problem is that it does not work with the old PGP signatures, so when receiving those kind of mails the signatures must be hand-checked.

To use a GPG key with Claws-Mail navigate to. Once there choose which key to use, most users should go with the default key.

Thunderbird
Thunderbird users can use the crypt USE flag to enable Enable encryption support with enigmail.

Credits
John Michael Ashley's GnuPG Handbook is a very good book for beginners.

Swift (Sven Vermeulen) for pushing me to re-write this.

Everyone in the team; you guys rock.

Tiago Serra for getting me back on the privacy track.

External resources

 * https://www.void.gr/kargig/blog/2013/12/02/creating-a-new-gpg-key-with-subkeys/ - An article explaining the creation of subkeys.