天天看點

Linux下使用GPG(GnuPG)加密及解密檔案

文章目錄

        • Linux下使用GPG(GnuPG)加密及解密檔案
          • 1. 簡介
          • 2.環境及版本
          • 3.GPG公鑰生成
          • 4.檢視公鑰
          • 5.檢視私鑰
          • 6.導出公鑰
          • 7.導出私鑰
          • 8.加密檔案
            • 本機加密
            • 其他電腦加密
          • 9.解密檔案
            • 本機解密
            • 其他電腦解密
          • 10.解除安裝密鑰對

Linux下使用GPG(GnuPG)加密及解密檔案

1. 簡介
GNU Privacy Guard(GnuPG或GPG)是一種加密軟體,它是PGP加密軟體的滿足GPL的替代物。GnuPG依照由IETF訂定的OpenPGP技術标準設計。GnuPG用于加密、數字簽名及産生非對稱鑰匙對的軟體。
2.環境及版本

系統 centos 7

核心版本:3.10.0-693.el7.x86_64

gpg版本:2.0.22

3.GPG公鑰生成
1.由于預設已經安裝GPG,故直接輸入“gpg --gen-key”并按Enter鍵執行,然後輸入“1”選擇密鑰種類,然後回車。
[[email protected] ~]$ 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
           
2.詢問密鑰對位數,預設為2048,本例中手動輸入1024.
RSA keys may be between 1024 and 4096 bits long.
What keysize do you want? (2048) 1024
           
  1. 提示輸入密鑰對有效期,輸入“0”(永不過期)并按Enter鍵确定。
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
           
  1. 提示将永不過期,輸入“y”确認。
Key does not expire at all
Is this correct? (y/N) y
           
  1. 輸入密鑰對名稱(本例中為cheshi)。
GnuPG needs to construct a user ID to identify your key.
Real name: cheshi
           
  1. 依次輸入郵件位址和備注。
Real name: cheshi
Email address: [email protected]
Comment: NA
You selected this USER-ID:
    "cheshi (NA) <[email protected]>"
           
  1. 詢問是否修改或者确認,直接輸入“o”确認。
Change (N)ame, (C)omment, (E)mail or (O)kay/(Q)uit? o

           
  1. 提示您需要設定私鑰。
    Linux下使用GPG(GnuPG)加密及解密檔案
  2. 再次輸入私鑰。
    Linux下使用GPG(GnuPG)加密及解密檔案
  1. 系統需要時間生成密鑰對,期間最好随機輸入字元或移動滑鼠等,有助于生成密鑰對,結果如下圖。
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.
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: key 70E8A292 marked as ultimately trusted
public and secret key created and signed.


gpg: checking the trustdb
gpg: 3 marginal(s) needed, 1 complete(s) needed, PGP trust model
gpg: depth: 0  valid:   1  signed:   0  trust: 0-, 0q, 0n, 0m, 0f, 1u
pub   1024R/70E8A292 2019-06-05
      Key fingerprint = C54E 7282 49B7 51E8 00BB  DCFA B8BD F821 70E8 A292
uid                  cheshi (NA) <[email protected]>
sub   1024R/26A8DC48 2019-06-05
           
4.檢視公鑰
[[email protected] ~]$ gpg --list-key
/home/db4/.gnupg/pubring.gpg
----------------------------
pub   1024R/70E8A292 2019-06-05
uid                  cheshi (NA) <[email protected]>
sub   1024R/26A8DC48 2019-06-05
           
5.檢視私鑰
[[email protected] ~]$ gpg --list-secret-keys
/home/db4/.gnupg/secring.gpg
----------------------------
sec   1024R/70E8A292 2019-06-05
uid                  cheshi (NA) <[email protected]>
ssb   1024R/26A8DC48 2019-06-05
           
6.導出公鑰
1.指令格式
gpg -a --export 公私鑰生成的使用者 > 導出的公鑰檔案路徑和檔案名
           
2.指令樣式
[[email protected] ~]$ gpg -a --export ceshi > ceshi6666.asc
在目前的路徑下面會生成一個ceshi6666.asc的公鑰檔案
           
3.備注
将上面的這個公鑰檔案ceshi6666.asc給需要加密的伺服器一方即可
7.導出私鑰

1.指令格式

gpg -a --export-secret-keys 公私鑰生成的使用者 >導出的私鑰檔案路徑和檔案名
           

2.指令樣式

[[email protected] ~]$ gpg -a --export-secret-keys ceshi > scheshi6666.asc
目前路徑下面會生成一個私鑰的檔案scheshi6666.asc
           
8.加密檔案

本機加密

輸入“gpg --encrypt --recipient “cheshi” 123455”指令,意為使用公鑰(名稱為cheshi)加密名稱為“123455”的檔案。
[[email protected] ~]$ gpg --encrypt --recipient "cheshi" 123455

           
該使用者加密完畢後發現同目錄下生成一個名為“原檔案名.gpg”的檔案(本例中為123455.gpg)
[[email protected] ~]$ ls 1*
123455  123455.gpg
           
該使用者輸入“vim 123455.gpg”嘗試檢視加密檔案,顯示為亂碼,将此加密檔案(123455.gpg)發送給給你公鑰的人。
[[email protected] ~]$ vim 123455.gpg
84><8c>^CÏÍÏ<9e>&¨ÜH^A^Cý^_;<8a><86>¼U^[í^L2½àÉ^]²Bº'_<88>"ÎÖ^N^_^M^^Hì^_M<99>htH<÷T^O^U^_{ð0ÿÐh7wA¢s^P$:<88>Ô*á<92>ÀeB÷ü|õ<86>¯<89><99>^Wþ!ê^O^Dk-^C3^ZFpÞ^R<8e>ó÷²ì<9e>F­fTbÙ´Q}ce$<98>^P!Àÿ·{<96>ä)Üí<8f>xv­'<97><8f>ÿSñ<83><8c>Òq^A&ùv§ÛûÈìlû<89>Þ^@¹´¤¥ä÷Ë¡«Æ9ëRÐ(\g¶}4îÊùM)ð^\åJ^RgOX^O^[l©vZ^S^Y<8a>X 0Áü^?<8c>kÔXÚ&^Rßç^Nº^F^NDÒí<85>Æ^@_só^HÊÿ/qE}Å<80>^Pª³×^V<98>KM<88>E¸^A©³Ö^T#^DG^Z
~  
           

其他電腦加密

導入公鑰

該使用者接收到公鑰後,在指令行輸入“gpg --import ceshi6666.asc”導入公鑰。
[[email protected] 模闆]$ gpg --import ceshi6666.asc
gpg: 密鑰 70E8A292:公鑰“cheshi (NA) <[email protected]>”已導入
gpg: 合計被處理的數量:1
gpg:           已導入:1  (RSA: 1)
           

加密檔案

該使用者輸入“gpg --encrypt --recipient “cheshi” 123455”指令,意為使用公鑰(名稱為cheshi)加密名稱為“123455”的檔案。 系統提示您是否确認公鑰指紋正确,在導入他人的公鑰前建議詳細核對,輸入“y”繼續。
[[email protected] 模闆]$ gpg --encrypt --recipient "cheshi" 123455
gpg: 26A8DC48:沒有證據表明這把密鑰真的屬于它所聲稱的持有者


pub  1024R/26A8DC48 2019-06-05 cheshi (NA) <[email protected]>
主鑰指紋: C54E 7282 49B7 51E8 00BB  DCFA B8BD F821 70E8 A292
子鑰指紋: D8ED AE06 C208 D378 E562  12B9 CFCD CF9E 26A8 DC48


這把密鑰并不一定屬于使用者辨別聲稱的那個人。如果您真的知道自
己在做什麼,您可以在下一個問題回答 yes。


無論如何還是使用這把密鑰嗎?(y/N)y
           
該使用者加密完畢後發現同目錄下生成一個名為“原檔案名.gpg”的檔案(本例中為123455.gpg)
[[email protected] 模闆]$ ls
123455  123455.gpg  
           
該使用者輸入“vim 123455.gpg”嘗試檢視加密檔案,顯示為亂碼,将此加密檔案(123455.gpg)發送給給你公鑰的人。 注意:在隻有公鑰的情況下,加密後的檔案無法讀取。
[[email protected] 模闆]$ vim 123455.gpg

<84><8c>^CÏÍÏ<9e>&¨ÜH^A^Cÿ@PÎ,Ló<98>^Lo<9e>^Yú^L츩^D?6}<8b> ^W^@^E/·x^Pã{^][email protected]ÜÌ,h5`^]^[ǧ[<86>.^XHïZ<8f>@ªsBÊ΢ÖnúA<98>ÍL×A^GìW³ L
<8e>ÂöZýYEN¬ë<80>®dÅÕ^_ì^O<97>î^F«AUÍ<8d>r3Xø^C<8c>ÙÙÊ^CÅ^Q^BFO΢ ¦p<8e>h#ävÒq^ADF^_>×<8f>«     1ÇìP.ÍSUyùV<85>LÄû^PÀb=^QN^R^Q½ãJ{{CH;^\Z^\<99>1Ï)üe ¤"<9f>vk¢ ó,¦È¶6^Zqxù^_¢{O_Éí<80>xª <98>@WÇù¹FO<94>è?¦DáÊ "<9f>;åÈ^^<91>6"<9a>ñ^Y¦/S<8b><8d>t9
           
9.解密檔案

本機解密

輸入“gpg --decrypt 123455.gpg > 123455”将加密檔案解密。提示輸入私鑰密碼,輸入正确的私鑰密碼後解密成功。
[[email protected] ~]$ gpg --decrypt 123455.gpg > 123455


You need a passphrase to unlock the secret key for
user: "cheshi (NA) <[email protected]>"
1024-bit RSA key, ID 26A8DC48, created 2019-06-05 (main key ID 70E8A292)


gpg: encrypted with 1024-bit RSA key, ID 26A8DC48, created 2019-06-05
      "cheshi (NA) <[email protected]>"


           

其他電腦解密

導入公鑰

将私鑰導出發給需要解密的使用者,解密的使用者執行”gpg --import 私鑰名稱“,将私鑰導入電腦中
[[email protected] 模闆]$ gpg --import scheshi6666.asc
gpg: 密鑰 70E8A292:私鑰已導入
gpg: 密鑰 70E8A292:“cheshi (NA) <[email protected]>”未改變
gpg: 合計被處理的數量:1
gpg:           未改變:1
gpg:       讀取的私鑰:1
gpg:       導入的私鑰:1
           
輸入“gpg --decrypt 123455.gpg > 123455”将加密檔案解密。提示輸入私鑰密碼,輸入正确的私鑰密碼後解密成功。
[[email protected] 模闆]$ gpg --decrypt 123455.gpg > 123455


您需要輸入密碼,才能解開這個使用者的私鑰:“cheshi (NA) <[email protected]>”
1024 位的 RSA 密鑰,鑰匙号 26A8DC48,建立于 2019-06-05 (主鑰匙号 70E8A292)


gpg: 由 1024 位的 RSA 密鑰加密,鑰匙号為 26A8DC48、生成于 2019-06-05
      “cheshi (NA) <[email protected]>”
           
Linux下使用GPG(GnuPG)加密及解密檔案
輸入“vim 123455”檢視此檔案,可正常浏覽及編輯。
[[email protected] 模闆]$ vim 123455
dgsbvbsgf v
bgsfd vxsgbf
xvc
bgsfd xvc
s vfbxc
sf
xvc
gbsf v
~        
           
10.解除安裝密鑰對
如需解除安裝密鑰對,輸入“gpg --delete-secret-keys cheshi”解除安裝私鑰(必須先解除安裝私鑰,然後才可解除安裝公鑰)。
[[email protected] 模闆]$ gpg --delete-secret-keys cheshi
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  1024R/70E8A292 2019-06-05 cheshi (NA) <[email protected]>


要從鑰匙環裡删除這把密鑰嗎?(y/N)y
這是一把私鑰!――真的要删除嗎?(y/N)y
           
繼續輸入“gpg --delete-keys cheshi”解除安裝公鑰。
[[email protected] 模闆]$ gpg --delete-keys cheshi
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  1024R/70E8A292 2019-06-05 cheshi (NA) <[email protected]>


要從鑰匙環裡删除這把密鑰嗎?(y/N)y

           
檢視,輸入”gpg --list-key“,裡面為空。
[[email protected] 模闆]$ gpg --list-key
           

繼續閱讀