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
- gpg 指令 [選項] [文件名]
- 指令:
- -s,
-
- -b,
- -e,
- -c,
- -d,
-
-
-
-
-
- -K,
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 選項:
- -a,
- -r,
- -u,
- -z N 設定壓縮等級爲 N (0 表示不壓縮)
-
- -o,
- -v,
- -n,
- -i,
-
-
- 實現對稱加密
- [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> = 密鑰在 n 天后過期
- <n>w = 密鑰在 n 周後過期
- <n>m = 密鑰在 n 月後過期
- <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) <heinrichh@duesseldorf.de>"
-
- 真實姓名:gnupg
- 電子郵件地址:[email protected]
- 註釋:
- 您選定了這個用戶標識:
- “gnupg <root@xfcy.org>”
-
- 更改姓名(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 <root@xfcy.org>
- sub 2048R/9EF29EB1 2013-04-03
-
- [root@rhel6 GPG]# gpg --list-keys
- /root/.gnupg/pubring.gpg
- ------------------------
- pub 2048R/D44CECE9 2013-04-03
- uid gnupg <root@xfcy.org>
- 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 <root@xfcy.org>" 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 <root@xfcy.org>
- 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 <root@xfcy.org>
- 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 <root@xfcy.org>"
- 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 <root@xfcy.org>
-
- 要爲這把密鑰建立一份吊銷證書嗎?(y/N)y
- 請選擇吊銷的原因:
- 0 = 未指定原因
- 1 = 密鑰已泄漏
- 2 = 密鑰被替換
- 3 = 密鑰不再使用
- Q = 取消
- (也許您會想要在這裏選擇 1)
- 您的決定是什麼?1
- 請輸入描述(可選);以空白行結束:
- >
- 吊銷原因:密鑰已泄漏
- (不給定描述)
- 這樣可以嗎? (y/N)y
-
- 您需要輸入密碼,才能解開這個用戶的私鑰:“gnupg <root@xfcy.org>”
- 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 <root@xfcy.org>
- 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 <root@xfcy.org>
-
- 要從鑰匙環裏刪除這把密鑰嗎?(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 <root@xfcy.org>
- 要從鑰匙環裏刪除這把密鑰嗎?(y/N)y
- [root@rhel6 ~]# gpg --list-keys