Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1123373
  • 博文数量: 188
  • 博客积分: 2267
  • 博客等级: 大尉
  • 技术积分: 1907
  • 用 户 组: 普通用户
  • 注册时间: 2011-08-01 11:17
文章分类

全部博文(188)

文章存档

2016年(16)

2015年(16)

2014年(12)

2013年(32)

2012年(45)

2011年(67)

分类: LINUX

2011-08-17 14:06:46

       曾几何时加密已经是一种可保护在系统上存储或通过网络传输的数据免受损坏的一种必不可少的技术。Kerberos,Luks,SSL……它可用于验证来自特定人员的数据是否经过了修改,还可以保护数据的机密性!  
      在使用gpg产生密钥的时候,每次产生一对密钥,其中公钥是传给需要加密的人,你希望那些人可以与你通过加密数据传输就可以给他咯;而私钥就是用来解密的!


1> 使用gpg --gen-key 生成密钥对
# 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.

Please select what kind of key you want:
   (1) RSA and RSA (default)
   (2) DSA and Elgamal
   (3) DSA (sign only)
   (4) RSA (sign only)
Your selection? 
RSA 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) 1y
Key does not expire at all
Is this correct? (y/N) y
###### 前边三项可以保持默认,我这里选择的是1y(默认表示天,使用数字加 w表示周,m表示月,y表示年)
GnuPG needs to construct a user ID to identify your key.

Real name: ykyx00 ####必须大于5位
Email address: ykyx00@gmail.com ###方便以后有问题来好找你
Comment: ###描述性质的,可以忽略!
You selected this USER-ID:
    "ykyx00 "

Change (N)ame, (C)omment, (E)mail or (O)kay/(Q)uit? O     ### 按下O 之后,输入密码,他会有密码强度的提示(所以一寸长,一寸强,在这里得到来很好的体现),10位就很强悍啦!没事了玩玩鼠标吧,密码就是根据这个随机产生滴!要不他就在这停下来了!
ps:如果使用普通用户制作在这里出错,可能是没有权限调用图形,su - root ,然后执行 xhost + ,即可!
You need a Passphrase to protect your secret key.

can't connect to `/root/.gnupg/S.gpg-agent': No such file or directory
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: key 57308CA7 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
gpg: next trustdb check due at 2012-08-16
pub   2048R/57308CA7 2011-08-17 [expires: 2012-08-16]
      Key fingerprint = 8EC2 A971 8611 E816 1F35  B13C E304 847B 5730 8CA7
uid                  ykyx00
sub   2048R/6D7B90F6 2011-08-17 [expires: 2012-08-16]
### 看到这些共享你完成第一步,已经做好来密钥对!Ok! Let't go!


2>查看一下生成的密钥对,gpg --list-keys (其实生对的最后就可以看到了)
# gpg -k                      ##(和 gpg --list-keys是一样的)

/root/.gnupg/pubring.gpg
------------------------
pub   2048R/57308CA7 2011-08-17 [expires: 2012-08-16]
uid                  ykyx00
sub   2048R/6D7B90F6 2011-08-17 [expires: 2012-08-16]


3>导出公钥 
# gpg -a -o  ~/abc.key --export 57308CA7              #  -a 其实是 --armor 是以ASCII 形式加强, -o 是 --output 输出,export 就是导出的意思! abc.key 只是一个名字,可以随便起的,这里的57308CA7 就是查看时候的pub后边的那个公钥号了!

4>把公钥给需要加密的人(这里我传到了192.168.0.119)
# scp abc.key student@192.168.0.119:~

5>在 192.168.0.119 这台机子加密文件,然后传给目前这这台机子 192.168.0.19
 a.在119上导入 19发过来的公钥
  $gpg --import abc.key
 b.当然我们也可以看一下这个公钥对不对,检查其真实性
 $gpg --edit-key someone
someone是别人的用户ID
出现命令提示符 >
>fpr
查看用户someone的公钥的指纹,之后应设法核对指纹,以证明真实性。如果真实,则可以签署。
查看someone的指纹,用下面这个命令
#gpg -k
>sign
签署这个公钥,这样以后再使用它加密时,就不会再警告
>check
检查用户someone的公钥已有的签名
出现sig! 3   sig! 1   表示已完成
输入quit,回车,再输入y保存退出


 6>用这个公钥加密一个文件吧。这才是重点,我们需要用的地方!
  $ gpg -e -a -r ykyx00 file
######## -e 是 --encrypt ;-a 是 --armor;如果未提供 -r key-id(还记得你的Real name 吗!) ,命令将提示收件人输入。默认输出文件为 file.asc

7> scp file.asc root@192.168.0.19:~ ###是传这个加密过的.asc 的哦,不要传错了!

8> 我们回到这个192.168.0.19 解密一下这个,下来就是见证奇迹的时候,不要眨眼睛,看好咯!
# gpg -d file.asc #### -d 是decrypt 解密的意思!
这样的话,是有点麻烦,每次要看的话,都要输入一次密码!
# gpg -o file -d file.asc  #####指定一个输出文件file,以后打开file即可,不用这么麻烦了!

写到这里,一个简单的加密过程就算完成了!扩充一点吧,如果你觉得这个密钥有问题,应该怎么移除呢??
其实也很简单滴:
a.# gpg --delete-secret-keys 57308CA7                     ##pub

b.#gpg --delete-key 6D7B90F6                                #sub


ok!大功告成! 
阅读(1655) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~