使用GnuPG加密和簽名
PGP的出現
PGP是一套用于消息加密、驗證的應用程序,其加密/驗證機制采用名為IDEA算法的散列算法。PGP及其同類產品均遵守OpenPGP數據加解密標準。
</blockquote>但是PGP的普及并非一帆風順,PGP的主要開發者Philip R. Zimmermann,在1991年的時候將源碼放到了互聯網上。由于美國在當時對于加解密算法和軟件的出口有嚴格的限制,Zimmermann被FBI偵訊達數年。不過,美國是保護個人著作的。于是,Zimmermann在志愿者的幫助下,將PGP的源碼被出版為書,銷往歐洲,由于合法,所以才有了今天的普及。
而由于PGP是商業軟件,GnuPG是遵循OpenPGP數據加解密標準的自由軟件實現,中文譯為GNU隱私衛士,衛士之名當之無愧,不像某些公司,阿貓阿狗都敢叫衛士。
對稱加密和非對稱加密
對稱加密 簡單的說,加密和解密時使用相同的密鑰即對稱加密,比如我們日常使用的壓縮軟件加密使用的就是對稱加密。
</li>非對稱加密 采用這種方法,首先需要生成一對密鑰。公開發布出去的稱為公鑰,用戶自己保密的稱為私鑰。公鑰用來加密,私鑰用來解密。
</li> </ul>RSA算法
RSA加 密算法是常用的非對稱加密算法,一般認為RSA是1977年由在MIT工作的Ron Rivest、Adi Shamir和Leonard Adleman一起提出的,其實早在1973年,在英國政府通訊總部工作的數學家Clifford Cocks在一個內部文件中提出了一個相同的算法,但他的發現被列入機密,一直到1997年才被發表。
RSA算法的可靠性
要知道,沒有絕對的安全,但是要破解RSA需要相當長的時間或是很大的代價,就可以認為它是安全的。
密鑰長度是256位,個人電腦幾小時就能分解其因子。
</li>1999年,數百臺電腦合作分解了512位長的密鑰。
</li>1994年Peter Shor)證明一臺量子計算機可以在多項式時間內進行因數分解。
</li> </ul>對極大整數做因數分解的難度決定了RSA算法的可靠性。 今天只有短的RSA鑰匙才可能被強力方式解破。到2013年為止,世界上還沒有任何可靠的攻擊RSA算法的方式。只要其鑰匙的長度足夠長,用RSA加密的信息實際上是不能被解破的。
</blockquote>因此,未來出現了量子計算機,或者找到了一種有效分解大整數的算法,RSA就被看作是不可靠的。 看到這,你應該明白,一定要用4096位長的密鑰。
使用GnuPG
安裝GnuPG
Linux用戶直接使用包管理器安裝,也可安裝GUI前端KGpg。
</li>OS X用戶有GPGTools項目可以使用GnuPG。
</li>Windows用戶可以在Gpg4win項目找到安裝程序。
</li> </ul>生成鑰匙對
$ gpg --gen-key接著會出現算法選擇的問題:
gpg (GnuPG) 1.0.7; Copyright (C) 2002 Free Software Foundation, Inc. This program comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to redistribute it under certain conditions. See the file COPYING for details.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?</code></pre>
默認的是RSA,既支持加密也支持簽名,輸入1并回車。接著選擇密鑰長度:
RSA keypair will have 1024 bits. RSA keys may be between 1024 and 4096 bits long. What keysize do you want? (2048)2048默認的長度是2048位,直接回車即可。不過俺強烈建議使用4096位,輸入4096并回車,接下來選擇鑰匙有效期限:
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)0 Key does not expire at all Is this correct (y/n)? y 默認永久有效,建議輸入一個有效期限并回車,120表示120天,24w表示24個星期,6m表示6個月,1y表示1年。 我用的默認永久有效,直接回車。問你是否確定,輸入y并回車。
接著填寫個人信息:
GnuPG needs to construct a user ID to identify your key.Real name: ibrother Email address: ibrother.linux@gmail.com Comment: ibrother in github You selected this USER-ID: "ibrother (ibrother in github) IBROTHER.LINUX@GMAIL.COM "
Change (N)ame, (C)omment, (E)mail or (O)kay/(Q)uit? O /IBROTHER.LINUX@GMAIL.COM</code></pre>
這3行個人信息將用于產生鑰匙的uid 第1行建議填寫網名,比如俺常用的網名是ibrother。 第2行填寫你的email地址,比如俺的郵箱ibrother.linux@gmail.com 第3行是備注,用于進一步標明身份。
接下來,輸入密碼用于保護私鑰,程序就會開始生成鑰匙對,過程可能幾秒鐘到幾分鐘。此時,動動鼠標讓電腦工作起來有助于產生足夠的隨機數。
You need a Passphrase to protect your secret key.Enter passphrase:</code></pre>
一旦生成鑰匙對,強烈建議立即生成注銷證書
首先查看一下公鑰ID
$ gpg --list-keys--list-keys/home/ibrother/.gnupg/pubring.gpg
pub 2048R/A0A2A2F8 2014-07-09 uid [ultimate] ibrother (ibrother in github) IBROTHER.LINUX@GMAIL.COM
sub 2048R/4AAB15B9 2014-07-09 /IBROTHER.LINUX@GMAIL.COM</code></pre>
生成注銷證書
gpg --output revoke.asc --gen-revoke A0A2A2F8sec 2048R/A0A2A2F8 2014-07-09 ibrother (ibrother in github) IBROTHER.LINUX@GMAIL.COM
Create a revocation certificate for this key? y Please select the reason for the revocation: 0 = No reason specified 1 = Key has been compromised 2 = Key is superseded 3 = Key is no longer used Q = Cancel (Probably you want to select 1 here) Your decision? 1 Enter an optional description; end it with an empty line: > Someone cracked me and got my key and passphrase > Reason for revocation: Key has been compromised Someone cracked me and got my key and passphrase Is this okay? y
You need a passphrase to unlock the secret key for user: "ibrother (ibrother in github) IBROTHER.LINUX@GMAIL.COM " 2048-bit RSA key, ID A0A2A2F8, created 2014-07-09
ASCII armored output forced. Revocation certificate created.
Please move it to a medium which you can hide away; if Mallory gets access to this certificate he can use it to make your key unusable. It is smart to print this certificate and store it away, just in case your media become unreadable. But have some caution: The print system of your machine might store the data and make it available to others! /IBROTHER.LINUX@GMAIL.COM /IBROTHER.LINUX@GMAIL.COM</code></pre>
注銷證書已經生成,請妥善保管。如果不小心被人盜取了密碼,這是你注銷公鑰的唯一憑證。
發布公鑰
由于是公鑰加密,我們必須將公鑰發布出去,有以下兩種方法:
導出公鑰
$ gpg -a --output key.public --export UID把UID替換成你的名字或email地址。會在當前工作目錄得到key.public文件,使用以下命令查看內容:
$ cat key.public內容類似如下:
-----BEGIN PGP PUBLIC KEY BLOCK----- Version: GnuPG v2 ....... -----END PGP PUBLIC KEY BLOCK-----這樣你就可以把公鑰放在博客上,或者email給你的小伙伴。另一種方法是將公鑰發送至公鑰服務器:
把公鑰發布到公鑰服務器
$ gpg --keyserver keys.gnupg.net --send-key ID只需要將ID替換成你的公鑰ID。
以上說的是發布你的公鑰,這樣小伙伴就可以發送加密的文件或郵件給你,那么如何發送加密文件或郵件給小伙伴呢?這就需要獲取小伙伴的公鑰。
導入公鑰
$ gpg --keyserver keys.gnupg.net -recv-key 0xA0A2A2F80xA0A2A2F8是俺的公鑰id,替換成你的小伙伴的公鑰id
或者導入公鑰文件
$ gpg --import key.public由于公鑰加密的特點,公鑰的發布過程,和獲取公鑰的過程必須保證可靠性,因此有了指紋驗證:
驗證指紋并簽收公鑰
$ gpg --fingerprint輸出應該包含如下所示:
pub 2048R/A0A2A2F8 2014-07-09 Key fingerprint = DE91 71DC AE38 DE8E AB5A C423 3E6A 8CC1 A0A2 A2F8 uid [ultimate] ibrother (ibrother in github) ibrother.linux@gmail.com sub 2048R/4AAB15B9 2014-07-09
</blockquote>其中“DE91 71DC AE38 DE8E AB5A C423 3E6A 8CC1 A0A2 A2F8”就是這個公鑰的指紋,和小伙伴核對確認無誤后就可以簽收了:
$ gpg --sign-key ibrother加密文件
$ gpg -a --output message-ciper.txt -r ibrother -e message.txt參數說明:
- -a輸出文本格式
- --output指定輸出文件名
- -r知道接收者的公鑰uid
- -e表示執行加密操作
</ul>如果加密二進制文件不需要-a參數
解密文件
$ gpg --output message-plain.txt -d message-ciper.txt參數說明:
- --output指定解密后的文件名
- -d表示執行解密操作
</ul>簽名一個文件
獨立簽名文件簽名方式
$ gpg -a -b message.txt獨立簽名方式是原文件和簽名文件分開,可以用如下命令驗證簽名:
$ gpg --verify message.txt.asc輸出一定要有“Good signature”字樣,就說明驗證成功。
clear簽名方式
$ gpg -a --clearsign message.txt使用如下命令提取原始信息:
$ gpg --output message-original.txt -d message.txt.ascGnuPG和mutt的整合
關于mutt的初步配置,建議看俺之前的博文
一旦mutt安裝好后,在/usr/share/doc/mutt-1.5.22-r3/samples/目錄下可以找到
gpg.rc.bz2文件$ bzcat /usr/share/doc/mutt-1.5.22-r3/samples/gpg.rc.bz2 >> .mutt/gpg.rc $ echo echo "source ~/.mutt/gpg.rc" >> .muttrc然后編輯
~/.mutt/gpg.rc,編輯或者加入以下幾行set pgp_good_sign="^\[GNUPG:\] GOODSIG"set pgp_sign_as = 0xA0A2A2F8 set pgp_timeout = 3600 set crypt_autosign = yes set crypt_replyencrypt = yes</code></pre>
將0xA0A2A2F8改為你自己的公鑰id。這樣使用mutt發送郵件默認使用簽名。
在mutt的發信界面可以選擇其他選項:
輸入P
e表示只加密,s表示簽名,b表示既加密又簽名,c表示不進行加密和簽名
收信界面,有s的即標明簽名郵件
如果有公鑰的話,查看郵件,mutt會自動驗證簽名,給給出提示信息:
其他郵件客戶端支持
Linux用戶可以使用Thunderbird,KDE桌面用戶建議使用Kmail,Kmail對Gmail有更好的支持。
</li>OS X自家的Mail就可以支持gpg郵件加密
</li>Windows用戶推薦Thunderbird
</li> </ul>參考網站
- </li>
- </li>
- </li>
- </li>
- </li>
- </li>
- </li> </ol> 來自:http://my.oschina.net/ibrother/blog/296928



