GnuPG全称GNU PrivacyGuard,它是基于公钥/私钥体系的加密工具PGP(Pretty Good Privacy)的开源版本它遵循OpenPGP标准。你可以免费使用,自由传播,并可以获得它的源代码。可以用于对Email、文件及其他数据的加密与验证,确保通信数据的保密性、完整性和真实性。
如何获得
可以在其官方网站获得源代码和编译好的程序,当前版本是1.4.6。
如果在Windows下使用,则安装后需要把安装目录添加进PATH环境变量以便在命令行下使用。GnuPG的kering(密钥环)默认保存在 C:/Documents and Settings/你的用户名/ApplicationData/gnupg目录下的话,要保存在其他地方,就得设置GNUPGHOME环境变量。具体可以看其安装目录下附带的文档。
PGP原理及规则
在介绍GPG前,先让我们看看PGP的基本原理及应用规则。
PGP使用双密匙来加密数据。每个使用PGP加密技术的人都要创建一对密匙,一个叫做公匙,另一个叫做私匙。公匙可被广泛传播,你可以贴在自己的网页上或发到专门的公钥管理网站上等等。私匙属于个人信息,绝不应该泄漏给其他人。
公匙和私匙相互作用对数据进行加密及解密。被公匙加密的数据只能被私匙解密,被私匙加密的数据也只能被一个公匙解密。这样就可以实现双重认证。
例如,A要给B发信息:
* A不想信息被其他人看见,怎么办?
首先A必须通过某些途径得到B的公钥。在发送信息前,使用B的公匙对信息进行加密。这样只有B用对应的私匙才能解密收到的信息。所以就保证了信息传输的安全,这就是PGP所谓的加密(encryption)与解密(decryption)。
* B收到信息后如何确定这信息是A发的呢?
首先A必须通过某种途径把自己的公钥发给B。在发送信息前,A先用自己的密钥加密信息。B收到信息后就可以使用A的公钥来对收到的信息进行验证。这样就能确定信息来自A,这就是PGP所谓的签名(signature)与验证(verification)。
如何使用GnuPG来生成自己公钥/私钥
你首先要创建一个自己的密钥对,使用如下命令:
gpg --gen-key
期间会要求你输入个人信息如用户名,邮件地址,注释等,最好选有意义的。最后会让你输入passphrase(口令),这就是咱们通常意义上的密码了,解密和签名时都用得着的,按设密码的一般规则设就行了。除了那些Yes/No的问题,其他选项大可直接回车默认。
这里假设我们生成了一个名为foooo的用户:
用户名 (注释可选)
foooo (nobody)
接着你便可以使用下面之一的命令导出自己的公钥了
gpg --export foooo > foooo.pk ::导出为二进制形式,后缀名可以自己取
gpg --armor --export foooo > foooo.pk ::导出为ASCII字符形式,可以用文本编辑工具打开查看
下面是文本形式公钥的一个例子
-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: GnuPG v1.4.6 (MingW32)
mQGiBEV4GRURBAD9STM2AvmHRej94kjIbCfOYXZX6StxK8eU2M0d4j94KAgXe4s3
zxV6F1AyXxZAI8jVf/wyApE7iTGQ29B+kaMf4MRwhc3F80dz7R6Zh7TkUiqCP9q/
................................................................
................................................................
a8TUvPWylvqISQQYEQIACQUCRXgZHgIbDAAKCRC8O+Bl+/W+DU02AJ9Xq/5M5yiw
2tpiz/mSOXZzo+i8RQCgjZljVuAB9an8zFrSV52fzq9GhH8=
=N3TB
-----END PGP PUBLIC KEY BLOCK-----
现在你就可以把你的公钥传到网上或发给别人了。
获得别人的公钥之后可以使用下面的命令把公钥导入自己的钥匙圈
gpg --imp
如何使用GnuPG来加密/解密
你有一个文件beloved.txt要加密后发给你的GF,首先你得有GF的公钥,假设GF的公钥的用户信息为
ooyymm keti@butique.com
公钥为: ooyymm
加密命令如下
--recipient选项指定接受者,也就是你要用谁的公钥来加密。在GnuPG里面指定公钥或私钥可以使用该公钥或私钥的用户名、注释、邮件地址等。可以只输入一部分,但要唯一,GnuPG会查找用户列表来找到所需的公钥。
gpg --encrypt --recipient ooyymm beloved.txt ::生成.gpg的二进制加密文件
gpg --encrypt --armor --recipient ooyymm beloved.txt ::生成.asc得ASCII码签名文件
比如,使用邮箱地址来指定密钥:
gpg --encrypt --recipient keti@butique.com beloved.txt
当然GnuPG还可以通过其他手段来指明你要使用哪个公钥或私钥,具体请参看GnuPG的文档。
现在可以把加密后生成beloved.txt.gpg或beloved.txt.asc文件发给你GF吧。
你的GF收到文件后,必须使用自己的相应的私钥来解密,命令如下:
gpg --decrypt beloved.txt.gpg > beloved.txt
gpg --decrypt beloved.txt.asc > beloved.txt
这个过程需要输入passphrase。
如何使用GnuPG来签名/验证
签名过程:
1.在主机上创建密钥对
host # gpg --gen-key
按提示操作即可。操作完成后可以查看已经创建的密钥:
host # gpg --list-keys
2.然后使用密钥对文件进行签名,二进制签名:
host # gpg -s patch.tar.gz
创建的一个名为patch.tar.gz.gpg的文件就是签名后的包,如果想指定签名后的文件名字,可以用以下命令:
host # gpg -o pach.tar.gz.sig -s patch.tar.gz
要对对文本文件进行明文签名的话:
host # gpg --clearsign hello.txt
明文签名默认文件名为原文件名后加.asc,也可以使用-o 参数来指定文件名。
验证签名过程:
1.导入公钥
host # gpg -o pub.key --export userid
或者:
host # gpg --export userid >pub.key
userid是在创建密钥的时候输入的用户名。pub.key是二进制文件,如果想导出文本文件,加入-a参数即可:
host # gpg -a --export userid >pub.key.txt
target # gpg --imp
target # gpg --imp
target # gpg --ignore-time-conflict --improt pub.key
2.编辑公钥
target # gpg --edit-key userid
>fpr
查看这个公钥的指纹,核对指纹来确认这个key正确无误。
>sign
签署
->trust
信任
>quit
退出,问保存与否输入y。
3.验证签名:
target # gpg --verify patch.tar.gz.gpg
忽略时间校验:
target # gpg --ignore-time-conflict --verify patch.tar.gz.gpg
会显示签名校验的结果。
导入key来校验看上去比较麻烦,那么还有一种更简单的方式,直接在校验的时候通过参数来指定使用哪个key文件(必须是二进制的文件,文本文件不可用):
target # gpg --keyring pub.key --verify patch.tar.gz.gpg如果连时间都懒得设(有时候是没法设),那么也可以忽略时间戳的验证:
target # gpg --keyring pub.key --ignore-time-conflict --verify patch.tar.gz.gpg
附录:签名与验证过程
ms1@localhost:~/Desktop/work/sign> gpg --gen-key
gpg (GnuPG) 2.0.9; Copyright (C) 2008 Free Software Foundation, Inc.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Please select what kind of key you want:
(1) DSA and Elgamal (default)
(2) DSA (sign on
(5) RSA (sign on
Your selection? 1
DSA keypair will have 1024 bits.
ELG 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
<n> = key expires in n days
<n>w = key expires in n weeks
<n>m = key expires in n months
<n>y = key expires in n years
Key is valid for? (0)
Key does not expire at all
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) <heinrichh@duesseldorf.de>"
Real name: lagignition
Email address: lagignition@163.com
Comment:
You selected this USER-ID:
"lagignition <lagignition@163.com>"
Change (N)ame, (C)omment, (E)mail or (O)kay/(Q)uit? O
You need a Passphrase to protect your secret key.
We need to generate a lot of random bytes. It is a good idea to perform
some other act
disks) during the prime generation; this gives the random number
generator a better chance to gain enough entropy.
asdfasfdasdfasdfsadfsadf
asidpfupjxzcv
asdfiasjdf
wefijklasdf
We need to generate a lot of random bytes. It is a good idea to perform
some other act
disks) during the prime generation; this gives the random number
generator a better chance to gain enough entropy.
gpg: key BC123ABC 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: 2 signed: 0 trust: 0-, 0q, 0n, 0m, 0f, 2u
pub 1024D/BC123ABC 2009-11-06
Key fingerprint = A943 4978 4DF9 C9A5 726C 9163 7166 AAF9 BC12 3ABC
uid lagignition <lagignition@163.com>
sub 2048g/586310BF 2009-11-06
ms1@localhost:~/Desktop/work/sign> gpg -s -u lagignition test.txt
You need a passphrase to unlock the secret key for
user: "lagignition <lagignition@163.com>"
1024-bit DSA key, ID BC123ABC, created 2009-11-06
ms1@localhost:~/Desktop/work/sign> gpg -o pub_lagignition.key --export lagignition
[root@localhost sign]# gpg --imp
gpg: /root/.gnupg/trustdb.gpg: trustdb created
gpg: key BC123ABC: public key "lagignition <lagignition@163.com>" imp
gpg: Total number processed: 1
gpg: imp
[root@localhost sign]# gpg --verify test.txt.gpg
gpg: Signature made Thu 05 Nov 2009 09:24:48 PM EST using DSA key ID BC123ABC
gpg: Good signature from "lagignition <lagignition@163.com>"
gpg: WARNING: This key is not certified with a trusted signature!
gpg: There is no indication that the signature belongs to the owner.
Primary key fingerprint: A943 4978 4DF9 C9A5 726C 9163 7166 AAF9 BC12 3ABC
[root@localhost sign]# gpg --edit-key lagignition
gpg (GnuPG) 1.4.5; Copyright (C) 2006 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.
pub 1024D/BC123ABC created: 2009-11-06 expires: never usage: SC
trust: unknown validity: unknown
sub 2048g/586310BF created: 2009-11-06 expires: never usage: E
[ unknown] (1). lagignition <lagignition@163.com>
Command> fpr
pub 1024D/BC123ABC 2009-11-06 lagignition <lagignition@163.com>
Primary key fingerprint: A943 4978 4DF9 C9A5 726C 9163 7166 AAF9 BC12 3ABC
Command> trust
pub 1024D/BC123ABC created: 2009-11-06 expires: never usage: SC
trust: unknown validity: unknown
sub 2048g/586310BF created: 2009-11-06 expires: never usage: E
[ unknown] (1). lagignition <lagignition@163.com>
Please decide how far you trust this user to correctly verify other users' keys
(by looking at passports, checking fingerprints from different sources, etc.)
1 = I don't know or won't say
2 = I do NOT trust
3 = I trust marginally
4 = I trust fully
5 = I trust ultimately
m = back to the main menu
Your decision? 5
Do you really want to set this key to ultimate trust? (y/N) y
pub 1024D/BC123ABC created: 2009-11-06 expires: never usage: SC
trust: ultimate validity: unknown
sub 2048g/586310BF created: 2009-11-06 expires: never usage: E
[ unknown] (1). lagignition <lagignition@163.com>
Please note that the shown key validity is not necessarily correct
unless you restart the program.
Command> quit
[root@localhost sign]# gpg --verify test.txt.gpg
gpg: Signature made Thu 05 Nov 2009 09:24:48 PM EST using DSA key ID BC123ABC
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
gpg: Good signature from "lagignition <lagignition@163.com>"
[root@localhost sign]# echo $?
0
[root@localhost sign]# gpg --verify -u lagignition test.txt.gpg
gpg: Signature made Thu 05 Nov 2009 09:24:48 PM EST using DSA key ID BC123ABC
gpg: Good signature from "lagignition <lagignition@163.com>"
[root@localhost sign]# gpg -o test.txt test.txt.gpg
gpg: Signature made Thu 05 Nov 2009 09:24:48 PM EST using DSA key ID BC123ABC
gpg: Good signature from "lagignition <lagignition@163.com>"
Reference:
http://bbs.wmzhe.com/redirect.php?tid=264&goto=lastpost