GPG簡介
GPG是自由軟件基金會依照由IETF訂定的OpenPGP技術標準設計出了GNU Privacy Guard(GnuPG或GPG)。GPG是滿足GPL的PGP加密軟件的替代物,用于加密、數字簽名及產生非對稱匙對的軟件。
PGP是(英語:Pretty Good Privacy,中文含義“良好隱私密碼法”)是一套用于消息加密、驗證的應用程序,采用IDEA的散列算法作為加密與驗證之用。 PGP的主要開發者是菲爾·齊默爾曼(Phil Zimmermann)。齊默曼于1991年將PGP在互聯網上免費發布。OPENGPG是1997年7月,PGP Inc.與齊默爾曼同意IETF制定一項公開的互聯網標準,稱作OpenPGP,任何支持這一標準的程序也被允許稱作OpenPGP。
GPG常用步驟
1.安裝GPG軟件包
2.生成密鑰
3.密鑰管理
4.導入導出密鑰
1.安裝GPG軟件包
Debian系列發行版:
apt install gnupg //下載并安裝gnupg軟件包
Redhat系列發行版:
yum install gunpg //下載并安裝gnupg軟件包
2.對稱加密及解密
gpg -c myfile //對稱方式加密
這樣,會提示你輸入兩次密碼,然后生成myfile的加密文件myfile.gpg,注意不能對目錄加密,可以用-o選項指定輸出文件名稱。
解密對稱加密的文件:
gpg -o mydecrypt -d myfile.gpg //對稱方式解密
這樣,會提示你輸入解密密碼,輸入之后,將會解密相應的文件,通過-o指定生成的解密文件,-d指定被解密的文件。
3.生成密鑰
gpg --gen-key //生成密鑰(交互式)
[root@localhost ~]# gpg --gen-key
gpg (GnuPG) 2.0.22; Copyright (C) 2013 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.
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? 1
RSA keys may be between 1024 and 4096 bits long.
What keysize do you want? (2048) 2048
Requested keysize is 2048 bits
Please specify how long the key should be valid.
0 = key does not expire
<n> = key expires in n days
<n>w = key expires in n weeks
<n>m = key expires in n months
<n>y = key expires in n years
Key is valid for? (0) 0
Key does not expire at all
Is this correct? (y/N) y
GnuPG needs to construct a user ID to identify your key.
Real name: geekdeedy
Email address: geekboy@qq.com
Comment:
You selected this USER-ID:
"geekdeedy <geekboy@qq.com>"
Change (N)ame, (C)omment, (E)mail or (O)kay/(Q)uit? o
You need a Passphrase to protect your secret key.
We need to generate a lot of random bytes. It is a good idea to perform
some other action (type on the keyboard, move the mouse, utilize the
disks) during the prime generation; this gives the random number
generator a better chance to gain enough entropy.
GPG密鑰生成步驟:
- 選擇加密算法 (默認為1)
- 選擇密鑰長度 (默認為2048)
- 選擇密碼的有效期(默認值為0,表示永不過期)
- 確認上述信息是否正確 (確認y不確認n)
- 輸入個人信息(姓名、郵箱、說明;說明可以為空)
- 確認個人信息是否正確(O確認,Q退出,N修改用戶名,C修改說明,E修改郵箱)
- 使用口令加密私鑰
- 然后軟件說“需要生成大量隨機字節”,你可以隨意進行一些操作,敲擊鍵盤、晃動鼠標等。
- 生成密鑰
最后,建議再生成一個撤銷證書,以便以后密鑰作廢時,請求公鑰服務器撤銷你的公鑰:
gpg --ken-revoke [uid] //請求服務器生成撤銷證書
4.管理密鑰
列出密鑰
gpg --list-keys //列出密鑰
[root@localhost ~]# gpg --list-keys
/root/.gnupg/pubring.gpg
------------------------------------
pub 2048R/5F40F244 2017-07-19
uid geekdeedy <geekboy@qq.com>
sub 2048R/7D1E46CE 2017-07-19
//第一行是公鑰文件路徑,第二行是公鑰信息,第三行是用戶ID,第四行是私鑰信息。
刪除密鑰
gpg --delete-secret-keys [uid] //刪除這個uid的私鑰(刪除時必須先刪除私鑰然后才能刪除公鑰)
gpg --delete-keys [uid] //刪除這個uid的公鑰
刪除私鑰
[root@localhost ~]# gpg --delete-secret-keys geekdeedy
gpg (GnuPG) 2.0.22; Copyright (C) 2013 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/A2AB8C54 2017-07-19 geekdeedy (this is comment) <geekboy@qq.com>
Delete this key from the keyring? (y/N) y
This is a secret key! - really delete? (y/N) y
刪除公鑰
[root@localhost ~]# gpg --delete-keys geekdeedy
gpg (GnuPG) 2.0.22; Copyright (C) 2013 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/A2AB8C54 2017-07-19 geekdeedy (this is comment) <geekboy@qq.com
Delete this key from the keyring? (y/N) y
//刪除密鑰成功是沒有提示的,沒有消息就是最好的消息。
導入密鑰
gpg --import [密鑰文件] //導入密鑰文件中的密鑰
導出密鑰
導出公鑰
gpg -omypubkey --export [uid] //二進制方式導出公鑰(默認)
gpg -a -omypubkeyascii --export [uid] //以文本方式導出公鑰
導出私鑰
gpg -o mysubkey --export-secret-keys [uid] //以二進制方式導出私鑰(默認)
gpg -o mysubkey --export-secret-keys [uid] //以文本方式導出私鑰
密鑰默認以二進制方式導出私鑰
如果沒KeyID則是備份所有的私鑰,-o表示輸出到文件mysubkey中,如果加上-a的參數則輸出文本格式的信息,否則輸出的是二進制格式信息。
非對稱加密及解密
加密前查看文件內容
[root@localhost ~]# ls
test
[root@localhost ~]# cat test
hello this is earth,welcome
加密
[root@localhost ~]# gpg -ea -r geekdeedy test(-e表示加密、-a表示以ascii格式輸出、-r表示指定密鑰)
加密后查看文件內容
[root@localhost ~]# ls
test test.asc
[root@localhost ~]# cat test.asc //使用-a選項表示以ASCII編碼輸出,后綴以asc結尾,默認是以gpg結尾
-----BEGIN PGP MESSAGE-----
Version: GnuPG v2.0.22 (GNU/Linux)
hQEMA4YGEWB9HkbOAQgAgqrpnYkoW/y0SUZkXtMrHnTnHmQj4WgMNC+g5sqAhg9L
SvTmD0UdJYU5QJJOqgJF9DcWLU6H8M6KnQU9m1aGvzmZLR5fWKlh0qJG5jojOVGa
rfpaATozUJrwSRZUyh3OoEP28K39jmhBoVLpyUyYyV3VNBPrDIOwqBAj62DYbCrY
UCRNX7/ozcWq4h/8Gq/oVH3b2hDGD6++CQBfuvI4N+ckEh4U1JNZQkHT7urL38ec
O+Hcs3Xu9NvwiW1HY25+4i+bldDJrHkueLTcg9xxdW50MQj2pjPuIohWEYLrsYqH
6AUJEFWnLuHkuhQygo+q7cErJqaV+Zj6df/HxIkfNdJbAelbiQVIMWdBkSwI91qj
v0cyTtM1G39WC14u5ZoeUxoxkDWjzIVCuh0Z8SLDRYhaUEz8rlqJE/zRB7VozRya
2T5luUZ1wGGdtCt4e0OSbQN40VeNEER5PI3M5w==
=GmS0
-----END PGP MESSAGE-----
//使用gpg密鑰加密過的test數據
解密
[root@localhost ~]# gpg -o test2 -d test.asc //-o 表示解密后輸出文件名 -d 表示需要解密的文件,解密需要私鑰密碼
You need a passphrase to unlock the secret key for
user: "geekdeedy <geekboy@qq.com>"
2048-bit RSA key, ID 7D1E46CE, created 2017-07-19 (main key ID 5F40F244)
gpg: encrypted with 2048-bit RSA key, ID 7D1E46CE, created 2017-07-19
"geekdeedy <geekboy@qq.com>"
[root@localhost ~]# ls
test test2 test.asc
[root@localhost ~]# cat test2
hello this is earth,welcome
//此時就能看到加密前的數據了
簽名與驗證
簽名作用是驗證明文、加密文件、密鑰是來自正確的發送者的,沒經過其它人的修改。簽名使用的也是密鑰對,與加密操作相同。只是在結果上,點不同。它只是在文件最后添上加密的驗證信息(簽名)。一旦文件有所改變,簽名驗證就會出錯。比如我們ubuntu安裝軟件時,首先要用事先保存的密鑰(從可信的服務器上下載)驗證軟件源的簽名,以保證我們連的是正確的安全的下載服務器。
gpg -o test.sig -s test //產生二進制簽名
gpg -o test.sig2 -clearsign test //產生文本格式簽名
簽名過程中都需要加密
gpg -o test.sig -ser geekdeedy test //簽名并解密
gpg -o test3 --decrypt test.sig //恢復加密的簽名文件
gpg -o test4.sig --ab test //分離式簽名,不包含原文件數據
gpg --verify test4.sig test //驗證分離式簽名和原文件數據
[其他]
編輯公鑰
可以對公鑰進行編輯,這里沒具體實踐,大致過程如下:
#gpg --edit-key [uid] //uid是別人的用戶id,輸入之后,出現命令提示符號。
fpr <===輸入這個表示查看uid的指紋,核對信息真實性,這樣之后簽署。
sign <===輸入這個簽署公鑰,這樣以后再使用它加密的時候不會產生警告了。
check <===輸入這個,檢查uid已有的鑰匙的簽名。
list //使用list可以看到當前用戶的 密鑰及用戶標識
adduid //添加一個用戶標識
quit <===輸入這個,退出交互,可能會提示你保存之前的設置。
revuid 使用注銷命令進行標識的注銷。