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,程序自動查找私鈅,如果私鑰存在則進行解密。需要輸入私鈅的祕密(在生成是輸入的)。

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