gpg加解密

【演示环境 Centos 5.7, gpg】

GPG (an open source implementation of the OpenPGP IETF standard crypto system)通过GPG加密,生成公、私钥对,私钥为自己秘密保存,公钥则公开发布。别人通过我的公钥对文件进行加密,我通过私钥对文件进行解密。或者我通过私钥对文件进行签名,对方通过我们的公钥验证文件真伪。

【优点】:1、加密不需要密码,只要系统中有对应公钥即可。2、解密需要私钥及私钥保护密码,双重安全。

创建密钥:

  1. #gpg --gen-key 
    1. [root@client ~]# gpg --gen-key 
    2. gpg (GnuPG) 1.4.5; Copyright (C) 2006 Free Software Foundation, Inc. 
    3. This program comes with ABSOLUTELY NO WARRANTY. 
    4. This is free software, and you are welcome to redistribute it 
    5. under certain conditions. See the file COPYING for details. 
    6.  
    7. Please select what kind of key you want: 
    8.    (1) DSA and Elgamal (default) 
    9.    (2) DSA (sign only) 
    10.    (5) RSA (sign only) 
    11. Your selection? 1 (选择加密形式)
    12. DSA keypair will have 1024 bits. 
    13. ELG-E keys may be between 1024 and 4096 bits long. 
    14. What keysize do you want? (2048)  
    15. Requested keysize is 2048 bits 
    16. Please specify how long the key should be valid. 
    17.          0 = key does not expire 
    18.       <n>  = key expires in n days 
    19.       <n>w = key expires in n weeks 
    20.       <n>m = key expires in n months 
    21.       <n>y = key expires in n years 
    22. Key is valid for? (0) 1y (选择密钥期限)
    23. Key expires at Wed 06 Mar 2013 04:47:35 PM CST 
    24. Is this correct? (y/N) y (确认)
    25.  
    26. You need a user ID to identify your key; the software constructs the user ID 
    27. from the Real Name, Comment and Email Address in this form: 
    28.     "Heinrich Heine (Der Dichter) <heinrichh@duesseldorf.de>
    29.  
    30. Real name: tttttt  (用户名)
    31. Email address: [email protected] (邮件)
    32. Comment: hhhhhh (内容)
    33. You selected this USER-ID: 
    34.     "ttttt (hhhhhh) <test@126.com>
    35.  
    36. Change (N)ame, (C)omment, (E)mail or (O)kay/(Q)uit? O (保存)
    37. You need a Passphrase to protect your secret key. 
    38. Enter passphrase:  xxxxxx (这不是私钥,是私钥的保护密码,解密时用到)
    39.  
    40. We need to generate a lot of random bytes. It is a good idea to perform 
    41. some other action (type on the keyboard, move the mouse, utilize the 
    42. disks) during the prime generation; this gives the random number 
    43. generator a better chance to gain enough entropy. 
    44. +++++++++++++++++++++++++...+++++.++++++++++++++++++++.++++++++++++++++++++++++++++++.++++++++++++++++++++++++++++++++++++++++++++++++++>.++++++++++.....+++++ 
    45. We need to generate a lot of random bytes. It is a good idea to perform 
    46. some other action (type on the keyboard, move the mouse, utilize the 
    47. disks) during the prime generation; this gives the random number 
    48. generator a better chance to gain enough entropy. 
    49. ..+++++++++++++++++++++++++...+++++.+++++++++++++++++++++++++++++++++++++++++++++.+++++.+++++++++++++++..+++++++++++++++.+++++++++++++++..+++++++++++++++.+++++>.++++++++++>+++++>+++++.>+++++............................+++++^^^ 
    50. gpg: key 6B180A56 marked as ultimately trusted 
    51. public and secret key created and signed. 
    52.  
    53. gpg: checking the trustdb 
    54. gpg: 3 marginal(s) needed, 1 complete(s) needed, PGP trust model 
    55. gpg: depth: 0  valid:   5  signed:   0  trust: 0-, 0q, 0n, 0m, 0f, 5u 
    56. gpg: next trustdb check due at 2012-04-04 
    57. pub   1024D/6B180A56 2012-03-06 [expires: 2013-03-06] 
    58.       Key fingerprint = CDC4 0517 0772 40DD 9B3A  B76D 495C 223C 6B18 0A56 
    59. uid                  ttttt (hhhhhh) <test@126.com> 
    60. sub   2048g/C453FA82 2012-03-06 [expires: 2013-03-06] 
列出密钥公钥列表,(一台主机可以导入若干公私钥,列出列表查看uid)
  1. #gpg --list-keys  (或  gpg -v --fingerprint) 
    1. /root/.gnupg/secring.gpg ------------------------ 
    2. sec   1024D/91C9DDA0 2012-03-05 [expires: 2012-04-04]  
    3. uid                  tttttt (hhhhhh) <test@126.com>  
    4. ssb   2048g/61849255 2012-03-05 
备份公钥:
  1. #gpg -o keyfilename.pub --export 'uid' 
备份私钥:
  1. #gpg -o keyfilename.sec --export-secret-keys 'uid'
uid的内容为列出公钥中的uid 即“tttttt (hhhhhh) <test@126.com>如果没有 'uid' ,则是备份所有的私钥或私钥,-o表示输出到文件keyfilename中,如果加上-a的参数则输出文本格式的信息,否则输出的是二进制格式信息。
然后在别的机器上导入公钥。
  1. gpg --import keyfilename.pub 
列出公钥
  1. #gpg --list-keys  (或  gpg -v --fingerprint) 

使用公钥加密文件,生成'file.gpg'文件.

  1. #gpg --encrypt --recipient 'tttttt (hhhhhh) <test@126.com>filename 

将文件传回本机或者将本机生成的私钥上传到该机并导入就可以对其解密: 

  1. #gpg --output new-filename --decrypt filename.gpg 
  2.  
    1. You need a passphrase to unlock the secret key for 
    2. user: "tttttt (hhhhhh) <test@126.com>
    3. 2048-bit ELG-E key, ID 61849255, created 2012-03-05 (main key ID 91C9DDA0) 
    4.  
    5. Enter passphrase:  

 解密时,不需要指定uid,程序自动查找私钥,如果私钥存在则进行解密。需要输入私钥的秘密(在生成是输入的)。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章