GPG 是GNU Privacy Guard的缩写,是一个开源的加密软件,主要用于数据的安全性和隐私保护。GPG 是基于 OpenPGP 标准的实现,该标准定义了一种用于加密、签名和验证数据的标准格式。
GPG 提供了以下主要功能:
GPG 被广泛应用于各种场景中,包括电子邮件加密、文件加密、软件包签名等。由于其开源和跨平台的特性,GPG 受到了许多用户的欢迎和信任,被认为是保护数据隐私和安全的重要工具之一。
今天我们的实验就是在本地产生一个密钥文件。
我们的实验环境是Windows WSL子系统,Ubuntu 22.04。通常,在Ubuntu系统中,gpg默认有安装,可以通过使用gpg指令来操作GPG(GNU Privacy Guard)相关功能。
要生成新的GPG密钥,可以直接使用如下指令:
- gpg --full-generate-key
-
执行后会要求你选择需求的密钥类型
- mirror@Ubuntu22:~$ gpg --full-generate-key
- gpg (GnuPG) 2.2.27; Copyright (C) 2021 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)
- (14) Existing key from card
- Your selection?
-
生成 GPG 密钥对时出现的选项,让你选择密钥的类型。每种类型都有不同的加密和签名算法组合。针对每个选项含义说明如下:
- RSA and RSA (default): 这是最常用的选项,用于创建同时支持 RSA 加密和 RSA 签名的密钥对。RSA 是一种广泛使用的加密和签名算法。
- DSA and Elgamal: 这个选项创建了一个同时支持 DSA 签名和 Elgamal 加密的密钥对。Elgamal 是另一种公钥加密系统。
- DSA (sign only): 这个选项创建了仅支持 DSA 签名的密钥对。这意味着你只能用它来对数据进行数字签名,而不能用于加密信息。
- RSA (sign only): 这个选项创建了仅支持 RSA 签名的密钥对。与选项 3 相似,只能用于签名而不能用于加密信息。
- Existing key from card: 如果你已经有一个存储在智能卡上的密钥,并希望将其导入到 GPG 中,选择此选项,它会允许你使用智能卡中现有的密钥对来执行操作。
这些选项允许你根据你的需求选择密钥类型,通常,默认的选项(1)RSA and RSA 是一个广泛适用且安全性较高的选择。
在本次实验中,我们也选择:(1) RSA and RSA (default)
这里是询问希望生成的密钥长度,RSA密钥长度决定了安全的能力,通常密钥长度越长,破解的难度就越大,安全性自然也就越高。
密钥长度支持1024-4096,这里默认是3072。我们选择长度最长的4096
- 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
-
输入 0 表示密钥永不过期,这意味着密钥永久有效。
输入一个数字 表示密钥在指定的天数后过期。
输入 w表示密钥在指定的周数后过期。
输入 m 表示密钥在指定的月数后过期。
输入 y 表示密钥在指定的年数后过期。
如果想要2年后过期,则输入2y表示。这里测试,我们选择0为永不过期。
- Key does not expire at all
- Is this correct? (y/N)
-
选择密钥不过期,它会重复与你确认。避免创建密钥是出现误选,因为不过期并不是一个推荐选项。所有的密钥应该都需要设定有效期。
- GnuPG needs to construct a user ID to identify your key.
-
- Real name: parish
- Email address: parish@parish.com
- Comment: none
- You selected this USER-ID:
- "parish (none) <parish@parish.com>"
-
- Change (N)ame, (C)omment, (E)mail or (O)kay/(Q)uit?
-
接着会需要输入一些个人信息,如姓名和电子邮件地址。这些信息将与你的公钥关联,以便其他人可以识别你的密钥。
如果确认无误,则输入o进入下一步。
你需要设置一个密码来保护你的私钥。这个密码应该足够复杂,以防止他人猜解。输入你的密码并确认。
密码需要确认再次输入。两次必须要一致。密码输入完成后,密钥就创建完成了,接着会输入如下:
- 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 D3D26B91D167ACD6 marked as ultimately trusted
- gpg: directory '/home/mirror/.gnupg/openpgp-revocs.d' created
- gpg: revocation certificate stored as '/home/mirror/.gnupg/openpgp-revocs.d/41A4A756278E8392D4515C48D3D26B91D167ACD6.rev'
- public and secret key created and signed.
-
- pub rsa4096 2023-12-18 [SC]
- 41A4A756278E8392D4515C48D3D26B91D167ACD6
- uid parish (none) <parish@parish.com>
- sub rsa4096 2023-12-18 [E]
-
从以上结果现实,我们已经成功创建一个RSA 4096的GPG密钥,在生成密钥的过程中,系统需要产生大量的随机字节,以确保密钥的安全性。为了提高随机数生成器的熵(entropy),建议在生成密钥的时候进行一些其他操作,比如键盘输入、鼠标移动或磁盘使用,这样可以增加系统熵池中的随机性数据。
通过gpg --list-keys 指令列出本地密钥文件
- gpg --list-keys
-
- mirror@Ubuntu22:~$ gpg --list-keys
- gpg: checking the trustdb
- gpg: marginals needed: 3 completes needed: 1 trust model: pgp
- gpg: depth: 0 valid: 1 signed: 0 trust: 0-, 0q, 0n, 0m, 0f, 1u
- /home/mirror/.gnupg/pubring.kbx
- -------------------------------
- pub rsa4096 2023-12-18 [SC]
- 41A4A756278E8392D4515C48D3D26B91D167ACD6
- uid [ultimate] parish (none) <parish@parish.com>
- sub rsa4096 2023-12-18 [E]
-
- mirror@Ubuntu22:~$
-
在 GPG(GNU Privacy Guard)中,生成密钥时会显示以下几个字段:
- pub rsa4096 2023-12-18 [SC]
- 41A4A756278E8392D4515C48D3D26B91D167ACD6
-
本次创建的密钥对,公钥指纹(fingerprint)是 41A4A756278E8392D4515C48D3D26B91D167ACD6公钥指纹是一个唯一标识你公钥的字符串,用于确认密钥的正确性。公钥可以分享给其他人用于加密信息或验证你的签名。
- sub rsa4096 2023-12-18 [E]
-
以上就是在Ubuntu中通过gpg指令创建本地密钥文件的过程,希望对大家学习有所帮助。