加密工具 GPG
GPG(GNU Privacy Guard,GNU隐私卫士)是一种开源的加密工具,用于保护数据的隐私和安全。它是PGP(Pretty Good Privacy,相当好的隐私)的免费实现版本,由Werner Koch于1997年首次发布,遵循GNU通用公共许可证(GPL)。GPG广泛应用于文件加密、电子邮件加密与签名,以及数据完整性验证等领域。
# GPG的主要功能
- 加密与解密 GPG使用公钥加密(非对称加密)机制。用户拥有一个公钥(公开分享)和一个私钥(保密保存)。发送者用接收者的公钥加密数据,只有拥有对应私钥的人才能解密。这种方式确保了数据在传输过程中的安全性。
- 数字签名 GPG允许用户对文件或消息进行数字签名,使用私钥生成签名,接收者可以用发送者的公钥验证签名的真实性。这可以证明数据来源的合法性,并确保内容未被篡改。
- 密钥管理 GPG支持生成、管理和分发密钥对。用户可以创建自己的密钥对,并通过密钥服务器(如keys.openpgp.org)共享公钥,同时保护私钥的安全。
- 跨平台支持 GPG可在多种操作系统上运行,包括Windows、macOS和Linux,具有很强的兼容性。
# 工作原理
GPG结合了对称加密和非对称加密的优点:
- 对称加密用于快速加密实际数据(如文件或消息),生成一个临时会话密钥。
- 非对称加密(基于RSA或DSA等算法)用于加密会话密钥,确保只有目标接收者能解密。
- 常见的加密算法包括AES(对称)和RSA(非对称)。
# 非对称加密简介
在学习使用GPG之前,必须要了解非对称加密的基本原理。
非对称加密通过使用加密算法生成一对密钥(公钥和私钥)来确保数据安全。任何人都可以使用公钥加密数据,但只有拥有正确私钥的人才能解密并读取数据。
公钥用作加密,私钥则用作解密。使用公钥把明文加密后所得的密文,只能用相对应的私钥才能解密并得到原本的明文,最初用来加密的公钥不能用作解密。由于加密和解密需要两个不同的密钥,故被称为非对称加密
公钥可以公开,可任意向外发布;私钥不可以公开,必须由用户自行严格秘密保管,绝不透过任何途径向任何人提供,也不会透露给被信任的要通信的另一方。
# 加密
如果任何人使用公钥加密明文,得到的密文可以透过不安全的途径(如网络)发送,只有对应的私钥持有者才可以解密得到明文;其他人即使从网络上窃取到密文及加密公钥,也无法(在数以年计的合理时间内)解密得出明文。
# 数字签名
相反,如果某一用户使用他的私钥加密明文,任何人都可以用该用户的公钥解密密文;由于私钥只由该用户自己持有,故可以肯定该文件必定出自于该用户;公众可以验证该用户发布的数据或文件是否完整、中途有否曾被篡改,接收者可信赖这些数据、文件确实来自于该用户,这被称作数字签名。
总结:
公钥用于加密数据或验证数字签名,可以自由分发和共享。
私钥用于解密数据和创建数字签名,但必须保密,以确保安全。
# GPG 说明
# 名词
简写 | 含义 | |
---|---|---|
key pair | 密钥对(包含两部分) | |
primary key [master key] | 主密钥(包括主公钥,主私钥) | |
public key | pub | 公钥 |
secret key | sec | 私钥 |
sub-key | sub | 子公钥 |
secret sub-key | ssb | 子私钥 |
key fingerprint | 密钥指纹 |
# 功能
标识 | 含义 | 作用 |
---|---|---|
S | sign | 签名功能 |
E | encrypt | 加密功能 |
A | authenticate | 身份验证功能,在其他协议中用于身份验证,例如SSH TLS。只能通过 --expert 选项制作具有该功能的子公钥。 |
C | certify | 认证,主密钥必备能力,且只有主密钥具有 |
私钥的功能:认证[C]、签名[S]、解密
公钥的功能:验证签名、加密[E]
# 加密与签名
加密过程: 对方使用你的公钥加密数据后发给你;这些加密后的数据只有你的私钥能解密。
签名的过程:
先使用摘要算法(例如:SHA-256)给数据提取一个指纹(摘要、哈希值);
然后用私钥,将这串哈希值加密,得到一个数字签名,和文件一起发出去;
对方收到文件和签名后,先计算文件的哈希值;然后用你的公钥,从数字签名中解密出你文件的哈希值,两相对比,如果一致,那么签名就是有效的。
我们可以发现在这两个过程中,私钥的所用是相反的:在加密中,私钥用于解密;而在签名中,私钥用于加密。
GPG 密钥的能力中, [C]、[S]、[A] 均属于签名方案,只有 [E] 是加密方案。
# GPG 概念
GPG 的吊销证书
如果你的 SSH 登录私钥丢了,你只需要告诉对应服务器就行了,让服务器删除相应公钥。 除非你的组织要求很严格,否则你应该不太想在 SSH 上倒腾有效期、吊销证书、短期证书这些问题。
而 GPG 的公钥因为需要被广为人知,所以一旦私钥丢了就很麻烦,得通知所有人“我这个公钥不能信了”。 因此 GPG 多了一个“吊销证书”(revocation certificate),拿着这个证书你就能证明“我就是我,我的身份证丢了,你们赶紧把丢了的那个作废”。 而其他人呢,看到你在原来的证书基础之上,又发布了这么一张吊销证书,就知道原来的证书已经不可信了。
GPG 的过期时间
GPG 密钥对可以设置“过期时间”,也可以永久有效,你可以随便设置。 当你发布公钥的时候,里面就包含了过期时间信息。 别人也就知道,在这个时间过后,该证书的签名是无效的。
很多人会选择一个懒人方案:给主密钥设置一个很长的过期时间,给子密钥设置一个较短的过期时间。
GPG 的子密钥
GPG 非常明智地设计了 subkey 机制,让用户免去给自己设计证书信任链的麻烦,我们先看个例子:

- 主私钥必须有【认证 [C]】这个能力,且这项能力只能属于主私钥。
- 主私钥可以同时拥有【认证 [C]】、【签名 [S]】、【身份验证 [A]】三项能力。
- 子私钥可以同时拥有【签名 [S]】、【身份验证 [A]】两项能力。
- 【加密 [E]】能力必须属于独立的子私钥,因为加密用的是不同的算法变体(和签名不一样)。
一个 GPG 密钥对之所以可以有这么多能力,是因为它本质上是若干密钥对的集合,只不过它们被封装到了一起。所有子密钥的有效性,都来自于主密钥的认证。子密钥可以在没有主密钥的情况下单独使用。
子密钥就像主密钥的“助手”,分担具体任务,提升安全性和管理效率。
GPG 的用户身份(UID)
一个 uid 由三个部分组成:
- 全名(一般是实名)
- 注释(用 ( ) 包括)
- 邮箱地址(用 < > 包括)
这三个部分都是可选的,只要有一项即可。
# 安装GPG
GPG 可以在大多数发行版的软件库中找到,开箱即用。
sudo apt-get install gnupg
sudo yum install gnupg
// Make sure to add code blocks to your code group
# 生成密钥
# 生成主密钥
gpg --full-generate-key
会出现,如下
gpg (GnuPG) 2.4.4; Copyright (C) 2024 g10 Code GmbH
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
(2) DSA and Elgamal
(3) DSA (sign only)
(4) RSA (sign only)
(9) ECC (sign and encrypt) *default*
(10) ECC (sign only)
(14) Existing key from card
Your selection?
Please select which elliptic curve you want:
(1) Curve 25519 *default*
(4) NIST P-384
(6) Brainpool P-256
Your selection?
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
第一段是版权声明,然后让用户自己选择加密算法。默认选择ECC(椭圆曲线)。
第二段让我们选择椭圆曲线。选择默认。
接下来设定密钥有效期。
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
2
3
4
5
6
7
8
Is this correct? (y/N) y
GnuPG needs to construct a user ID to identify your key.
Real name: Zhou Chuang
Email address: zhouchuang0905@gmail.com
Comment: It's for learn gpg.
You selected this USER-ID:
"Zhou Chuang (It's for learn gpg.) <zhouchuang0905@gmail.com>"
Change (N)ame, (C)omment, (E)mail or (O)kay/(Q)uit? O
2
3
4
5
6
7
8
9
10
11
"真实姓名"填入你姓名的英文写法,"电子邮件地址"填入你的邮件地址,"注释"这一栏可以空着。 然后,你的"用户ID"生成了。
接着,系统会让你设定一个私钥的密码。这是为了防止误操作,或者系统被侵入时有人擅自动用私钥。 然后,系统就开始生成密钥了,这时会要求你做一些随机的举动,以生成一个随机数:
稍后密钥就生成成功了:
gpg: revocation certificate stored as '/home/zhou/.gnupg/openpgp-revocs.d/A25E747A3E4B946FD8B313AE79AAAAA6C3E5A7E5.rev'
public and secret key created and signed.
pub ed25519 2025-03-18 [SC]
A25E747A3E4B946FD8B313AE79AAAAA6C3E5A7E5
uid Zhou Chuang (It's for learn gpg.) <zhouchuang0905@gmail.com>
sub cv25519 2025-03-18 [E]
2
3
4
5
6
7
上面的字符串:A25E747A3E4B946FD8B313AE79AAAAA6C3E5A7E5
,这是"用户ID"的Hash字符串(指纹),可以用来替代"用户ID"。
这时,最好再生成一张"撤销证书",以备以后密钥作废时,可以请求外部的公钥服务器撤销你的公钥。
$ gpg --gen-revoke [用户ID]
# 生成子密钥
# 创建具有签名功能的子密钥【S】
点击查看
❯ gpg --expert --edit-key A25E747A3E4B946FD8B313AE79AAAAA6C3E5A7E5
gpg (GnuPG) 2.4.4; Copyright (C) 2024 g10 Code GmbH
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Secret key is available.
gpg: checking the trustdb
gpg: marginals needed: 3 completes needed: 1 trust model: pgp
gpg: depth: 0 valid: 2 signed: 0 trust: 0-, 0q, 0n, 0m, 0f, 2u
gpg: next trustdb check due at 2028-03-16
sec ed25519/79AAAAA6C3E5A7E5
created: 2025-03-18 expires: never usage: SC
trust: ultimate validity: ultimate
ssb cv25519/4F92D94E32CA3934
created: 2025-03-18 expires: never usage: E
[ultimate] (1). Zhou Chuang (It's for learn gpg.) <zhouchuang0905@gmail.com>
gpg> addkey
Please select what kind of key you want:
(3) DSA (sign only)
(4) RSA (sign only)
(5) Elgamal (encrypt only)
(6) RSA (encrypt only)
(7) DSA (set your own capabilities)
(8) RSA (set your own capabilities)
(10) ECC (sign only)
(11) ECC (set your own capabilities)
(12) ECC (encrypt only)
(13) Existing key
(14) Existing key from card
Your selection? 4
RSA keys may be between 1024 and 4096 bits long.
What keysize do you want? (3072)
Requested keysize is 3072 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)
Key does not expire at all
Is this correct? (y/N) y
Really create? (y/N) y
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.
sec ed25519/79AAAAA6C3E5A7E5
created: 2025-03-18 expires: never usage: SC
trust: ultimate validity: ultimate
ssb cv25519/4F92D94E32CA3934
created: 2025-03-18 expires: never usage: E
ssb rsa3072/B96E3DC939D57247
created: 2025-03-18 expires: never usage: S
[ultimate] (1). Zhou Chuang (It's for learn gpg.) <zhouchuang0905@gmail.com>
gpg> save
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
查看是否创建成功:
❯ gpg -k
/home/zhou/.gnupg/pubring.kbx
-----------------------------
pub ed25519 2025-03-18 [SC]
A25E747A3E4B946FD8B313AE79AAAAA6C3E5A7E5
uid [ultimate] Zhou Chuang (It's for learn gpg.) <zhouchuang0905@gmail.com>
sub cv25519 2025-03-18 [E]
sub rsa3072 2025-03-18 [S]
2
3
4
5
6
7
8
# 创建具有验证功能的子密钥【A】
点击查看
❯ gpg --expert --edit-key A25E747A3E4B946FD8B313AE79AAAAA6C3E5A7E5
gpg (GnuPG) 2.4.4; Copyright (C) 2024 g10 Code GmbH
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Secret key is available.
sec ed25519/79AAAAA6C3E5A7E5
created: 2025-03-18 expires: never usage: SC
trust: ultimate validity: ultimate
ssb cv25519/4F92D94E32CA3934
created: 2025-03-18 expires: never usage: E
ssb rsa3072/B96E3DC939D57247
created: 2025-03-18 expires: never usage: S
[ultimate] (1). Zhou Chuang (It's for learn gpg.) <zhouchuang0905@gmail.com>
gpg> addkey
Please select what kind of key you want:
(3) DSA (sign only)
(4) RSA (sign only)
(5) Elgamal (encrypt only)
(6) RSA (encrypt only)
(7) DSA (set your own capabilities)
(8) RSA (set your own capabilities)
(10) ECC (sign only)
(11) ECC (set your own capabilities)
(12) ECC (encrypt only)
(13) Existing key
(14) Existing key from card
Your selection? 8
Possible actions for this RSA key: Sign Encrypt Authenticate
Current allowed actions: Sign Encrypt
(S) Toggle the sign capability
(E) Toggle the encrypt capability
(A) Toggle the authenticate capability
(Q) Finished
Your selection? S
Possible actions for this RSA key: Sign Encrypt Authenticate
Current allowed actions: Encrypt
(S) Toggle the sign capability
(E) Toggle the encrypt capability
(A) Toggle the authenticate capability
(Q) Finished
Your selection? E
Possible actions for this RSA key: Sign Encrypt Authenticate
Current allowed actions:
(S) Toggle the sign capability
(E) Toggle the encrypt capability
(A) Toggle the authenticate capability
(Q) Finished
Your selection? A
Possible actions for this RSA key: Sign Encrypt Authenticate
Current allowed actions: Authenticate
(S) Toggle the sign capability
(E) Toggle the encrypt capability
(A) Toggle the authenticate capability
(Q) Finished
Your selection? Q
RSA keys may be between 1024 and 4096 bits long.
What keysize do you want? (3072) 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
Really create? (y/N) y
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.
sec ed25519/79AAAAA6C3E5A7E5
created: 2025-03-18 expires: never usage: SC
trust: ultimate validity: ultimate
ssb cv25519/4F92D94E32CA3934
created: 2025-03-18 expires: never usage: E
ssb rsa3072/B96E3DC939D57247
created: 2025-03-18 expires: never usage: S
ssb rsa2048/A7D2ED1B1EB73619
created: 2025-03-18 expires: never usage: AR
[ultimate] (1). Zhou Chuang (It's for learn gpg.) <zhouchuang0905@gmail.com>
gpg> save
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
查看是否创建成功:
❯ gpg -k 79AAAAA6C3E5A7E5
pub ed25519 2025-03-18 [SC]
A25E747A3E4B946FD8B313AE79AAAAA6C3E5A7E5
uid [ultimate] Zhou Chuang (It's for learn gpg.) <zhouchuang0905@gmail.com>
sub cv25519 2025-03-18 [E]
sub rsa3072 2025-03-18 [S]
sub rsa2048 2025-03-18 [AR]
2
3
4
5
6
7
# 密钥管理
# 列出密钥
❯ gpg --list-keys
/home/zhou/.gnupg/pubring.kbx
-----------------------------
pub ed25519 2025-03-18 [SC]
A25E747A3E4B946FD8B313AE79AAAAA6C3E5A7E5
uid [ultimate] Zhou Chuang (It's for learn gpg.) <zhouchuang0905@gmail.com>
sub cv25519 2025-03-18 [E]
sub rsa3072 2025-03-18 [S]
sub rsa2048 2025-03-18 [AR]
2
3
4
5
6
7
8
9
第一行显示公钥文件名(pubring.kbx),第二行显示公钥特征,第三行显示"用户ID",第四行显示私钥特征。
# 删除密钥
# 先删除私钥部分才能删除公钥部分
$ gpg --delete-secret-keys [用户ID] # 删除私钥
$ gpg --delete-keys [用户ID] # 删除公钥
2
3
# 删除子密钥
点击查看
❯ gpg --expert --edit-key A25E747A3E4B946FD8B313AE79AAAAA6C3E5A7E5
gpg (GnuPG) 2.4.4; Copyright (C) 2024 g10 Code GmbH
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Secret key is available.
sec ed25519/79AAAAA6C3E5A7E5
created: 2025-03-18 expires: never usage: SC
trust: ultimate validity: ultimate
ssb cv25519/4F92D94E32CA3934
created: 2025-03-18 expires: never usage: E
ssb rsa3072/B96E3DC939D57247
created: 2025-03-18 expires: never usage: S
ssb rsa2048/A7D2ED1B1EB73619
created: 2025-03-18 expires: never usage: AR
[ultimate] (1). Zhou Chuang (It's for learn gpg.) <zhouchuang0905@gmail.com>
gpg> key A7D2ED1B1EB73619
sec ed25519/79AAAAA6C3E5A7E5
created: 2025-03-18 expires: never usage: SC
trust: ultimate validity: ultimate
ssb cv25519/4F92D94E32CA3934
created: 2025-03-18 expires: never usage: E
ssb rsa3072/B96E3DC939D57247
created: 2025-03-18 expires: never usage: S
ssb* rsa2048/A7D2ED1B1EB73619
created: 2025-03-18 expires: never usage: AR
[ultimate] (1). Zhou Chuang (It's for learn gpg.) <zhouchuang0905@gmail.com>
gpg> delkey A7D2ED1B1EB73619
Do you really want to delete this key? (y/N) y
sec ed25519/79AAAAA6C3E5A7E5
created: 2025-03-18 expires: never usage: SC
trust: ultimate validity: ultimate
ssb cv25519/4F92D94E32CA3934
created: 2025-03-18 expires: never usage: E
ssb rsa3072/B96E3DC939D57247
created: 2025-03-18 expires: never usage: S
[ultimate] (1). Zhou Chuang (It's for learn gpg.) <zhouchuang0905@gmail.com>
gpg> save
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
# 输出密钥
公钥文件(.gnupg/pubring.kbx)以二进制形式储存,armor
参数可以将其转换为ASCII码显示;
$ gpg --armor --output public-key.txt --export [用户ID]
"用户ID"指定哪个用户的公钥,output
参数指定输出文件名(public-key.txt)。
类似地,export-secret-keys
参数可以转换私钥。
$ gpg --armor --output private-key.txt --export-secret-keys [用户ID]
导出公钥到终端:
gpg --export --export-options export-minimal --armor [用户ID]
# 上传公钥
网页上传
keys.openpgp.org
keyserver.ubuntu.com
命令上传
$ gpg --send-keys [用户ID] --keyserver keyring.debian.org
1使用上面的命令,你的公钥就被传到了服务器keyring.debian.org,然后通过交换机制,所有的公钥服务器最终都会包含你的公钥。 由于公钥服务器没有检查机制,任何人都可以用你的名义上传公钥,所以没有办法保证服务器上的公钥的可靠性,通过公钥指纹校验。
# 公钥指纹
你可以在网站上公布一个公钥指纹,让其他人核对下载到的公钥是否为真。fingerprint
参数生成公钥指纹。
$ gpg --fingerprint [用户ID]
❯ gpg --fingerprint zhouchuang0905@gmail.com
pub ed25519 2025-03-18 [SC]
A25E 747A 3E4B 946F D8B3 13AE 79AA AAA6 C3E5 A7E5
uid [ultimate] Zhou Chuang (It's for learn gpg.) <zhouchuang0905@gmail.com>
sub cv25519 2025-03-18 [E]
sub rsa3072 2025-03-18 [S]
2
3
4
5
6
# 导入密钥
除了生成自己的密钥,还需要将他人的公钥或者你的私钥密钥输入系统。这时可以使用import
参数。
导入公钥和私钥:
$ gpg --import [密钥文件公钥/私钥]
为了获得他人的公钥,可以让对方直接发给你,或者到公钥服务器上寻找。
$ gpg --keyserver keys.openpgp.org --search-keys [用户ID]
从服务器导入公钥:
$ gpg --keyserver keys.openpgp.org --recv-keys 5550D5D614E9CC36F1B731B930B117F460FCAF5F
正如前面提到的,我们无法保证服务器上的公钥是否可靠,下载后还需要用其他机制验证。
# 加密和解密
# 加密
假定有一个文本文件test.txt,怎样对它加密呢?
encrypt
参数用于加密,recipient
指定ID来加密
gpg --encrypt --output file.gpg --recipient user@example.com file
❯ cat test.txt
这是一个绝密文件
❯ gpg --recipient zhouchuang0905@gmail.com --output test.txt.gpg --encrypt test.txt
❯ ls
test.txt test.txt.gpg
❯ cat test.txt.gpg
�^O��N2�94@ ��
x9T]x����`0�� �FP0}��0*�8;��:b�T
Xw�Sr�t*����{�nuRj����*�7K�+
�փ<�i A�����
�S
�7M>o�U�Q
�%
2
3
4
5
6
7
8
9
10
11
12
13
recipient
参数指定接收者的公钥,output
参数指定加密后的文件名,encrypt
参数指定源文件。运行上面的命令后,test.en.txt就是已加密的文件,可以把它发给对方。
# 解密
对方收到加密文件以后,就用自己的私钥解密。
❯ gpg --output file --decrypt file.gpg
❯ gpg --output test.de.txt --decrypt test.txt.gpg
gpg: encrypted with cv25519 key, ID 4F92D94E32CA3934, created 2025-03-18
"Zhou Chuang (It's for learn gpg.) <zhouchuang0905@gmail.com>"
2
3
输入私钥密码:
解密成功
❯ cat test.de.txt
这是一个绝密文件
2
# 签名
# 对文件签名
有时候,我们不需要对文件进行加密,只需要对文件进行签名,表示这个文件确实是我本人发出的。使用sign
参数来签名。
如果想生成单独的签名文件,与原文件内容分开存放,可以使用detach-sign
参数。
如果想让生成的签名文件内容采用ASCII
码的形式,要加上armor
参数。
示例:为 test.txt 签名
❯ gpg --local-user zhouchuang0905@gmail.com --armor --detach-sign test.txt
–local-user
指定本地ID进行签名。默认情况下选择最新未被吊销的具有[S]功能的密钥。
他会生成一个末尾为asc的文件:
❯ cat test.txt.asc
-----BEGIN PGP SIGNATURE-----
iQHNBAABCgA3FiEE/52MvkVRXIEgCjwcuW49yTnVckcFAmfZZtsZHHpob3VjaHVh
bmcwOTA1QGdtYWlsLmNvbQAKCRC5bj3JOdVyR4ivDACYOhnCRra+pwX1z6duOOa0
Xgfg7WIF3B/v18bwvgZsPnD/ZhQAuFNXAEpgECM7LrC/rHUko8YmUmTKGbRKNPnm
hmnjcGeNbsq+DHAzRNhQC6hReoP9r0W64lp41gFJkMbd44ajLMy0lvcyhHRc2Xqp
uvCKhUY17fDaMDwq41CvhSSu5met5EKY1LEp2IUWaPOcU9VCRyeo6HfDKSqgoPyl
BQ5EULVmcF8xcc/ja4tylm8wvrgs5jDjo3+WKvc5F7jBDU3pB65zLkZ2h7S9PUrl
qUg454ONwn23r1n2oQ6h7hjS3ItakthuThxy0tXNwr9yKvFtOMacNat8KVqp/aPW
LaMtlKuRgwwONQxsEJOpLlgwiC5G7pkYNZJ+JKtd3nAM5TLaOJWhC79+LW13RlGm
TXix431eDy5jOPyYFePo/AxgPccj5MN3l64WOohTwpEa3z09NFoI5XuF9KdtmDKG
yqRCev5V4PARQ06QtDb5AuZ/MZpjximqF2GIbaW7jP4=
=KgnC
-----END PGP SIGNATURE-----
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 验证签名
加入我们收到了别人的文件和签名,如何验证签名为真?需要用对方的公钥来验证签名为真。使用verify
参数来验证。
签名asc
文件和文件本身分离,验证命令如下:
❯ gpg --verify test.txt.asc test.txt
gpg: Signature made Tue Mar 18 20:28:11 2025 CST
gpg: using RSA key FF9D8CBE45515C81200A3C1CB96E3DC939D57247
gpg: issuer "zhouchuang0905@gmail.com"
gpg: Good signature from "Zhou Chuang (It's for learn gpg.) <zhouchuang0905@gmail.com>" [ultimate]
2
3
4
5
另一个举例:使用openresty做个测试。
$ wget https://openresty.org/download/openresty-1.21.4.1rc1.tar.gz
$ wget https://openresty.org/download/openresty-1.21.4.1rc1.tar.gz.asc
$ gpg --verify openresty-1.21.4.1rc1.tar.gz.asc openresty-1.21.4.1rc1.tar.gz
gpg: Signature made Wed 22 Dec 2021 09:49:37 AM CST
gpg: using RSA key E52218E7087897DC6DEA6D6D97DB7443D5EDEB74
gpg: Can't check signature: No public key
$ gpg --keyserver keyserver.ubuntu.com --recv-keys E52218E7087897DC6DEA6D6D97DB7443D5EDEB74
gpg: /root/.gnupg/trustdb.gpg: trustdb created
gpg: key 97DB7443D5EDEB74: public key "OpenResty Admin <admin@openresty.com>" imported
gpg: Total number processed: 1
gpg: imported: 1
$ gpg --verify openresty-1.21.4.1rc1.tar.gz.asc openresty-1.21.4.1rc1.tar.gz
gpg: Signature made Wed 22 Dec 2021 09:49:37 AM CST
gpg: using RSA key E52218E7087897DC6DEA6D6D97DB7443D5EDEB74
gpg: Good signature from "OpenResty Admin <admin@openresty.com>" [unknown]
gpg: WARNING: This key is not certified with a trusted signature!
gpg: There is no indication that the signature belongs to the owner.
Primary key fingerprint: E522 18E7 0878 97DC 6DEA 6D6D 97DB 7443 D5ED EB74
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20