天天看点

Linux下的文件加解密技术:GnuPG

GnuPG(GNU Privacy Guard或GPG):Linux 下的基于PGP(Pretty Good Privacy)机制的加密及签名软件

PGP(Pretty Good Privacy)是一个基于RSA 公钥加密体系的邮件加密软件,不但可以对用户的数据保密,以防止非授权者阅读,还能给邮件加上数字签名,从而使收信人确信邮件是由您发出的。从而让人们可以安全地与从未见过的人们通信,而事先不需要任何保密的渠道用来传递密钥。

GnuPG 是实现安全通信和数据存储的一系列工具集,可以做数据加密、数字签名。

在功能上,它和PGP 是一样的,但PGP 使用了IDEA专利算法,使用PGP 会有许可证的问题;而GnuPG 并没有使用这个算法,所以对用户来说使用上没有任何限制。

GnuPG 支持的算法有如下。

公钥:RSA、RSA-E、RSA-S、ELG-E、DSA

对称加密:3DES、CAST5、BLOWFISH、AES、AES192、AES256、TWOFISH

散列:MD5、SHA1、RIPEMD160、SHA256、SHA384、SHA512

压缩:不压缩、ZIP、ZLIB、BZIP2

gpg 指令 [选项] [文件名] 

指令: 

-s, --sign [文件名]        生成一份签字 

--clearsign [文件名]       生成一份明文签字 

-b, --detach-sign        生成一份分离的签字 

-e, --encrypt            加密数据 

-c, --symmetric          仅使用对称加密 

-d, --decrypt            解密数据(默认) 

--verify             验证签字 

--list-keys          列出密钥 

--list-sigs          列出密钥和签字 

--check-sigs             列出并检查密钥签字 

--fingerprint            列出密钥和指纹 

-K, --list-secret-keys    列出私钥 

--gen-key            生成一副新的密钥对 

--delete-keys            从公钥钥匙环里删除密钥 

--delete-secret-keys      从私钥钥匙环里删除密钥 

--sign-key           为某把密钥添加签字 

--lsign-key          为某把密钥添加本地签字 

--edit-key           编辑某把密钥或为其添加签字 

--gen-revoke             生成一份吊销证书 

--export             导出密钥 

--send-keys          把密钥导出到某个公钥服务器上 

--recv-keys          从公钥服务器上导入密钥 

--search-keys            在公钥服务器上搜寻密钥 

--refresh-keys           从公钥服务器更新所有的本地密钥 

--import             导入/合并密钥 

--card-status            打印卡状态 

--card-edit          更改卡上的数据 

--change-pin             更改卡的 PIN 

--update-trustdb         更新信任度数据库 

--print-md 算法 [文件]     使用指定的散列算法打印报文散列值 

选项: 

-a, --armor          输出经 ASCII 封装 

-r, --recipient          为收件者“某甲”加密 

-u, --local-user          使用这个用户标识来签字或解密 

-z N                 设定压缩等级为 N (0 表示不压缩) 

--textmode           使用标准的文本模式 

-o, --output             指定输出文件 

-v, --verbose            详细模式 

-n, --dry-run            不做任何改变 

-i, --interactive         覆盖前先询问 

--openpgp            行为严格遵循 OpenPGP 定义 

--pgp2           生成与 PGP 2.x 兼容的报文 

实现对称加密 

[root@rhel6 ~]# echo "对称加密测试" > test 

[root@rhel6 ~]# gpg -c test  

[root@rhel6 ~]# rm -rf test 

[root@rhel6 ~]# gpg test.gpg  

[root@rhel6 ~]# cat test 

对称加密测试 

实现不对称加密 

1.创建公私钥 

[root@rhel6 ~]# gpg --list-key 

[root@rhel6 ~]# gpg --gen-key 

gpg (GnuPG) 2.0.14; Copyright (C) 2009 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. 

请选择您要使用的密钥种类: 

   (1) RSA and RSA (default) 

   (2) DSA and Elgamal 

   (3) DSA (仅用于签名) 

   (4) RSA (仅用于签名) 

您的选择?  

RSA 密钥长度应在 1024 位与 4096 位之间。 

您想要用多大的密钥尺寸?(2048) 

您所要求的密钥尺寸是 2048 位 

请设定这把密钥的有效期限。 

         0 = 密钥永不过期 

      <n>  = 密钥在 n 天后过期 

      <n>w = 密钥在 n 周后过期 

      <n>m = 密钥在 n 月后过期 

      <n>y = 密钥在 n 年后过期 

密钥的有效期限是?(0)  

密钥永远不会过期 

以上正确吗?(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) <[email protected]>" 

真实姓名:gnupg 

电子邮件地址:[email protected] 

注释: 

您选定了这个用户标识: 

    “gnupg <[email protected]>” 

更改姓名(N)、注释(C)、电子邮件地址(E)或确定(O)/退出(Q)?o 

您需要一个密码来保护您的私钥。 

我们需要生成大量的随机字节。这个时候您可以多做些琐事(像是敲打键盘、移动 

鼠标、读写硬盘之类的),这会让随机数字发生器有更好的机会获得足够的熵数。 

gpg: 密钥 D44CECE9 被标记为绝对信任 

公钥和私钥已经生成并经签名。 

gpg: 正在检查信任度数据库 

gpg: 需要 3 份勉强信任和 1 份完全信任,PGP 信任模型 

gpg: 深度:0 有效性:  1 已签名:  0 信任度:0-,0q,0n,0m,0f,1u 

pub   2048R/D44CECE9 2013-04-03 

密钥指纹 = 9F08 B39F 897F C1E8 2321  1766 5E11 0908 D44C ECE9 

uid                  gnupg <[email protected]> 

sub   2048R/9EF29EB1 2013-04-03 

[root@rhel6 GPG]# gpg --list-keys 

/root/.gnupg/pubring.gpg 

------------------------ 

2.导出公(私)钥 

[root@rhel6 GPG]# gpg --export --armor D44CECE9 > pub_file.key 

[root@rhel6 GPG]# gpg --export-secret-keys --armor D44CECE9 > priv_file.key 

3.发布公钥: 

[root@rhel6 GPG]# cp pub_file.key /var/ftp/pub/ 

4.导入公钥 

[root@rhel6-2 ~]# scp rhel6:/root/pub_file.key ./ 

pub_file.key                                                  100% 1703     1.7KB/s   00:00 

[root@rhel6-2 ~]# gpg --list-keys 

[root@rhel6-2 ~]# gpg --import pub_file.key  

gpg: key D44CECE9: public key "gnupg <[email protected]>" imported 

gpg: Total number processed: 1 

gpg:               imported: 1  (RSA: 1) 

[root@rhel6-2 ~]# gpg --list-keys       

[root@rhel6-2 ~]# echo "secret password" > gpg_test 

5.远程用户通过公钥加密文件 

[root@rhel6-2 ~]# gpg --encrypt --armor -r D44CECE9 gpg_test  

gpg: 9EF29EB1: There is no assurance this key belongs to the named user 

pub  2048R/9EF29EB1 2013-04-03 gnupg <[email protected]> 

 Primary key fingerprint: 9F08 B39F 897F C1E8 2321  1766 5E11 0908 D44C ECE9 

      Subkey fingerprint: F498 D9D1 4D32 361C 13D6  49FF E9EE 6FD8 9EF2 9EB1 

It is NOT certain that the key belongs to the person named 

in the user ID.  If you *really* know what you are doing, 

you may answer the next question with yes. 

Use this key anyway? (y/N) y 

6.传送加密文件: 

[root@rhel6-2 ~]# scp gpg_test.asc rhel6:/root/ 

root@rhel6's password:  

gpg_test.asc                                               100%  580     0.6KB/s   00:00 

7.解密加密文件: 

[root@rhel6 ~]# gpg -o gpg_test --decrypt gpg_test.asc     

You need a passphrase to unlock the secret key for 

user: "gnupg <[email protected]>" 

2048-bit RSA key, ID 9EF29EB1, created 2013-04-03 (main key ID D44CECE9) 

[root@rhel6 ~]# cat gpg_test 

secret password 

8.密钥的回收: 

当密钥对生成之后,应该立即做一个公钥回收证书,如果忘记了私钥的口令或者私钥丢失/被盗窃,可以发布这个证书来声明以前的公钥不再有效。 

一旦回收证书被发放,以前的证书就不能再被其他用户访问,因此以前的公钥也就失效了。 

[root@rhel6 ~]# gpg --output revoke.asc --gen-revoke D44CECE9 

sec  2048R/D44CECE9 2013-04-03 gnupg <[email protected]> 

要为这把密钥建立一份吊销证书吗?(y/N)y 

请选择吊销的原因: 

  0 = 未指定原因 

  1 = 密钥已泄漏 

  2 = 密钥被替换 

  3 = 密钥不再使用 

  Q = 取消 

(也许您会想要在这里选择 1) 

您的决定是什么?1 

请输入描述(可选);以空白行结束: 

>  

吊销原因:密钥已泄漏 

(不给定描述) 

这样可以吗? (y/N)y 

您需要输入密码,才能解开这个用户的私钥:“gnupg <[email protected]>” 

2048 位的 RSA 密钥,钥匙号 D44CECE9,建立于 2013-04-03 

已强行使用 ASCII 封装过的输出。 

已建立吊销证书。 

请把这个文件转移到一个可隐藏起来的介质(如软盘)上;如果坏人能够取得这 

份证书的话,那么他就能让您的密钥无法继续使用。把这份凭证打印出来再藏 

到安全的地方也是很好的方法,以免您的保存媒体损毁而无法读取。但是千万 

小心:您的机器上的打印系统可能会在打印过程中把这些数据临时在某个其他 

人也能够看得到的地方! 

[root@rhel6 GPG]# gpg --keyserver Server Address --send-keys mykeyID 

9.密钥的删除: 

必须先删除私钥,然后才能删除公钥。 

[root@rhel6 ~]# gpg --list-keys 

[root@rhel6 ~]# gpg --delete-secret-keys D44CECE9 

要从钥匙环里删除这把密钥吗?(y/N)y 

这是一把私钥!——真的要删除吗?(y/N)y 

[root@rhel6 ~]# gpg --delete-keys D44CECE9        

pub  2048R/D44CECE9 2013-04-03 gnupg <[email protected]> 

[root@rhel6 ~]# gpg --list-keys   

     本文转自Vnimos51CTO博客,原文链接:http://blog.51cto.com/vnimos/1175889,如需转载请自行联系原作者

继续阅读