使用 GnuPG 签名、加密与解密

使用 GnuPG 签名、加密与解密


警告

请勿将本文提到的任何技术用于非法用途

遵守当地法律。


重要警告

重要警告:许多国家都限制使用或出口加密技术。如果您可能受到此类限制,您应该在尝试开发或分发加密代码之前寻求专业的法律建议。


协议

特别说明: 本文按“原样”提供,不做任何明示或暗示的担保,包括但不限于对适用性、正确性、使用情景合法性的担保。在和任何情况下,作者均不对读者使用本文提及的内容造成的结果进行赔偿或承担任何责任。

本作品采用 知识共享署名-禁止演绎 4.0 国际许可协议 进行许可。(CC-BY-ND 4.0)

当 CSDN 平台提供的版权协议与本协议 (CC-BY-ND 4.0) 不一致时,以本协议为准。

Copyright © 2024, 还没想好昵称的新建p

特别说明: 在转载时,禁止对本文的任何内容进行修改。

特别说明: “警告”和“协议”的全部内容应该按原样包含在本文的所有转载副本内容中。

如果您不同意协议中的任何一项请勿继续阅读或转载,并关闭此页面。

时间版本
2024-06-11 23:36 UTC+0100发布
2024-06-11 23:41 UTC+0100修正错别字

0. 假设

  1. 假设您的计算机操作系统支持常见的密码学算法,如 RSA 等。
  2. 假设您已经安装了 GNU Privacy Guard,即 GPG。假设您的计算机中有 libgcrypt 库等。
    如果您尚未安装,请使用您的软件包管理器安装。例如,若您使用 Ubuntu Linux,则应使用如下命令
sudo apt update
sudo apt install gnupg
  1. 在本文的用例中:假设非对称加密算法如 RSA4096 是足够安全的。假设密钥交换算法如 Diffie-Hellman 是足够安全的。假设安全散列算法如 SHA512 是足够安全的。假设您的计算机能生成足够安全的素数。假设您所安装的 GnuPG 的源代码是安全的,且没有供应链攻击。

另外,假设您对密码学有所了解。

本文以使用 gpg (GnuPG) 2.4.5 为例。

1. 生成公钥和密钥

使用如下命令生成公钥和密钥。

gpg --full-gen-key

GnuPG 将询问您相关信息。

gpg (GnuPG) 2.4.5; 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? 

此时,您应该回答使用何种加密算法。您可以回答 1,即使用 RSA 和 RSA。

RSA keys may be between 1024 and 4096 bits long.
What keysize do you want? (3072)

此时,您应该回答 RSA 的长度。若您希望更安全,您应该使用更长的 RSA,可以回答 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)

此时,您应该回答过期时间。

Key expires at Mon 30 Feb 2:33:33 2024 BST
Is this correct? (y/N)

如果信息正确,您应该回答 y

GnuPG needs to construct a user ID to identify your key.

Real name: 
Email address: 
Comment: 

此时,您应该回答您的真实姓名、邮箱和备注。

You selected this USER-ID:
    "New Producer <example@acm.org>"

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: revocation certificate stored as '/home/your-user-name/.gnupg/openpgp-revocs.d/DEADBEEFDEADBEEFDEADBEEFDEADBEEFDEADBEEF.rev'
public and secret key created and signed.

pub   rsa4096 2024-01-01 [SC] [expires: 2024-02-30]
      DEADBEEFDEADBEEFDEADBEEFDEADBEEFDEADBEEF
uid                      New Producer <example@acm.org>
sub   rsa4096 2024-01-01 [E] [expires: 2024-02-30]

2. 生成撤销密钥

您应该尽快生成撤销密钥。

gpg --output revoke.asc --gen-revoke example@acm.org

您应该将上述命令中的邮箱替换为您的邮箱。

sec  rsa4096/DEADBEEFDEADBEEF 2024-01-01 New Producer <example@acm.org>

Create a revocation certificate for this key? (y/N) 

您应该回答 y

Please select the reason for the revocation:
  0 = No reason specified
  1 = Key has been compromised
  2 = Key is superseded
  3 = Key is no longer used
  Q = Cancel
(Probably you want to select 1 here)
Your decision? 
Enter an optional description; end it with an empty line:
> 

您应该回答吊销原因和描述。

Is this okay? (y/N) y
ASCII armored output forced.
Revocation certificate created.

Please move it to a medium which you can hide away; if Mallory gets
access to this certificate he can use it to make your key unusable.
It is smart to print this certificate and store it away, just in case
your media become unreadable.  But have some caution:  The print system of
your machine might store the data and make it available to others!

您应该根据提示妥善保存您的撤销密钥,如使用安全的打印机和驱动程序打印。

3. 导出公钥

为了安全地通信,您应该分发您的公钥。

使用如下命令列出您的公钥。

gpg --list-keys
[keyboxd]
---------
pub   rsa4096 2024-01-01 [SC] [expires: 2024-02-30]
      DEADBEEFDEADBEEFDEADBEEFDEADBEEFDEADBEEF
uid           [ unknown] New Producer <example@acm.org>
sub   rsa4096 2024-01-01 [E] [expires: 2024-02-30]

使用如下命令导出您的公钥。

gpg --output nproducer.gpg --export example@acm.org

如上命令生成二进制的公钥,不便于在邮件或网页中分发。您可以添加 --armor 参数将其转换为 ASCII。

gpg --armor --export example@acm.org
-----BEGIN PGP PUBLIC KEY BLOCK-----

[省略]
-----END PGP PUBLIC KEY BLOCK-----

注意: 通常情况下,您不应该导出您的私钥。

4. 导入公钥

使用如下命令可导入公钥。

gpg --import nproducer.gpg
gpg: key DEADBEEF: public key imported
gpg: Total number processed: 1
gpg:               imported: 1

您可以根据用户手册,将导入的公钥认证、签名、检查等。

5. 加密和解密文件

假设您是 Alice,使用如下命令加密文件 docdoc.gpg

gpg --output doc.gpg --encrypt --recipient bob@acm.org doc

Bob 使用其私钥,使用如下命令解密 doc.gpg 得到 doc

gpg --output doc --decrypt doc.gpg

5. 签名和验证签名

假设您是 Alice,使用如下命令为文件 doc 签名。

gpg --output doc.sig --sign doc

Bob 使用您的公钥验证 doc 的签名。

gpg --output doc --decrypt doc.sig

您也可以使用 --clearsign 生成 ASCII 签名。

gpg --clearsign doc

您也可以将签名与文件分离。

gpg --output doc.sig --detach-sig doc

Bob 使用如下命令验证。

gpg --verify doc.sig doc

参考文献

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值