实现对称加密
[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 天后过期
w= 密钥在 n 周后过期
m= 密钥在 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) "
真实姓名:gnupg
电子邮件地址:root@xfcy.org
注释:
您选定了这个用户标识:
“gnupg ”
更改姓名(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
sub 2048R/9EF29EB1 2013-04-03
[root@rhel6 GPG]# gpg --list-keys
/root/.gnupg/pubring.gpg
------------------------
pub 2048R/D44CECE9 2013-04-03
uid gnupg
sub 2048R/9EF29EB1 2013-04-03
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 " imported
gpg: Total number processed: 1
gpg: imported: 1 (RSA: 1)
[root@rhel6-2 ~]# gpg --list-keys
/root/.gnupg/pubring.gpg
------------------------
pub 2048R/D44CECE9 2013-04-03
uid gnupg
sub 2048R/9EF29EB1 2013-04-03
[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
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 "
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
要为这把密钥建立一份吊销证书吗?(y/N)y
请选择吊销的原因:
0= 未指定原因
1= 密钥已泄漏
2= 密钥被替换
3= 密钥不再使用
Q= 取消
(也许您会想要在这里选择 1)
您的决定是什么?1
请输入描述(可选);以空白行结束:
>
吊销原因:密钥已泄漏
(不给定描述)
这样可以吗? (y/N)y
您需要输入密码,才能解开这个用户的私钥:“gnupg ”
2048 位的 RSA 密钥,钥匙号 D44CECE9,建立于 2013-04-03
已强行使用 ASCII 封装过的输出。
已建立吊销证书。
请把这个文件转移到一个可隐藏起来的介质(如软盘)上;如果坏人能够取得这
份证书的话,那么他就能让您的密钥无法继续使用。把这份凭证打印出来再藏
到安全的地方也是很好的方法,以免您的保存媒体损毁而无法读取。但是千万
小心:您的机器上的打印系统可能会在打印过程中把这些数据临时在某个其他
人也能够看得到的地方!
[root@rhel6 GPG]# gpg --keyserver Server Address --send-keys mykeyID
9.密钥的删除:
必须先删除私钥,然后才能删除公钥。
[root@rhel6 ~]# gpg --list-keys
/root/.gnupg/pubring.gpg
------------------------
pub 2048R/D44CECE9 2013-04-03
uid gnupg
sub 2048R/9EF29EB1 2013-04-03
[root@rhel6 ~]# gpg --delete-secret-keys D44CECE9
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.
sec 2048R/D44CECE9 2013-04-03 gnupg
要从钥匙环里删除这把密钥吗?(y/N)y
这是一把私钥!——真的要删除吗?(y/N)y
[root@rhel6 ~]# gpg --delete-keys D44CECE9
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.
pub 2048R/D44CECE9 2013-04-03 gnupg
要从钥匙环里删除这把密钥吗?(y/N)y
[root@rhel6 ~]# gpg --list-keys