Please enable Javascript to view the contents

GPG加密软件的使用

 ·   ·  ☕ 9 分钟

先来看 PGP 和 GPG 程序的介绍。

PGP

PGP(英语:Pretty Good Privacy,中文含义"良好隐私密码法")是一套用于消息加密、验证的应用程序

Phil Zimmermann 于 1991 年将 PGP 在互联网上免费发布。PGP 本身是商业应用程序;对应的开源软件为 GPG(GnuPG)。如今 PGP 软件属于 Symantec (赛门铁克公司) 公司。

PGP.cn PGP 中國:创建一个在中国可以让人信任的公钥发布/查询/下载网站,我们承诺,公钥发布/查询/下载的功能将永远免费。

GPG

GNU Privacy Guard (简称 GnuPG 或 GPG)也是一种加密软件,它是 PGP 加密软件的开源替代程序。GnuPG 是一个混合加密软件程序,可以使用多种非专利的算法。

安装

ubuntu 安装 gnupg:

1
$ sudo apt install gnupg

Windows用户在官网下载安装程序安装。

默认配置文件:

默认的配置文件是 ~/.gnupg/gpg.conf~/.gnupg/dirmngr.conf.

创建密钥对

1
$ gpg --gen-key

操作示例:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
[fan 18:58:33]~$ gpg --gen-key
gpg (GnuPG) 1.4.20; Copyright (C) 2015 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) 4096
您所要求的密钥尺寸是 4096 位
请设定这把密钥的有效期限。
         0 = 密钥永不过期
      <n>  = 密钥在 n 天后过期
      <n>w = 密钥在 n 周后过期
      <n>m = 密钥在 n 月后过期
      <n>y = 密钥在 n 年后过期
密钥的有效期限是?(0)
密钥永远不会过期
以上正确吗?(y/n) y

您需要一个用户标识来辨识您的密钥;本软件会用真实姓名、注释和电子邮件地址组合
成用户标识,如下所示:
    "Heinrich Heine (Der Dichter) <[email protected]>"

真实姓名: Fan
电子邮件地址: [email protected]
注释: github
您选定了这个用户标识:
    "Fan(github) <[email protected]>"

更改姓名(N)、注释(C)、电子邮件地址(E)或确定(O)/退出(Q)? O
您需要一个密码来保护您的私钥。

我们需要生成大量的随机字节。这个时候您可以多做些琐事(像是敲打键盘、移动
鼠标、读写硬盘之类的),这会让随机数字发生器有更好的机会获得足够的熵数。
....+++++

随机字节不够多。请再做一些其他的琐事,以使操作系统能搜集到更多的熵!
(还需要184字节)
+++++
我们需要生成大量的随机字节。这个时候您可以多做些琐事(像是敲打键盘、移动
鼠标、读写硬盘之类的),这会让随机数字发生器有更好的机会获得足够的熵数。

随机字节不够多。请再做一些其他的琐事,以使操作系统能搜集到更多的熵!
(还需要231字节)
...+++++

随机字节不够多。请再做一些其他的琐事,以使操作系统能搜集到更多的熵!
(还需要170字节)
+++++
gpg: 密钥 2DBA87CF 被标记为绝对信任
公钥和私钥已经生成并经签名。

gpg: 正在检查信任度数据库
gpg: 需要 3 份勉强信任和 1 份完全信任,PGP 信任模型
gpg: 深度:0 有效性:  1 已签名:  0 信任度:0-,0q,0n,0m,0f,1u
pub   4096R/2DBA87CF 2017-04-11
      密钥指纹 = 3C00 AC7B 3D06 E22E AEDE  72B0 B28F ACA4 2EBC 87DF
uid                  Fan (github) <[email protected]>
sub   4096R/873278A9 2017-04-11

后续操作

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# 最好制作一张撤销证书,用于密钥作废,请求外部公钥服务器撤销你的公钥
# 这里 2DBA87CF 为我的 key id 
gpg --gen-revoke 2DBA87CF

# 上传公钥到公共的公钥服务器。
$ gpg --send-keys 2DBA87CF

# 任何人都可以用你的名义上传公钥所以我们可以
# 生成用于公布的公钥指纹,然后进行公布,比如我在文末公布了自己的公钥指纹,好让他人校验
$ gpg --fingerpint 2DBA87CF
密钥指纹 = 3C00 AC7B 3D06 E22E AEDE  72B0 B28F ACA4 2EBC 87DF

用户ID 就是 uid ,比如上面输出的 uid Fan (github) <[email protected]>

而这里的 2DBA87CF 应该称作为:

  • 密钥ID
  • KEY_ID
  • 或者更确切的说,它是 MASTERKEYID (主key id)。

另外还有 SUBKEYID。
用户邮箱MASTERKEYID 在一些场合可以相互替换,但不是全部场合(被这个坑过)。

  • 用户名和电子邮件。可以给同样的密钥不同的身份,比如给同一个密钥关联多个电子邮件。
  • 任何导入密钥的人都可以看到这里的用户名和电子邮件地址。

常用命令

查看公钥

1
2
3
$ gpg --list-keys
# 或者
$ gpg -k
1
2
3
4
5
6
[fan 15:50:59]~/.gnupg$ gpg -k
/home/fan/.gnupg/pubring.gpg    # 公钥文件
----------------------------
pub   4096R/2DBA87CF 2017-04-11   # PUBlic key特征: 4096位,key id 和生成时间
uid                  Fan (for github) <[email protected]>  # 用户ID
sub   4096R/873278A9 2017-04-11  # public SUBkey特征

注意 : 输出中可以看到是 .gnupg/pubring.gpg 文件中的内容。

查看私钥:

1
2
3
$ gpg --list-secret-keys
# 或者
$ gpg -K
1
2
3
4
5
6
[fan 15:35:25]~/.gnupg$ gpg -K
/home/fan/.gnupg/secring.gpg      # 私钥文件
----------------------------
sec   4096R/2DBA87CF 2017-04-11
uid                  Fan (for github) <[email protected]>
ssb   4096R/873278A9 2017-04-11

注意: 输出中可以看到是 .gnupg/secring.gpg 文件中的内容。

一些缩写介绍:

sec => 'SECret key'
ssb => 'Secret SuBkey'
pub => 'PUBlic key'
sub => 'public SUBkey'

下面的命令中的 MASTERKEYID 可以直接替换成邮箱地址。

生成和使用撤销证书

生成撤销证书

1
2
3
4
5
# 二进制证书 revocation-gmail.cert; 但会提示 "已强行使用 ASCII 封装过的输出"
$ gpg --output revocation-gmail.cert --gen-revoke MASTERKEYID

# -a (--armor)输出文件为 revocation-gmail-cert.txt 文本文件
$ gpg -a -o revocation-gmail-cert.txt --gen-revoke MASTERKEYID

导出密钥并备份

可使用 -a 代替 --armor,使用 -o 代替 --output

导出公钥:

gpg --armor --output public-key-gmail.txt --export MASTERKEYID

导出私钥:

gpg --armor --output secret-key-gmail.txt --export-secret-keys MASTERKEYID

编辑命令

gpg --edit-key MASTERKEYID

当运行编辑命令时,出现的几个标记的介绍:

Constant           Character      Explanation
─────────────────────────────────────────────────────
PUBKEY_USAGE_SIG      S       key is good for signing
PUBKEY_USAGE_CERT     C       key is good for certifying other signatures
PUBKEY_USAGE_ENC      E       key is good for encryption
PUBKEY_USAGE_AUTH     A       key is good for authentication

可以看到

  • 最重要的主密钥显示:SC(“sign”&“certify”,代表可以签名和认证其它密钥)
  • 第一个副密钥显示:E(“encrypt”,加密)
  • 第二个副密钥显示:S(“sign”,签名)

具体操作见 将另一个电子邮件与您的 GPG 密钥相关联

将另一个电子邮件与您的 GPG 密钥相关联

将另一个电子邮件与您的 GPG 密钥相关联,示例:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
  $ gpg --edit-key <key-id>
  gpg> adduid
  Real Name: <name>
  Email address: <email>
  Comment: <comment or Return to none>
  Change (N)ame, (C)omment, (E)mail or (O)kay/(Q)uit? O
  Enter passphrase: <password>
  gpg> uid <uid>
  gpg> trust
  Your decision? 5
  Do you really want to set this key to ultimate trust? (y/N) y
  gpg> save

$ gpg --send-keys <key-id>

GPG: Change email for key in PGP key servers (Example)

在您的 GPG 密钥中使用经过验证的电子邮件地址。如果您需要更新或添加电子邮件地址到您的 GPG 密钥,请参阅: Associating an email with your GPG key - GitHub Enterprise 2.10 Documentation

Github GPG + Keybase PGP - Ahmad Nassri

gpg key 用途

常见的两种用途: 加密、签名(利用公钥進行加密和签名)

  • 對 Email 進行加密
  • 加密一些文本信息,比如(註冊信息),使用 PGP 加密這些信息后可將加密后的密文發佈到自己的 Blog 上,什麼時候需要就取回密文進行解密。
  • 簽名功能:可以利用 PGP 的簽名功能對你在網絡上的言論進行簽名,防止他人篡改你的原文。

对文字内容及文件进行加密、解密、签名的方法見該網站。 OpenPGP Symantec Encryption (PGP) Documentation 客户端/服务器架构。

在 GitHub 和 GitLab 中的使用

我们可以让Git使用gpg key对 提交或Tag进行签名

注意:在 GitHub 中使用的 gpg key 的邮件地址必须是经过 GitHub 认证过的邮箱地址

Your GPG key must be associated with a GitHub verified email that matches your committer identity.

设置 Signed commits

对单次提交进行签名,在提交时使用下面的命令:

1
2
# -S选项表示对此次提交使用gpg进行签名
git commit -S -m "提交信息"

为某个仓库或所有本地提交设置为签名提交:

  • 首先需要找到你的 key id :

    • 运行 gpg -K [email protected]
    • 找到以 sec开始的行
    • 斜杠后面的十六进制数字是 key ID,比如 ABCD0123
  • 将你的 key ID 添加到git的全局配置文件 .gitconfig:

    1
    2
    3
    4
    
    [user]
          name = you
          email = [email protected]
          signingkey = ABCD0123
    

    或使用运行命令: git config --global commit.gpgsign true 在 git 的全局配置文件中进行配置,这会导致所有提交都会进行签名。

  • 将下面的内容添加到GPG 的配置文件 .gnupg/gpg.conf 中:

    use-agent
    no-tty
    
  • 配置当前仓库中的所有提交默认都进行签名,打开仓库下的 .git/config文件,添加如下内容:

    1
    2
    
    [commit]
        gpgsign = true
    

    它和这个命令作用一样: git config commit.gpgsign true

补充

与 SSH KEY 的关系是什么?

GitHub 中,使用 SSH 连接到 GitHub 时可以使用 ssh key来进行加密连接;而 gpg key 用于认证每次提交。

我的具体操作就是按照 Authenticating to GitHub 所说的方法进行设置。

其中有一步, 是要告诉 Git 你的 GPG key id 是多少,步骤如下:

# 使用此命令列出我的key id,顾名思义LONG 这种形式的id 比一般的id要长
$ gpg --list-secret-keys --keyid-format LONG
# 我的是 B28FACA42EBC87DF

# 将其写入git配置
$ git config --global user.signingkey B28FACA42EBC87DF

而后面的两个命令,列出的 2EBC87DF 也都是 key id,只是比上面的短:

$ gpg -K [email protected]
sec   4096R/2EBC87DF 2017-04-11
...
# 或使用命令
$ gpg --list-keys
sec   4096R/2EBC87DF 2017-04-11

Signed commits

Tips 小贴士:

To set all commits for a repository to be signed by default, in Git versions 2.0.0 and above, run git config commit.gpgsign true. To set all commits in any local repository on your computer to be signed by default, run git config --global commit.gpgsign true.

To store your GPG key passphrase so you don’t have to enter it every time you sign a commit, we recommend using the following tools:

For Mac users, the GPG Suite allows you to store your GPG key passphrase in the Mac OS Keychain. For Windows users, the Gpg4win integrates with other Windows tools. You can also manually configure gpg-agent to save your GPG key passphrase, but this doesn’t integrate with Mac OS Keychain like ssh-agent and requires more setup.

gpg-agent

配置起来比较麻烦,这里只介绍,个人电脑个人用户的简单配置,在 gpg-agent.conf 文件中添加如下配置:

1
2
3
4
5
6
7
8
9
# 8小时:28800‬ , 12小时:43200‬ , 24小时 :86400  7天:604800

# 设置缓存的有效时间,默认为600秒
default-cache-ttl 604800
# 最大缓存时间
max-cache-ttl 604800

# 无人值守密码
allow-loopback-pinentry

将以下行添加到 .bashrc

1
export GPG_TTY=$(tty)

下面是具体的配置和解释;可以不看(感觉好啰嗦)。

1. 配置

提前备份的文件:

gpg-agent.conftrustlist.txt(This is the list of trusted keys. You should backup this file.)

手动停止gpg-agent: gpgconf --kill gpg-agent

在配置文件~/.gnupg/gpg-agent.conf中添加:

1
2
# 3600 = 60秒 × 60分钟 设置缓存的有效时间,默认为600秒。每次访问都重新开始计时,前提是没有超出最大缓存时间,该时间通过 max-cache-ttl设置默认为2小时
default-cache-ttl 3600

ignore-cache-for-signing (忽略所有缓存) This option will let gpg-agent bypass the passphrase cache for all signing operation. Note that there is also a per-session option to control this behavior but this command line option takes precedence.

2.重新加载 agent

更改配置后需要重新加载 agent。

1
$ gpg-connect-agent reloadagent /bye

该命令将会打印出 OK

3.pinentry

最后 agent 需要知道如何向用户索要密码,默认是使用一个 gtk dialog (gtk 对话框)。

~/.gnupg/gpg-agent.conf配置文件中,可以通过pinentry-program配置你要采用的程序:

1
2
3
4
5
6
# PIN entry program
# pinentry-program /usr/bin/pinentry-curses
# pinentry-program /usr/bin/pinentry-qt
# pinentry-program /usr/bin/pinentry-kwallet

pinentry-program /usr/bin/pinentry-gtk-2

仍然需要重新加载 agent

4.Start gpg-agent with systemd user(可选)

这里使用的是 archlinux

Create a systemd unit file:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
# 文件 ~/.config/systemd/user/gpg-agent.service 中
[Unit]
Description=GnuPG private key agent
IgnoreOnIsolate=true

[Service]
Type=forking
ExecStart=/usr/bin/gpg-agent --daemon
Restart=on-abort

[Install]
WantedBy=default.target

5.无人值守的密码短语(可选)

为了具有与旧版本相同类型的功能,必须完成两件事情:

  1. edit the gpg-agent configuration to allow loopback pinentry mode:

    1
    2
    
    # ~/.gnupg/gpg-agent.conf
    allow-loopback-pinentry
    

    然后重启 gpg-agent,以生效。

  2. 需要更新应用程序,最好使用命令行加参数的形式,来使用环回模式,如下:

1
$ gpg  --pinentry-mode loopback

如果这样不行,则尝试在配置文件中添加相应配置项:

1
2
# ~/.gnupg/gpg.conf
pinentry-mode loopback

gpg --pinentry-mode loopback命令不能执行,没有这个选项。后面的没有做了。配置了前面的已经可以了。

My PGP PUBLIC KEY

Fingerprint=683D ABB1 ABD1 6E7B 04A4  1284 6C98 8F2F 8B35 D6D7

学习资料

推荐的文章:

另参考:

您的鼓励是我最大的动力
alipay QR Code

Felix
作者
Felix
如无必要,勿增实体。

3

目录