安装

windows 安装

winget install gpg4win

密钥类型

  1. 公钥 public key
  2. 私钥 secret key
  3. 子密钥 subkey

工具

  • GnuPG
  • seahorse

    • 图形化密钥管理工具

简介

  • 密钥管理工具
  • 加密解密工具

key server

  • http://pgp.mit.edu/

    • store and retrieve keys
    • 公钥分享网站,任何人都可上传,有被覆盖和错误风险

入门概念

参考:

基本功能

使用场景:

[C]

Certificating 认证

  • 认证密钥
[S]
Signing 签名
[A]
Authenticating 验证
[E]
Encrypting 加密

额外功能:

  • 生成密钥
  • 吊销证书

    • revocation certificate
    • 一个说明我的私钥丢失的证书,用于通知别人删除公钥
    • eg: 吊销公钥,(比如:忘记私钥,无法验证)
  • 过期时间

    • 例如:过期时间以后的签名无效
  • subkey 子密钥

主私钥 vs 子私钥

master key 和 subkey。

要点:

  • master key

    • 必须有 [C] 认证功能
    • 可以最多同时拥有 [A] 验证、 [S] 签名 和 [C] 认证三个功能
  • subkey

    • 最多同时拥有 [A] 验证 和 [S] 签名 两个功能
  • [E] 加密功能

    • 只能属于单个 subkey 子私钥
    • 因为加密算法的特殊性(和其他功能的算法不同)

密钥的 生成和管理

主密钥

创建主密钥

1
gpg --quick-generate-key 'Alice Engineer <alice@example.org>' rsa4096 cert
  • 创建密钥

    • ID: 'Alice Engineer <alice@example.org>'
    • 加密算法(algorithm): rsa4096
    • 用途: cert

      • 认证,只有主密钥(primary key)具有认证功能

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 88888C2F374A0667 marked as ultimately trusted gpg: directory 'home/<USER>.gnupg/openpgp-revocs.d' created gpg: revocation certificate stored as 'home/<USER>.gnupg/openpgp-revocs.d/888A43C6C780A55A8888ED8E88888C2F374A0667.rev' public and secret key created and signed.

Note that this key cannot be used for encryption. You may want to use the command "–edit-key" to generate a subkey for this purpose. pub rsa4096 2022-03-19 [C] [expires: 2024-03-18] 888A43C6C780A55A8888ED8E88888C2F374A0667 uid Alice Engineer <alice@example.org>

密钥指纹(fingerprint):

  • 指纹长度: 40 位, 888A43C6C780A55A8888ED8E88888C2F374A0667
  • 长指纹 ID(long): 最后 16 位(88888C2F374A0667)
  • 短指纹 ID(short): 最后 8 位(374A0667)

    • 避免使用,防止碰撞

纸质备份 secret key

使用 paperkey 程序

1
gpg --export-secret-key [fpr] | paperkey -o /tmp/key-backup.txt

身份(UID)

添加身份

key 的 UID 添加,用于个人身份的多重性(个人、工作、学习、开源项目等)

1
gpg --quick-add-uid [User-ID] 'Alice Engineer <allie@example.net>'
查看身份
1
gpg --list-keys <fingerprint>
设置主身份

因为 gpg 会把最近添加的身份作为主身份,改回原来的身份就要用到这个功能了

1
gpg --quick-set-primary-uid [User-ID] 'Alice Engineer <alice@example.org>'

导出私钥

1
gpg --export-secret-keys

子密钥

添加子密钥

1
2
3
4
5
6
7
8
# 添加一个 rsa2048 的加密密钥
gpg --quick-add-key [fpr] rsa2048 encr

# 添加一个 rsa2048 的签名密钥
gpg --quick-add-key [fpr] rsa2048 sign

# 添加一个 rsa2048 的验证密钥
gpg --quick-add-key [fpr] rsa2048 auth

注意区别: 这里使用的是 add-key, 并且制定了 [fingerprint](主密钥指纹)

导出子私钥

1
gpg --export-secret-subkeys

公钥

keyserver 公钥上传

trust of web(信任之网) 公钥的托管网络

1
2
3
4
gpg --send-key [fingerprint]

# 制定server
gpg --keyserver hkp://pgp.mit.edu:80 --send-key [fingerprint]

注意:

  • 隐私泄漏风险

    • 上传信息完全公开(姓名、邮箱、公钥)
    • 只可撤回,不可删除
刷新别人的公钥
1
gpg2 --refresh

导出公钥

1
gpg --armor --output <output-file.txt> --export [User-ID]

注: –armor 导出 ASCII 格式

U 盘备份

特性: 相比于 paperkey 纸质备份,易于恢复到电脑

流程:

  1. 准备可加密 U 盘
  2. 备份 gpg home 目录到可加密 U 盘
  3. 备份整个 ~/.gnupg 目录
  4. 测试备份可用
  5. ( 谨慎 )删除本地私钥

    • 深处私钥可以避免泄漏风险
  6. 谨慎: 删除本地吊销证书

备份命令

1
cp -rp ~/.gnupg /u_pan/directory/gnupg_backup

备注: -p 参数,保持用户和权限

测试备份成功命令

1
gpg --homedir=[/u_pan/directory/gnupg_backup] --list-key [fingerprint]

注:判断输出结果是否正确

删除本地私钥命令

  1. 查看私钥的 keygrip

    1
    
    gpg --with-keygrip --list-key [fingerprint]

    查看到的 keygrip 几位私钥的文件名 basename 的 stem 部分

  2. 私钥存储位置

    ~/.gnupg/private-keys.v1.d/[keygrip].key
    

    注: v1 与 gpg2 无关

  3. 通过 rm 命令删除即可

删除本地吊销证书命令

吊销证书与主密钥的 fingerprint 相关

1
rm ~/.gnupg/openpgp-revocs.d/[fingerprint].rev

使用 移动硬件存储 子密钥

参考: 技术|用 PGP 保护代码完整性(五):将子密钥移到一个硬件设备中

好处: 密钥的使用只在移动硬件内部使用,不会暴露在内存中,避免被窃取。

移动硬件: 智能卡或支持 U2F 协议的硬件。

支持硬件列表:

生成密钥

交互式命令

1
gpg --gen-key

完整命令

生成主密钥:

1
gpg --quick-generate-key 'UID' [alogrithm] [Usage] [Expire]

生成子密钥:

1
gpg --quick-add-key [fingerprint] [algorithm] [Usage] [Expire]

导出

1
2
3
gpg --export                    # 公钥
gpg --export-secret-keys        # 主私钥
gpg --export-secret-subkeys     # subkey 私钥
  • –armor

    • 以 ASCII 码导出结果
    • 不使用该参数,默认导出二进制
  • –output [file-name.txt]

    • 导出结果到文件

导入

1
gpg --import ./path/to/keyfile

如何添加为信任 key

使用 gpg --edit-key [fingerprint] 命令 trust 给定 key

参考:pgp - How to raise a key to ultimate trust on another machine? - Information …

加密解密数据

加密

1
gpg --recipient [User-ID-reciever] --output ./path/to/encrypted-file.en.txt --encrypt ./path/to/encrypt-file.txt
  • 参数

    • –recipient

      • 指定接收者的 User-ID,用于查询对方公钥
    • –output

      • 指定加密后的文件
    • –encrypt

      • 指定待加密的文件

解密

1
gpg --decrypt ./file/to/decrypt.en.txt --output ./decrypted/file.txt

签名和验证签名

文件签名

二进制签名

1
gpg --sign ./file.txt

注: 生成文件 file.txt.gpg

ASCII 签名

1
gpg --clearsign ./file.txt

注: 生成文件 file.txt.asc, .asc 是 ASCII 的意思

签名 + 加密

1
gpg --local-user [发信者ID] --recipient [接收者ID] --armor --sign --encrypt demo.txt

验证签名

1
gpg --verify file.txt.asc file.txt

注: 同时指定 签名文件被签名的文件

练习使用例子

openvpn 安装包的签名验证,参考: GnuPG Public Key | OpenVPN

信任之网

刷新密钥环

1
/usr/bin/gpg2 --refresh

删除秘钥

通过 gpg 删除

  1. 主密钥删除(C key)

    gpg --delete-secret-keys [fpr]
    
  2. 其他秘钥删除

    gpg --delete-keys [fpr]
    

通过 .gnupg 文件夹中文件删除

步骤:

  1. 获取 keygrip

    • keygrip 用来命名秘钥对应的 .key 文件
    • 命令:

      • C key: gpg --with-keygrip -k
      • A/E/S key: gpg --with-keygrip -K
  2. key 存放目录

    • ~/.gnupg/private-keys-v1.d/
  3. 主密钥删除还要同时删除 吊销证书

    • 存放目录: ~/.gnupg/openpgp-revocs.d

FAQ

文件后缀 .asc

只能说明这是一个 ASCII 内容格式的文件

  • 有可能是: public key 导出文件
  • 有可能是: private key 导出文件
  • 有可能是: 签名文件

备份 gnupg

直接备份整个 ~/.gnupg 文件夹

生成备份文件

流程:

  1. 公钥备份

    1
    2
    3
    
    # 备份所有的 keyring 中的key
    # 这个备份的是公钥等
    gpg --export backup.gpg
  2. 私钥备份

    1
    
    gpg --export-secret-keys backup.gpg
  3. 子密钥备份

    1
    
    gpg --export-secret-subkeys backup.gpg
  4. 导入

    1
    
    gpg --import ./backup.gpg