天天看点

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”并按回车键执行,然后输入“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”(永不过期)并按回车键确定。
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
           

继续阅读