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
 
  1. gpg 指令 [選項] [文件名] 
  2. 指令: 
  3. -s, --sign [文件名]        生成一份簽字 
  4. --clearsign [文件名]       生成一份明文簽字 
  5. -b, --detach-sign        生成一份分離的簽字 
  6. -e, --encrypt            加密數據 
  7. -c, --symmetric          僅使用對稱加密 
  8. -d, --decrypt            解密數據(默認) 
  9. --verify             驗證簽字 
  10. --list-keys          列出密鑰 
  11. --list-sigs          列出密鑰和簽字 
  12. --check-sigs             列出並檢查密鑰簽字 
  13. --fingerprint            列出密鑰和指紋 
  14. -K, --list-secret-keys    列出私鑰 
  15. --gen-key            生成一副新的密鑰對 
  16. --delete-keys            從公鑰鑰匙環裏刪除密鑰 
  17. --delete-secret-keys      從私鑰鑰匙環裏刪除密鑰 
  18. --sign-key           爲某把密鑰添加簽字 
  19. --lsign-key          爲某把密鑰添加本地簽字 
  20. --edit-key           編輯某把密鑰或爲其添加簽字 
  21. --gen-revoke             生成一份吊銷證書 
  22. --export             導出密鑰 
  23. --send-keys          把密鑰導出到某個公鑰服務器上 
  24. --recv-keys          從公鑰服務器上導入密鑰 
  25. --search-keys            在公鑰服務器上搜尋密鑰 
  26. --refresh-keys           從公鑰服務器更新所有的本地密鑰 
  27. --import             導入/合併密鑰 
  28. --card-status            打印卡狀態 
  29. --card-edit          更改卡上的數據 
  30. --change-pin             更改卡的 PIN 
  31. --update-trustdb         更新信任度數據庫 
  32. --print-md 算法 [文件]     使用指定的散列算法打印報文散列值 
  33. 選項: 
  34. -a, --armor          輸出經 ASCII 封裝 
  35. -r, --recipient          爲收件者“某甲”加密 
  36. -u, --local-user          使用這個用戶標識來簽字或解密 
  37. -z N                 設定壓縮等級爲 N (0 表示不壓縮) 
  38. --textmode           使用標準的文本模式 
  39. -o, --output             指定輸出文件 
  40. -v, --verbose            詳細模式 
  41. -n, --dry-run            不做任何改變 
  42. -i, --interactive         覆蓋前先詢問 
  43. --openpgp            行爲嚴格遵循 OpenPGP 定義 
  44. --pgp2           生成與 PGP 2.x 兼容的報文 
 
 
  1. 實現對稱加密 
  2. [root@rhel6 ~]# echo "對稱加密測試" > test 
  3. [root@rhel6 ~]# gpg -c test  
  4. [root@rhel6 ~]# rm -rf test 
  5. [root@rhel6 ~]# gpg test.gpg  
  6. [root@rhel6 ~]# cat test 
  7. 對稱加密測試 
  8.  
  9. 實現不對稱加密 
  10. 1.創建公私鑰 
  11. [root@rhel6 ~]# gpg --list-key 
  12. [root@rhel6 ~]# gpg --gen-key 
  13. gpg (GnuPG) 2.0.14; Copyright (C) 2009 Free Software Foundation, Inc. 
  14. This is free software: you are free to change and redistribute it. 
  15. There is NO WARRANTY, to the extent permitted by law. 
  16.  
  17. 請選擇您要使用的密鑰種類: 
  18.    (1) RSA and RSA (default) 
  19.    (2) DSA and Elgamal 
  20.    (3) DSA (僅用於簽名) 
  21.    (4) RSA (僅用於簽名) 
  22. 您的選擇?  
  23. RSA 密鑰長度應在 1024 位與 4096 位之間。 
  24. 您想要用多大的密鑰尺寸?(2048) 
  25. 您所要求的密鑰尺寸是 2048 位 
  26. 請設定這把密鑰的有效期限。 
  27.          0 = 密鑰永不過期 
  28.       <n>  = 密鑰在 n 天后過期 
  29.       <n>w = 密鑰在 n 周後過期 
  30.       <n>m = 密鑰在 n 月後過期 
  31.       <n>y = 密鑰在 n 年後過期 
  32. 密鑰的有效期限是?(0)  
  33. 密鑰永遠不會過期 
  34. 以上正確嗎?(y/n)y 
  35.  
  36. You need a user ID to identify your key; the software constructs the user ID 
  37. from the Real Name, Comment and Email Address in this form: 
  38.     "Heinrich Heine (Der Dichter) <heinrichh@duesseldorf.de>
  39.  
  40. 真實姓名:gnupg 
  41. 電子郵件地址:[email protected] 
  42. 註釋: 
  43. 您選定了這個用戶標識: 
  44.     “gnupg <root@xfcy.org>” 
  45.  
  46. 更改姓名(N)、註釋(C)、電子郵件地址(E)或確定(O)/退出(Q)?o 
  47. 您需要一個密碼來保護您的私鑰。 
  48.  
  49. 我們需要生成大量的隨機字節。這個時候您可以多做些瑣事(像是敲打鍵盤、移動 
  50. 鼠標、讀寫硬盤之類的),這會讓隨機數字發生器有更好的機會獲得足夠的熵數。 
  51. gpg: 密鑰 D44CECE9 被標記爲絕對信任 
  52. 公鑰和私鑰已經生成並經簽名。 
  53.  
  54. gpg: 正在檢查信任度數據庫 
  55. gpg: 需要 3 份勉強信任和 1 份完全信任,PGP 信任模型 
  56. gpg: 深度:0 有效性:  1 已簽名:  0 信任度:0-,0q,0n,0m,0f,1u 
  57. pub   2048R/D44CECE9 2013-04-03 
  58. 密鑰指紋 = 9F08 B39F 897F C1E8 2321  1766 5E11 0908 D44C ECE9 
  59. uid                  gnupg <root@xfcy.org> 
  60. sub   2048R/9EF29EB1 2013-04-03 
  61.  
  62. [root@rhel6 GPG]# gpg --list-keys 
  63. /root/.gnupg/pubring.gpg 
  64. ------------------------ 
  65. pub   2048R/D44CECE9 2013-04-03 
  66. uid                  gnupg <root@xfcy.org> 
  67. sub   2048R/9EF29EB1 2013-04-03 
  68.  
  69. 2.導出公(私)鑰 
  70. [root@rhel6 GPG]# gpg --export --armor D44CECE9 > pub_file.key 
  71. [root@rhel6 GPG]# gpg --export-secret-keys --armor D44CECE9 > priv_file.key 
  72.  
  73. 3.發佈公鑰: 
  74. [root@rhel6 GPG]# cp pub_file.key /var/ftp/pub/ 
  75.  
  76. 4.導入公鑰 
  77. [root@rhel6-2 ~]# scp rhel6:/root/pub_file.key ./ 
  78. pub_file.key                                                  100% 1703     1.7KB/s   00:00 
  79. [root@rhel6-2 ~]# gpg --list-keys 
  80. [root@rhel6-2 ~]# gpg --import pub_file.key  
  81. gpg: key D44CECE9: public key "gnupg <root@xfcy.org>" imported 
  82. gpg: Total number processed: 1 
  83. gpg:               imported: 1  (RSA: 1) 
  84. [root@rhel6-2 ~]# gpg --list-keys       
  85. /root/.gnupg/pubring.gpg 
  86. ------------------------ 
  87. pub   2048R/D44CECE9 2013-04-03 
  88. uid                  gnupg <root@xfcy.org> 
  89. sub   2048R/9EF29EB1 2013-04-03 
  90. [root@rhel6-2 ~]# echo "secret password" > gpg_test 
  91.  
  92. 5.遠程用戶通過公鑰加密文件 
  93. [root@rhel6-2 ~]# gpg --encrypt --armor -r D44CECE9 gpg_test  
  94. gpg: 9EF29EB1: There is no assurance this key belongs to the named user 
  95.  
  96. pub  2048R/9EF29EB1 2013-04-03 gnupg <root@xfcy.org> 
  97.  Primary key fingerprint: 9F08 B39F 897F C1E8 2321  1766 5E11 0908 D44C ECE9 
  98.       Subkey fingerprint: F498 D9D1 4D32 361C 13D6  49FF E9EE 6FD8 9EF2 9EB1 
  99.  
  100. It is NOT certain that the key belongs to the person named 
  101. in the user ID.  If you *really* know what you are doing, 
  102. you may answer the next question with yes. 
  103.  
  104. Use this key anyway? (y/N) y 
  105.  
  106. 6.傳送加密文件: 
  107. [root@rhel6-2 ~]# scp gpg_test.asc rhel6:/root/ 
  108. root@rhel6's password:  
  109. gpg_test.asc                                               100%  580     0.6KB/s   00:00 
  110.  
  111. 7.解密加密文件: 
  112. [root@rhel6 ~]# gpg -o gpg_test --decrypt gpg_test.asc     
  113. You need a passphrase to unlock the secret key for 
  114. user: "gnupg <root@xfcy.org>
  115. 2048-bit RSA key, ID 9EF29EB1, created 2013-04-03 (main key ID D44CECE9) 
  116. [root@rhel6 ~]# cat gpg_test 
  117. secret password 
  118.  
  119.  
  120. 8.密鑰的回收: 
  121. 當密鑰對生成之後,應該立即做一個公鑰回收證書,如果忘記了私鑰的口令或者私鑰丟失/被盜竊,可以發佈這個證書來聲明以前的公鑰不再有效。 
  122. 一旦回收證書被髮放,以前的證書就不能再被其他用戶訪問,因此以前的公鑰也就失效了。 
  123. [root@rhel6 ~]# gpg --output revoke.asc --gen-revoke D44CECE9 
  124. sec  2048R/D44CECE9 2013-04-03 gnupg <root@xfcy.org> 
  125.  
  126. 要爲這把密鑰建立一份吊銷證書嗎?(y/N)y 
  127. 請選擇吊銷的原因: 
  128.   0 = 未指定原因 
  129.   1 = 密鑰已泄漏 
  130.   2 = 密鑰被替換 
  131.   3 = 密鑰不再使用 
  132.   Q = 取消 
  133. (也許您會想要在這裏選擇 1) 
  134. 您的決定是什麼?1 
  135. 請輸入描述(可選);以空白行結束: 
  136. >  
  137. 吊銷原因:密鑰已泄漏 
  138. (不給定描述) 
  139. 這樣可以嗎? (y/N)y 
  140.  
  141. 您需要輸入密碼,才能解開這個用戶的私鑰:“gnupg <root@xfcy.org>” 
  142. 2048 位的 RSA 密鑰,鑰匙號 D44CECE9,建立於 2013-04-03 
  143. 已強行使用 ASCII 封裝過的輸出。 
  144. 已建立吊銷證書。 
  145.  
  146. 請把這個文件轉移到一個可隱藏起來的介質(如軟盤)上;如果壞人能夠取得這 
  147. 份證書的話,那麼他就能讓您的密鑰無法繼續使用。把這份憑證打印出來再藏 
  148. 到安全的地方也是很好的方法,以免您的保存媒體損毀而無法讀取。但是千萬 
  149. 小心:您的機器上的打印系統可能會在打印過程中把這些數據臨時在某個其他 
  150. 人也能夠看得到的地方! 
  151.  
  152. [root@rhel6 GPG]# gpg --keyserver Server Address --send-keys mykeyID 
  153.  
  154. 9.密鑰的刪除: 
  155. 必須先刪除私鑰,然後才能刪除公鑰。 
  156. [root@rhel6 ~]# gpg --list-keys 
  157. /root/.gnupg/pubring.gpg 
  158. ------------------------ 
  159. pub   2048R/D44CECE9 2013-04-03 
  160. uid                  gnupg <root@xfcy.org> 
  161. sub   2048R/9EF29EB1 2013-04-03 
  162. [root@rhel6 ~]# gpg --delete-secret-keys D44CECE9 
  163. gpg (GnuPG) 2.0.14; Copyright (C) 2009 Free Software Foundation, Inc. 
  164. This is free software: you are free to change and redistribute it. 
  165. There is NO WARRANTY, to the extent permitted by law. 
  166.  
  167. sec  2048R/D44CECE9 2013-04-03 gnupg <root@xfcy.org> 
  168.  
  169. 要從鑰匙環裏刪除這把密鑰嗎?(y/N)y 
  170. 這是一把私鑰!——真的要刪除嗎?(y/N)y 
  171.  
  172. [root@rhel6 ~]# gpg --delete-keys D44CECE9        
  173. gpg (GnuPG) 2.0.14; Copyright (C) 2009 Free Software Foundation, Inc. 
  174. This is free software: you are free to change and redistribute it. 
  175. There is NO WARRANTY, to the extent permitted by law. 
  176. pub  2048R/D44CECE9 2013-04-03 gnupg <root@xfcy.org> 
  177. 要從鑰匙環裏刪除這把密鑰嗎?(y/N)y 
  178. [root@rhel6 ~]# gpg --list-keys   
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章