📓 Archive

GNUPG

FGJ: Create:2023/06/19 Update: (2025-03-08)

  • Intro(GnuPG) #

    • 基本概念 #

      Tip

      Key 管理是 GPG 的核心功能,GPG Key 不能简单的理解为非对称加密的 Private Key / Public Key / Key Pair。GPG Key 由如下信息组成:

      Key ID: 该 GPG Key 的唯一标识,值为主公钥的指纹,支持多种格式(Fingerprint, Long key ID, Short key ID),更多参见:What is a OpenPGP/GnuPG key ID?。
      UID: 1 个或多个,每个 UID 由 name、email、comment 组成,email 和 comment 可以为空。
      Expire: 过期时间,可以为永久。
      多个具有不同用途的非对称加密算法中的 Key 的集合。

      key 分类参见下表

      主秘钥和主公钥(Primary Key)、子秘钥和子公钥(Sub Key)都是成对出现的,其用途也是一致的。
      每一对都包含一个 key id 属性(为 public key 的指纹),其中主密钥/主公钥的 key id 就是当前 GPG Key 的 Key ID。

      类型全名缩写用途 (Usage)说明
      主公钥Public KeypubSC每个 GPG Key 有且只有一个 主公钥,可以选择一种或多种 Usage
      子公钥Public SubkeysubS/A/E每个 GPG Key 可以有多个子公钥,每个子公钥可以选择一种或多种 Usage
      主私钥Secret KeysecSC每个 GPG Key 有且只有一个 主私钥,可以选择一种或多种 Usage
      子私钥Secret SubkeyssbS/A/E每个 GOG Key 可以有多个子私钥,每个子私钥可以选择一种或多种 Usage

      用途参见下表

      缩写全名用途
      CCertificating管理证书,如添加/删除/吊销子密钥/UID,修改过期时间。
      SSigning签名,如文件数字签名、邮件签名、Git 提交。
      AAuthenticating身份验证,如登录。
      EEncrypting加密,如文件和文本。
    • 安装 #

      # 1. 源码安装
      # https://www.gnupg.org/download/index.en.html
      ./configure
      make && make install

      sudo apt-get install gnupg

      yum install gnupg

      brew install gnupg

    • 基本操作 #

      • 1. 查看公匙和私匙 #

        Tip

        gpg -k/-K [--keyid-format {none|short|0xshort|long|0xlong}] [KEYID]

      • 2. 生成与删除公私匙 #

        Tip

        gpg [--generate-key | --full-generate-key]

        gpg --delete-secret-keys <KEYID> gpg --delete-key <KEYID>


        Caution

        删除的时候得先删除私匙

      • 3. 输出密匙 #

        Tip

        gpg --armor [-o xx.txt] --export [KEYID]
        gpg --armor [-o xx.txt] --export-secret-keys [KEYID]

      • 4. 查看公匙指纹 #

        Tip

        gpg --fingerprint [KEYID]

      • 5. 上传公匙 #

        Tip

        有如下方式:

        1.: 通过 gpg --send-keys 方式。
        gpg --keyserver hkp://keyserver.ubuntu.com --send-keys [KEYID]
        gpg --keyserver hkps://keys.openpgp.org --recv-keys [KEYID]
        --keyserver name This option is deprecated - please use the –keyserver in ‘dirmngr.conf’ instead.



        2.: 通过 curl 方式。 参考
        gpg --export 425B8CB8073AAC1EB005E4E648E1F1185160B400 | curl -T - https://keys.openpgp.org



      • 6. 导入公匙 #

        Tip

        gpg --import [file]

    • 加密签名 #

      demo txt

      hello world;
      最近怎么样,我正在学习gpg。

      • 加密 #

        Note

        gpg --recipient 425B8CB8073AAC1EB005E4E648E1F1185160B400 --output demo.en.txt --encrypt demo.txt

      • 解密 #

        Note

        gpg --armor --decrypt demo.en.txt

      • 签名 #

        Note

        gpg --sign demo.txt
        生成 demo.txt.gpg 文件,这个文件默认采用二进制储存,如果想生成ASCII码的签名文件,可以使用--clearsign参数,生成 demo.txt.asc 文件
        如果想生成单独的签名文件,与文件内容分开存放,可以使用--detach-sign参数。生成一个单独的签名文件 demo.txt.sig 。该文件是二进制形式的,
        如果想采用ASCII码形式,要加上armor参数。gpg --armor --detach-sign demo.txt

      • 验签 #

        Note

        gpg --verify demo.txt.asc
        出现: WARNING: not a detached signature; file ‘demo.txt’ was NOT verified!
        是因为附近有’demo.txt’文件造成的。可以理解为这不是一个单独的签名文件。如果同级目录存在’demo.txt’,不对其进行hash验签。
        可以重命名’demo.txt’或者使用--detach-sign来消除警告。 参考

      • 签名➕加密 #

        Note

        gpg --local-user [发信者KEYID] --recipient [接收者KEYID] --armor --sign --encrypt demo.txt
        local-user参数指定用发信者的私钥签名,recipient参数指定用接收者的公钥加密,armor参数表示采用ASCII码形式显示,
        sign参数表示需要签名,encrypt参数表示指定源文件。

      • 解密➕验签 #

        Note

        gpg --decrypt demo.txt.asc
        将上述步骤生成的demo.txt.asc进行解密验签名。

        Caution

        需要注意的是:不能单独验证,可能是因为先加密的原因,需要解密后才可以看见签名,发现签名后顺带验证签名了。

    • 其他样例 #

      • openvpn 样例 #

        -- 导入openvpn公匙
        wget -O security-openvpn-net.asc https://keys.openpgp.org/vks/v1/by-fingerprint/F554A3687412CFFEBDEFE0A312F5F7B42F2B01E7
        gpg --import security-openvpn-net.asc
        
        -- 下载签名文件
        wget -c https://swupdate.openvpn.org/community/releases/openvpn-2.5.5.tar.gz.asc
        
        -- 下载源文件
        wget -c https://swupdate.openvpn.org/community/releases/openvpn-2.5.5.tar.gz
        
        -- 验证
        gpg --verify openvpn-2.5.5.tar.gz.asc openvpn-2.5.5.tar.gz
        
      • github 验证 #

        # https://docs.github.com/en/authentication/managing-commit-signature-verification/checking-for-existing-gpg-keys
        
    • 公用服务器 #

    • GnuPG套件 #

      Note

      gpg有很多的辅助程序,比如gpg-agent,dirmngr
      重启agent gpg-connect-agent reloadagent /bye
      查看agent gpg-connect-agent 'keyinfo --list' /bye
      查看keygrip gpg --with-keygrip -K 425B8CB8073AAC1EB005E4E648E1F1185160B400
      使用 keygrip 缓存密码"$(gpgconf --list-dirs libexecdir)"/gpg-preset-passphrase --preset <keygrip>回车后输密码,用于自动化。(需要在gpg-agent中开启allow-preset-passphrase选项)
      使用 keygrip 清除缓存"$(gpgconf --list-dirs libexecdir)"/gpg-preset-passphrase --forget <keygrip>
      清除可能会报错: gpg-preset-passphrase: clearing passphrase failed: No inquire callback in IPC(原因暂时未知),但是不影响密码的清除。
      或者通过重启 agent,全部清除。

  • Reference #


comments powered by Disqus