什么是 GPG?
GnuPG(或 Gnu Privacy Guard)是对称为 PGP(或 Pretty Good Privacy)的早期加密标准的改编。 GPG 使用“公共”和“私人”密钥的方法对消息或数据进行加密和签名。
在本文中,我们将讨论以下主题:
- 为什么需要 GPG?
- 公钥加密如何工作?
- 如何设置 GPG 密钥?
- 如何查找其他用户的公钥?
- 如何使我的公钥可供其他人使用?
- 如何使用 GPG 加密和解密消息?
如今,对隐私、保密和安全意识的认真需求比以往任何时候都更应该成为日常生活的主要重点之一。 在当今这个时代,这是一个不幸但必要的要求。 利用许多可用的 在线工具 采取明显的预防措施肯定可以将成为恶意软件目标或恶意行为者的风险降到最低,但并不能完全消除被入侵的可能性。
为什么需要 GPG?
GPG 是确保两方之间安全通信的绝佳方法。 它允许在不安全的网络中轻松共享敏感信息。
异步密钥加密使用公钥和私钥来加密/解密消息。 这称为非对称加密。 它允许您在不安全的网络上公开发送或发布您的公钥。 希望向您发送安全消息的一方使用您的公钥使用该密钥加密消息。 然后将消息发送给您,然后您使用您的私钥对其进行解密。 然后,您可以使用发件人的公钥进行回复,这使您可以安全地加密发给他们的消息。 然后他们使用他们的私钥来解密你加密的消息。
先决条件
安装 GPG
Ubuntu/Debian
root@host:~$ apt-get install gnupg
CentOS/红帽
root@host:~$ yum install gnupg
验证安装
root@host:~$ gpg --version
gpg (GnuPG) 2.2.12
libgcrypt 1.8.4
Copyright (C) 2018 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Home: /home/david/.gnupg
Supported algorithms:
Pubkey: RSA, ELG, DSA, ECDH, ECDSA, EDDSA
Cipher: IDEA, 3DES, CAST5, BLOWFISH, AES, AES192, AES256, TWOFISH,
CAMELLIA128, CAMELLIA192, CAMELLIA256
Hash: SHA1, RIPEMD160, SHA256, SHA384, SHA512, SHA224
Compression: Uncompressed, ZIP, ZLIB, BZIP2
如何设置 GPG 密钥?
生成密钥对,然后导出公钥。
首先,您必须生成一个密钥对。
root@host:~$ gpg --full-generate-key
gpg (GnuPG) 2.2.12; Copyright (C) 2018 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? (3072) 4096
Requested keysize is 4096 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: David Singer
Email address: [email protected]
Comment:
You selected this USER-ID:
"David Singer <[email protected]>"
Change (N)ame, (C)omment, (E)mail or (O)kay/(Q)uit? o
接下来,将出现一个弹出窗口,要求您输入密码。
警告:不要忘记您的密码! 如果忘记密码,您的新密钥将无法使用。 记下此密码并将其存储在安全位置。
接下来,GPG 将要求您在屏幕上移动鼠标以收集随机数据以在密钥创建过程中使用。
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 B3219C4BE8F1E313 marked as ultimately trusted
gpg: revocation certificate stored as '/home/david/.gnupg/openpgp-revocs.d/41C38C7648F1A89FC06395CFB3219C4BE8F1E313.rev'
public and secret key created and signed.
pub rsa4096 2019-12-17 [SC]
41C38C7648F1A89FC06395CFB3219C4BE8F1E313
uid David Singer <[email protected]>
sub rsa4096 2019-12-17 [E]
导出您的密钥
要导出新密钥,请使用:
root@host:~$ gpg --output your.name.gpg --armor --export [email protected]
当您导出您的密钥文件时,它将作为您共享的密钥的公共版本输出,并将被称为 yourname.gpg。 这是您将提供的文件,以允许其他人加密给您的消息。 这是一个链接 example 公共 gpg 文件.
导出公钥后,您需要将密钥共享给公钥服务器。 这是其他人可以找到并用于查找您的公钥的地方。 有多个密钥服务器可用于此目的。 一些比较流行的是:
- pgp.mit.edu
- keyserver.ubuntu.com
- 密钥服务器.pgp.com
- peegeepee.com
- pgp.surfnet.nl
- 钥匙.openpgp.org
- keyring.debian.org
- keys.mailvelope.com
这是一个 example 如何将您的公钥导出到 pgp.mit.edu 密钥服务器。
root@host:~$ gpg --send-keys --keyserver hkp://pgp.mit.edu E8F1E313
gpg: sending key B3219C4BE8F1E313 to hkp://pgp.mit.edu
使用加密密钥
找到接收者的公钥
为了加密传出消息,您将需要发送消息的人的公钥。 很多时候,个人或实体已经拥有位于上述一个或多个公钥服务器上的已建立 GPG 密钥。 如果他们不这样做,您可以将他们指向这篇文章以查看并为他们自己设置密钥对。
为了找到个人的公钥对,只需在上面提到的其中一个密钥服务器上快速搜索姓名、电子邮件地址或其他唯一定义因素,并显示其结果公钥。 搜索密钥的一种简单方法是运行这样的命令。
root@host [~]# gpg --keyserver hkp://p80.pool.sks-keyservers.net:80 --search [email protected]
为了 example,如果我们去搜索 [email protected](gpg 测试机器人),使用下面的命令,我们可以看到最多 最近的密钥 该机器人可用,我们可以使用该密钥发送测试电子邮件以验证我们的设置是否有效。
root@host [~]# gpg --keyserver hkp://p80.pool.sks-keyservers.net:80 --search [email protected]
gpg: searching for "[email protected]" from hkp server p80.pool.sks-keyservers.net
(1) Saeed Lootah (test) <[email protected]>
Saeed Lootah (test1) <[email protected]>
Adele (The friendly OpenPGP email robot) (test1) <[email protected]>
2048 bit RSA key BFEE478F, created: 2017-11-18
(2) Adele (Test) <[email protected]>
2048 bit RSA key ED161999, created: 2014-08-13, expires: 2018-08-13 (expired)
(3) adele-en <[email protected]>
2048 bit RSA key 4D486CC8, created: 2013-07-08, expires: 2017-07-08 (expired)
(4) Adele (Der freundliche E-Mail-Roboter) <[email protected]>
Adele (The friendly OpenPGP email robot) <[email protected]>
1024 bit DSA key 92AB3FF7, created: 2002-03-06
Keys 1-4 of 4 for "[email protected]". Enter number(s), N)ext, or Q)uit > 1
gpg: requesting key BFEE478F from hkp server p80.pool.sks-keyservers.net
gpg: key BFEE478F: public key "Adele (The friendly OpenPGP email robot) (test1) <[email protected]>" imported
gpg: no ultimately trusted keys found
gpg: Total number processed: 1
gpg: imported: 1 (RSA: 1)
root@host [~]#
如果某人的公钥发布到一个鲜为人知的密钥服务器上,而该密钥服务器可能不会与其他密钥服务器共享该信息,那么您在尝试定位该公钥时可能会遇到问题。
找到您的公钥
与您检索要向其发送消息的人的密钥相同的方式,他们也需要这样做。 您可以在网站、电子邮件底部(在您的签名中)或其他位置发布您的公钥,以帮助其他人收集此信息。
关键信任级别
与其他人共享您的密钥时,重要的是要注意发送者和接收者都应该为您提供给彼此的密钥建立一定程度的信任。 这是一个必须解决的安全步骤。 在验证密钥时,请务必使用权威且可靠的可信通信方法。 关键信任有两个重要因素。 签名的有效性和 Ownertrust。 这是两个不同的方面,不应混淆。
- 签名的有效性证明签名是否有效。
- Ownertrust 是指您分配给某个密钥的信任级别。
由于 GnuPG 使用 OpenPGP Web of Trust 模型,密钥的信任级别允许密钥验证其他密钥。 为了创建、修改或更新密钥,您可以使用以下命令。
root@host [~]# gpg --edit-key adele
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.
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 2048R/BFEE478F created: 2017-11-18 expires: never usage: SCE
trust: ultimate validity: ultimate
[ultimate] (1). Adele (The friendly OpenPGP email robot) (test1) <[email protected]>
[ultimate] (2) Saeed Lootah (test) <[email protected]>
[ultimate] (3) Saeed Lootah (test1) <[email protected]>
gpg> trust
pub 2048R/BFEE478F created: 2017-11-18 expires: never usage: SCE
trust: ultimate validity: ultimate
[ultimate] (1). Adele (The friendly OpenPGP email robot) (test1) <[email protected]>
[ultimate] (2) Saeed Lootah (test) <[email protected]>
[ultimate] (3) Saeed Lootah (test1) <[email protected]>
Please decide how far you trust this user to correctly verify other users' keys
(by looking at passports, checking fingerprints from different sources, etc.)
1 = I don't know or won't say
2 = I do NOT trust
3 = I trust marginally
4 = I trust fully
5 = I trust ultimately
m = back to the main menu
Your decision? 4
pub 2048R/BFEE478F created: 2017-11-18 expires: never usage: SCE
trust: full validity: ultimate
[ultimate] (1). Adele (The friendly OpenPGP email robot) (test1) <[email protected]>
[ultimate] (2) Saeed Lootah (test) <[email protected]>
[ultimate] (3) Saeed Lootah (test1) <[email protected]>
Please note that the shown key validity is not necessarily correct
unless you restart the program.
gpg> quit
root@host [~]#
root@host [~]# gpg-connect-agent /bye
root@host [~]#
正如您在第一行末尾看到的那样:
信任:0-、0q、0n、0m、0f、1u
0 之后的这些字母表示信任级别,如下所述。 数字 0 表示没有分配信任级别。 “1u”表示信任级别是终极的。
之后,我们使用 “gpg-连接代理/再见”。
GPG 将在再次需要时重新启动。
- 未知/未分配 Ownertrust: “-” 这是所有导入密钥的默认状态。 除了密钥本身的内容之外,没有关于密钥所有者的信息。 您的公钥环上所有未经验证的密钥都具有此初始信任级别。
- 信任计算失败:“e”。 这通常表明信任过程可能由于过期的密钥而失败。
- 没有任何: “q” 这通常表示信任级别仍然是“未知”,或者没有足够的信息来做出决定。 此设置有所不同,因为您实际上已将信任级别修改为“无”,或者如果密钥所有者已被识别为不正确地签署其他密钥。
- 没有任何: “n” 永远不要相信这个密钥。 如果发现签名或密钥具有欺诈性或无效,则使用此选项。
- 边缘: “m” 将密钥设置为边缘信任将修改密钥以显示为有效,这表明导入的密钥已被您信任网络中的至少三个其他人信任。 为了 example,如果您将 Bob、Sue 和 Ann 的密钥设置为“边际”并且他们都信任导入的密钥,则应将其设置为边际。
- 满的: “f” 此设置用于您信任的密钥以签署其他密钥。 基本上,如果 Sue 的密钥由您的朋友 Bob 签名,您将其密钥所有者信任设置为 Full,则 Sue 的密钥也将被信任。 您应该仅在验证并签署某人的密钥后使用 Full ownertrust。
- 最终的: “u” 这个 ownertrust 设置应该只用于你的个人密钥。 最终,您的密钥是您可以明确信任的唯一密钥!
注意:导入新密钥时,小写字母(e、q、mf 和 u)将显示在命令行中以作为响应。
验证外部密钥
验证外部 PGP 密钥的正确方法包括在您和密钥所有者之间建立连续的“信任链”。 在您个人不认识密钥所有者的某些情况下,此验证充其量可能很困难。
获得要添加的密钥的 ID 后,您可以通过上述密钥服务器接口之一确认密钥的详细信息。 您应该会看到电子邮件地址、照片和签名密钥的人记录的其他信息等信息。
root@host [~]# gpg --sign-key [email protected]
如果您尝试向其发送加密消息的人在网站或文件中有可用的公开公钥文件(例如 user.asc 或 user.key),我们可以在 gpg 中使用“–import”标志将该密钥添加到我们的密钥环中。
gpg --import < user.asc
验证密钥是否属于声称拥有所有权的人的最简单方法是打电话给他们或与密钥所有者进行音频/视频聊天。 要验证它们:
- 在 GPG 钥匙串中打开您联系人的公钥并双击它。
- 请您的联系人也这样做并打开他们的 GPG 钥匙串并双击它。
- 让他们给你读他们的指纹
- 验证指纹是否相同
- 将信任级别提高到您感到满意的程度。
root@host [~]# gpg --finger BFEE478F
pub 2048R/BFEE478F 2017-11-18
Key fingerprint = C5D2 8434 D779 D2DE 7255 029F B3DB 8177 BFEE 478F
uid Adele (The friendly OpenPGP email robot) (test1) <[email protected]>
uid Saeed Lootah (test) <[email protected]>
uid Saeed Lootah (test1) <[email protected]>
root@host [~]#
另一种收集外部公钥信息的方法,我们可以使用手指标志来收集更多信息。
正如我们在上面的交互中看到的,我们可以确认 [email protected] 电子邮件地址具有相关的 BFEE478F 签名。 这意味着我们可以添加对签名者信息的进一步验证。
root@host [~]# gpg --edit BFEE478F
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/BFEE478F created: 2017-11-18 expires: never usage: SCE
trust: unknown validity: unknown
[ unknown] (1). Adele (The friendly OpenPGP email robot) (test1) <[email protected]>
[ unknown] (2) Saeed Lootah (test) <[email protected]>
[ unknown] (3) Saeed Lootah (test1) <[email protected]>
gpg> lsign
Really sign all user IDs? (y/N) y
gpg> save
Key not changed so no update needed.
root@host [~]#
GnuPG 的信任数据库信息
每当更新信任数据库时,都会打印以下数据类型。 为了 example,在从密钥服务器接收到新的或更新的信息之后。
gpg: depth: 0 valid: 1 signed: 16 trust: 0-, 0q, 0n, 0m, 0f, 1u
gpg: depth: 1 valid: 16 signed: 115 trust: 1-, 1q, 1n, 1m, 12f, 0u
gpg: depth: 2 valid: 105 signed: 189 trust: 81-, 11q, 0n, 4m, 9f, 0u
gpg: depth: 3 valid: 29 signed: 120 trust: 19-, 9q, 0n, 0m, 1f, 0u
1
上述输出表明您在 GPG TrustDB 中维护的信任网络。 在 0 级”gpg:深度:0“,您将找到您的(最终受信任的)密钥。在此级别上不应有任何其他类型的受信任密钥。
只要未在配置文件中修改默认的最大深度路径,在更远深度受信任的密钥将生成级别 0-5。 OpenPGP 信任模型将深度 5 表示为标准。
现在就是这样! 在我们关于 GPG 的下一篇文章中,我们将讨论发送加密电子邮件的最佳方式。
想了解更多关于 GPG 或加密的信息吗?
对如何在服务器上实施这项技术或在日常工作流程中更多地利用它有进一步的想法吗?
给我们打电话 800.580.4985,或打开 聊天 或与我们联系,与我们知识渊博的解决方案或经验丰富的托管顾问之一交谈,了解您今天如何利用这些技术!