zoukankan      html  css  js  c++  java
  • gnupg

     
    1. 简介
     1.1 用途
     1.2 关键字: 
     1.3 相关软件
    2. 安装配置
     2.1 文件
     2.2 命令与选项
    3. 使用
     3.1 生成密钥和吊销证书
      3.1.1 创建密钥对 Create a key pair
      3.1.2 生成密钥后,您应该做的第一件事就是创建吊销证书
      3.1.3 查看密钥
     3.2 导出导入公钥
      3.2.1 导出您的公钥
      3.2.2 导入公钥 Import a public key
     3.3 使用keyserver密钥服务器
     3.4 Web密钥目录
     3.5 加密和解密
      3.5.1 不对称
      3.5.2. 对称
    4 Key维护
     4.1 备份私钥 Backup your private key
     4.2 撤销你的密钥对 Revoke a key
     4.3 编辑 key
     4.4. 删除 key
    5 签名
     5.1 创建签名
     5.2. 验证签名: 要验证签名,请使用--verify标志:
    6 扩展阅读链接

    1. 简介

    https://gnupg.org/
    GNU隐私卫士
    GnuPG是RFC4880(也称为PGP)定义的OpenPGP标准的完整和免费实现。
    GnuPG允许您对数据和通信进行加密和签名;它具有通用的密钥管理系统以及用于各种公钥目录的访问模块。GnuPG,也称为GPG,是一种命令行工具,具有易于与其他应用程序集成的功能。有大量的 前端应用程序和库。GnuPG还提供对S/MIME和Secure Shell(ssh)的支持。

    1.1 用途

    创作者使用: 制作签名sign
    生成生成密钥(对)和吊销证书; 安全稳妥的保存私钥和吊销证书; 发布公钥(导出公钥文件public.key提供下载; 或者通过keyserver密钥服务器)
    创建签名: 软件或文本配套的.sig签名文件提供下载, 使用户能够验证.

    大部分普通用户: 验证verify
    仅仅使用签名的验证部分, 比如下载某个软件, 用于确定是作者本人发布的.
    获取公钥: 下载公钥文件public.key导入; 或者通过keyserver密钥服务器导入
    获取签名文件: 下载软件配套的.sig签名文件; 使用命令验证.

    敏感文件的加密和解密: encrypt or decrypt

    1.2 关键字:

    • 加密: encrypt
    • 解密: decrypt
    • 非对称: Asymmetric
    • 对称: Symmetric
    • 签名: signatures, [*.sig] 任何人都可以获取.
    • 公钥: public key, [public.key], key-id, 密钥ID 任何人都可以获取.
    • 私钥: secret-keys, [privkey.asc] 仅作者本人妥善保管.
    • 吊销证书: revocation certificate [*.asc] 仅作者本人妥善保管.
    • 指纹: fingerprint, 公钥指纹 (指纹是公钥的短形式)

    1.3 相关软件

    https://wiki.archlinux.org/index.php/List_of_applications/Security#Encryption,_signing,_steganography
    https://wiki.archlinux.org/index.php/GnuPG
    || gnupg — The GNU project's complete and free implementation of the OpenPGP standard as defined by RFC4880. Free and Open Source replacement of PGP, mostly used for digital signing of packages.
    https://gnupg.org/
    || gpg-crypter — Graphical front-end to GnuPG(GPG) using the GTK3 toolkit and GPGME library.
    https://sourceforge.net/projects/gpg-crypter/
    || keybase — Key directory mapping social media identities, with cross platform encrypted chat, cloud storage, and git repositories.
    https://keybase.io/
    || kgpg — Simple interface for GnuPG, for KDE.
    https://www.kde.org/applications/utilities/kgpg/
    || kleopatra — Certificate Manager and Unified Crypto GUI for KDE. It supports managing X.509 and OpenPGP certificates in the GpgSM keybox and retrieving certificates from LDAP servers.
    https://www.kde.org/applications/utilities/kleopatra/
    || minisign — Simple program that only inplements key signing
    https://github.com/jedisct1/minisign
    || seahorse — GNOME application for managing encryption keys and passwords in the GNOME Keyring.
    https://wiki.gnome.org/Apps/Seahorse
    || scrypt — Command-line encryption utility featuring the memory-hardened scrypt key derivation function.
    https://www.tarsnap.com/scrypt.html

    2. 安装配置

    目录位置
    $GNUPGHOME由GnuPG使用指向存储其配置文件的目录。默认情况下$GNUPGHOME未设置,$HOME而是使用your 。因此,您将~/.gnupg在安装后立即找到目录。
    要更改默认位置,请以这种方式运行gpg 或设置环境变量。 $ gpg --homedir path/to/fileGNUPGHOME

    配置文件
    默认配置文件是~/.gnupg/gpg.conf和~/.gnupg/dirmngr.conf。
    默认情况下,gnupg目录的权限设置为700,其中包含的文件的权限设置为600。只有目录的所有者才有权读取,写入和访问文件。这是出于安全目的,不应更改。如果此目录或其中的任何文件未遵循此安全措施,则将收到有关不安全文件和主目录权限的警告。

    将任何所需的长选项附加到这些文件。不要写两个破折号,而只写选项名称和必需的参数。您将在中找到框架文件/usr/share/doc/gnupg/。如果这些文件在~/.gnupggpg中不存在,则将它们复制到首次运行。其他示例可在#See另请参见中。 https://wiki.archlinux.org/index.php/GnuPG#See_also
    此外,pacman使用一组不同的配置文件来进行软件包签名验证。有关详细信息: https://wiki.archlinux.org/index.php/Pacman/Package_signing

    2.1 文件

    FILES There are a few configuration files to control certain aspects of gpg's operation.
    Unless noted, they are expected in the current home directory (see: [option --homedir]).
    有一些配置文件可以控制gpg操作的某些方面.
    除非另有说明, 否则它们应在当前主目录中(请参阅: [option --homedir]).
    gpg.conf This is the standard configuration file read by gpg on startup.
    It may contain any valid long option; the leading two dashes may not be entered and the option may not be abbreviated.
    This default name may be changed on the command line (see: [gpg-option --options]).
    You should backup this file.
    这是gpg在启动时读取的标准配置文件.
    它可以包含任何有效的长选项; 前两个破折号可能不会输入, 并且该选项可能不会缩写.
    可以在命令行上更改此默认名称(请参阅: [gpg-option –options]). 您应该备份此文件.
      Note that on larger installations, it is useful to put predefined files into the directory ‘/etc/skel/.gnupg’ so that newly created users start up with a working configuration.
    For existing users a small helper script is provided to create these files (see: [addgnupghome]).
    请注意, 在较大的安装中, 将预定义的文件放入目录" /etc/skel/.gnupg”很有用, 以便新创建的用户以有效的配置启动.
    对于现有用户, 提供了一个小的帮助程序脚本来创建这些文件(请参阅:[addgnupghome]).
      For internal purposes gpg creates and maintains a few other files; They all live in the current home directory (see: [option --homedir]).
    Only the gpg program may modify these files.
    为了内部目的, gpg创建并维护了一些其他文件; 它们都位于当前主目录中(请参阅: [option --homedir]). 只有gpg程序可以修改这些文件.
    ~/.gnupg This is the default home directory which is used if neither the environment variable GNUPGHOME nor the option --homedir is given. 这是默认主目录, 如果既未提供环境变量GNUPGHOME也未提供选项--homedir, 则使用该默认主目录.
    ~/.gnupg/pubring.gpg The public keyring. You should backup this file. 公共密钥环. 您应该备份此文件.
    ~/.gnupg/pubring.gpg.lock The lock file for the public keyring. 公用密钥环的锁定文件.
    ~/.gnupg/pubring.kbx The public keyring using a different format. This file is shared with gpgsm.
    You should backup this file.
    公共密钥环使用其他格式. 该文件与gpgsm共享. 您应该备份此文件.
    ~/.gnupg/pubring.kbx.lock The lock file for ‘pubring.kbx’. " pubring.kbx”的锁定文件.
    ~/.gnupg/secring.gpg A secret keyring as used by GnuPG versions before 2.1. It is not used by GnuPG 2.1 and later. 2.1之前的GnuPG版本使用的秘密密钥环. GnuPG 2.1和更高版本不使用它.
    ~/.gnupg/secring.gpg.lock The lock file for the secret keyring. 秘密密钥环的锁定文件.
    ~/.gnupg/.gpg-v21-migrated File indicating that a migration to GnuPG 2.1 has been done. 表示已完成向GnuPG 2.1的迁移的文件.
    ~/.gnupg/trustdb.gpg The trust database. There is no need to backup this file; it is better to backup the ownertrust values (see: [option --export-ownertrust]). 信任数据库. 无需备份此文件. 最好备份ownertrust值(请参阅: [option --export-ownertrust]).
    ~/.gnupg/trustdb.gpg.lock The lock file for the trust database. 信任数据库的锁定文件.
    ~/.gnupg/random_seed A file used to preserve the state of the internal random pool. 用于保留内部随机池状态的文件.
    ~/.gnupg/openpgp-revocs.d/ This is the directory where gpg stores pre-generated revocation certificates.
    The file name corresponds to the OpenPGP fingerprint of the respective key.
    It is suggested to backup those certificates and if the primary private key is not stored on the disk to move them to an external storage device.
    Anyone who can access theses files is able to revoke the corresponding key.
    You may want to print them out.
    You should backup all files in this directory and take care to keep this backup closed away.
    这是gpg存储预先生成的吊销证书的目录.
    文件名对应于相应密钥的OpenPGP指纹.
    建议备份这些证书, 如果主私钥未存储在磁盘上, 则将其移至外部存储设备.
    任何可以访问这些文件的人都可以撤消相应的密钥.
    您可能需要将它们打印出来.
    您应该备份此目录中的所有文件, 并注意不要关闭此备份.
      Operation is further controlled by a few environment variables: 操作由一些环境变量进一步控制:
    HOME Used to locate the default home directory. 用于查找默认主目录.
    GNUPGHOME If set directory used instead of "~/.gnupg". 如果使用设置目录而不是"〜/.gnupg”.
    GPG_AGENT_INFO This variable is obsolete; it was used by GnuPG versions before 2.1. 该变量已过时; 2.1之前的GnuPG版本使用了它.
    PINENTRY_USER_DATA This value is passed via gpg-agent to pinentry.
    It is useful to convey extra information to a custom pinentry.
    该值通过gpg-agent传递给pinentry. 将额外的信息传达给自定义的收藏夹很有用.
    COLUMNS LINES Used to size some displays to the full size of the screen. 线用于将某些显示器的尺寸调整为屏幕的整个尺寸.
    LANGUAGE Apart from its use by GNU, it is used in the W32 version to override the language selection done through the Registry.
    If used and set to a valid and available language name (langid), the file with the translation is loaded from gpgdir/gnupg.nls/langid.mo.
    Here gpgdir is the directory out of which the gpg binary has been loaded.
    If it can't be loaded the Registry is tried and as last resort the native Windows locale system is used.
    除了GNU所使用, 它还用于W32版本中, 以覆盖通过注册表进行的语言选择.
    如果使用并设置为有效和可用的语言名称(langid), 则从gpgdir/gnupg.nls/langid.mo加载包含翻译的文件.
    这里的gpgdir是gpg二进制文件已从其中加载的目录.
    如果无法加载, 请尝试使用注册表, 并且作为最后的选择, 使用本机Windows语言环境系统.
      When calling the gpg-agent component gpg sends a set of environment variables to gpg-agent. The names of these variables can be listed using the command: 调用gpg-agent组件时, gpg会向gpg-agent发送一组环境变量.
    可以使用以下命令列出这些变量的名称:
      gpg-connect-agent 'getinfo std_env_names' /bye | awk '$1=="D" {print $2}'  

    2.2 命令与选项

    签名与验证

    -s, --sign make a signature 签名
    --clear-sign make a clear text signature 做出清晰的文字签名
    -b, --detach-sign make a detached signature 做一个独立的签名
    --verify verify a signature 验证签名

    加密与解密

    -e, --encrypt encrypt data 加密数据
    -c, --symmetric encryption only with symmetric cipher 仅使用对称密码加密
    -d, --decrypt decrypt data (default) 解密数据(默认)

    密钥命令

    -k, --list-keys list keys 清单键
    --list-signatures list keys and signatures 列出密钥和签名
    --check-signatures list and check key signatures 列出并检查密钥签名
    --fingerprint list keys and fingerprints 列出按键和指纹
    -K, --list-secret-keys list secret keys 列出密钥
    --generate-key
    --quick-generate-key
    generate a new key pair
    Quickly
    生成一个新的密钥对
    快速生成
    --full-generate-key full featured key pair generation 全功能密钥对生成
    --generate-revocation generate a revocation certificate 生成吊销证书
    --delete-keys remove keys from the public keyring 从公共密钥环中删除密钥
    --delete-secret-keys remove keys from the secret keyring 从秘密密钥环中删除密钥
    --sign-key
    --quick-sign-key
    sign a key
    Quickly
    签名钥匙
    快速签名
    --lsign-key
    --quick-lsign-key
    sign a key locally
    Quickly
    在本地签名密钥
    快速在本地签名
    --edit-key sign or edit a key 签名或编辑密钥, 操作项目输入help
    --change-passphrase change a passphrase 更改密码
    --quick-revoke-uid quickly revoke a user-id 快速撤消用户ID
    --quick-set-expire quickly set a new expiration date 快速设置新的到期日期
    --quick-add-uid quickly add a new user-id 快速添加新的用户ID
    --export export keys 导出密钥
    --import import/merge keys 导入/合并键
    --send-keys export keys to a keyserver 将密钥导出到密钥服务器
    --receive-keys import keys from a keyserver 从密钥服务器导入密钥
    --search-keys search for keys on a keyserver 在密钥服务器上搜索密钥
    --refresh-keys update all keys from a keyserver 更新密钥服务器中的所有密钥

    其他命令

    --card-status print the card status 打印卡状态
    --edit-card change data on a card 更改卡上的数据
    --change-pin change a card's PIN 更改卡的PIN码
    --update-trustdb update the trust database 更新信任数据库
    --print-md print message digests 打印消息摘要
    --server run in server mode 在服务器模式下运行
    --tofu-policy VALUE set the TOFU policy for a key 为钥匙设置豆腐政策

    公用选项

    -a, --armor create ascii armored output 创建ascii装甲输出
    -r, --recipient USER-ID encrypt for USER-ID 加密USER-ID
    -u, --local-user USER-ID use USER-ID to sign or decrypt 使用USER-ID签名或解密
    -z N set compress level to N (0 disables) 将压缩级别设置为N(0禁用)
    --textmode use canonical text mode 使用规范文本模式
    -o, --output FILE write output to FILE 将输出写入FILE
    -v, --verbose verbose 冗长的
    -n, --dry-run do not make any changes 不要做任何改变
    -i, --interactive prompt before overwriting 覆盖前提示
    --openpgp use strict OpenPGP behavior 使用严格的OpenPGP行为

    几个例子

    -se -r Bob [file] sign and encrypt for user Bob 为用户Bob签名并加密
    --clear-sign [file] make a clear text signature 做出清晰的文字签名
    --detach-sign [file] make a detached signature 做一个独立的签名
    --list-keys [names] show keys 显示键
    --fingerprint [names] show fingerprints 显示指纹
    gpg -sb file make a detached signature 生成独立签名
    gpg -u 0x12345678 -sb file make a detached signature with the key 0x12345678 使用密钥0x12345678进行独立签名
    gpg --verify pgpfile    
    gpg --verify sigfile [datafile] Verify the signature of the file but do not output the data unless requested. 验证文件签名,但除非要求,否则不输出数据。
    The second form is used for detached signatures, where sigfile is the detached signature (either ASCII armored or binary) and datafile are the signed data; if this is not given, the name of the file holding the signed data is constructed by cutting off the extension (".asc" or ".sig") of sigfile or by asking the user for the filename. 第二种形式用于分离的签名,其中sigfile是分离的签名(ASCII铠装或二进制),而datafile是签名的数据;如果未指定,则通过截取sigfile的扩展名(“ .asc”或“ .sig”)或向用户询问文件名来构造包含签名数据的文件的名称。
    If the option --output is also used the signed data is written to the file specified by that option; use - to write the signed data to stdout. 如果还使用了--output选项,则将签名数据写入该选项指定的文件;使用-将签名的数据写入stdout。

    $ gpg --edit-key tom@tom.tom
    gpg> help

    quit quit this menu 退出此菜单
    save save and quit 保存并退出
    help show this help 显示此帮助
    fpr show key fingerprint 显示钥匙指纹
    grip show the keygrip 显示按键
    list list key and user IDs 列出密钥和用户ID
    uid select user ID N 选择用户ID N
    key select subkey N 选择子项N
    check check signatures 检查签名
    sign sign selected user IDs [* see below for related commands] 对选定的用户ID签名[*参见以下相关命令]
    lsign sign selected user IDs locally 在本地签名所选的用户ID
    tsign sign selected user IDs with a trust signature 使用信任签名对选定的用户ID进行签名
    nrsign sign selected user IDs with a non-revocable signature 使用不可撤消的签名对选定的用户ID进行签名
    adduid add a user ID 添加用户ID
    addphoto add a photo ID 添加照片ID (必须为jpg, 建议240x288, 提示时输入图像的完整路径)
    deluid delete selected user IDs 删除所选的用户ID
    addkey add a subkey 添加一个子项
    addcardkey add a key to a smartcard 向智能卡添加密钥
    keytocard move a key to a smartcard 将密钥移至智能卡
    bkuptocard move a backup key to a smartcard 将备份密钥移到智能卡
    delkey delete selected subkeys 删除选定的子项
    addrevoker add a revocation key 添加撤销密钥
    delsig delete signatures from the selected user IDs 从选定的用户标识中删除签名
    expire change the expiration date for the key or selected subkeys 更改密钥或选定的子密钥的到期日期
    primary flag the selected user ID as primary 将所选的用户ID标记为主要
    pref list preferences (expert) 列出首选项(专家)
    showpref list preferences (verbose) 列表首选项(详细)
    setpref set preference list for the selected user IDs 设置所选用户标识的首选项列表
    keyserver set the preferred keyserver URL for the selected user IDs 为选定的用户ID设置首选的密钥服务器URL
    notation set a notation for the selected user IDs 为所选的用户ID设置符号
    passwd change the passphrase 更改密码
    trust change the ownertrust 改变所有者的信任
    revsig revoke signatures on the selected user IDs 撤销所选用户标识上的签名
    revuid revoke selected user IDs 撤销选定的用户ID
    revkey revoke key or selected subkeys 撤销键或选定的子键
    enable enable key 启用密钥
    disable disable key 禁用键
    showphoto show selected photo IDs 显示所选的照片ID
    clean compact unusable user IDs and remove unusable signatures from key 压缩无法使用的用户ID并从密钥中删除无法使用的签名
    minimize compact unusable user IDs and remove all signatures from key 压缩无法使用的用户ID并从密钥中删除所有签名

    3. 使用

    注意:每当命令中需要用户ID时,都可以使用您的密钥ID(key ID), 指纹(fingerprint), 名称的一部分或电子邮件地址等来指定它。GnuPG对此很灵活。

    3.1 生成密钥和吊销证书

    3.1.1 创建密钥对 Create a key pair

    $ gpg --full-gen-key
    提示:使用--expert选项可获取ECC等替代密码。

    该命令将提示您回答几个问题。
    1 选择key类型? 下面是本地的显示的菜单(gnupg 2.2.20-4)
       (1) RSA and RSA (default)
       (2) DSA and Elgamal
       (3) DSA (sign only)
       (4) RSA (sign only)
      (14) Existing key from card
    2 key大小(默认2048),对于大部分用户足够了. 较大的密钥大小4096“几乎没有给我们带来任何好处,同时又使我们付出了很多代价”。
    3 到期日expiration date。对于普通用户来说,一年的时间就足够了。这样,即使丢失了对密钥环的访问,也将使其他人知道它不再有效。以后,如有必要,可以延长到期日期,而不必重新发出新密钥。
    4 用户名: name and email 您的姓名和电子邮件地址。您可以稍后在同一密钥中添加多个身份(例如,如果您有多个电子邮件地址要与该密钥相关联)。
        "tom c (tom) <tom@tom.tom>"
    Change (N)ame, (C)omment, (E)mail or (O)kay/(Q)uit? o
    5 secure passphrase: 安全的密码短语, 就是密码.
    注意:导入密钥的任何人都会看到您在此处输入的名称和电子邮件地址。

    快速创建密钥对
    $ gpg --quick-generate-key tom@tom.tom
    About to create a key for:
        "tom@tom.tom"
    Continue? (Y/n) y
    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 C1B63A41D1D61D69 marked as ultimately trusted
    gpg: revocation certificate stored as '/home/toma/.gnupg/openpgp-revocs.d/EA0648C71CB9EFF989807DB4C1B63A41D1D61D69.rev'
    public and secret key created and signed.

    pub   rsa2048 2020-07-14 [SC] [expires: 2022-07-14]
          EA0648C71CB9EFF989807DB4C1B63A41D1D61D69
    uid                      tom@tom.tom
    sub   rsa2048 2020-07-14 [E]

    3.1.2 生成密钥后,您应该做的第一件事就是创建吊销证书

    After generating your key, one of the first things you should do is create a revocation certificate:
    $ gpg --gen-revoke --armor --output=revocation_certificate.asc user-id
      - revocation_certificate.asc 证书名称
      - user-id 通常是创建密钥的邮箱

    $ gpg --gen-revoke --armor --output=tomkey.rec.asc tom@tom.tom
    $ ls -la tomkey.rec*
    -rw------- 1 toma users 560 Jul 14 09:28 tomkey.rec.asc
    备注: 回头看上一步骤的输出, 已经有吊销凭证了, 保存在默认的隐藏目录下. 这个步骤应该是针对老版本gnupg的.
    当你的密钥丢失或遭到破坏,则可以用该证书撤销您的密钥。
    警告:有权撤消证书的任何人都可以撤消您的密钥。保护您的吊销证书就像保护密钥一样。打印出来,将其保存在磁盘上,然后安全地存储。它足够短,只要您将其打印出来,就可以不费力地手动输入。

    3.1.3 查看密钥

    要列出您的公共钥匙圈中的钥匙:To list keys in your public key ring:
    $ gpg --list-keys
    /home/toma/.gnupg/pubring.kbx
    -----------------------------
    pub   rsa4096 2018-05-28 [C] [expires: 2022-01-01]
          EF9538C9E8E64311A52CDEDFA13D0EF1914E7A72
    uid           [ unknown] Gentoo repository mirrors (automated git signing key) <repomirrorci@gentoo.org>
    sub   rsa2048 2018-05-28 [S] [expires: 2022-01-01]

    pub   dsa1024 2004-07-20 [SC] [expired: 2020-07-01]
          D99EAC7379A850BCE47DA5F29E6438C817072058
    uid           [ expired] Gentoo Linux Release Engineering (Gentoo Linux Release Signing Key) <releng@gentoo.org>

    pub   rsa2048 2020-07-14 [SC] [expires: 2022-07-14]
          EA0648C71CB9EFF989807DB4C1B63A41D1D61D69
    uid           [ultimate] tom@tom.tom
    sub   rsa2048 2020-07-14 [E]

    要列出您的秘密钥匙圈中的钥匙:To list keys in your secret key ring:
    $ gpg --list-secret-keys
    /home/toma/.gnupg/pubring.kbx
    -----------------------------
    sec   rsa2048 2020-07-14 [SC] [expires: 2022-07-14]
          EA0648C71CB9EFF989807DB4C1B63A41D1D61D69
    uid           [ultimate] tom@tom.tom
    ssb   rsa2048 2020-07-14 [E]

    $ gpg --list-secret-keys
    /home/toma/.gnupg/pubring.kbx
    -----------------------------
    sec   rsa2048 2020-07-14 [SC] [revoked: 2020-07-14]
          9B46F40E617AD230C570299C4420FD48AE9B2D38
    uid           [ revoked] tom c (tom) <tom@tom.tom>

    3.2 导出导入公钥

    3.2.1 导出您的公钥

    gpg的主要用途是通过公钥(public-key)加密来确保交换消息的机密性。有了它,每个用户都可以分发其密钥环的公钥,其他人可以使用它来加密发送给用户的消息。私钥(private key)必须始终保持私密,否则会破坏机密性。 有关消息交换的示例,请参见w:公钥加密。
    https://en.wikipedia.org/wiki/Public-key_cryptography
    因此,为了使其他人向您发送加密的消息,他们需要您的公共密钥。
    生成用户公钥的ASCII版本文件public.key(例如,通过电子邮件分发或提供下载):
    $ gpg --output public.key --armor --export user-id
      - public.key 公钥文件名
      - user-id 通常是邮件地址
    $ gpg --output tom-public.key --armor --export tom@tom.com
    $ ls -la *.key
    -rw-r--r-- 1 toma users 1749 Jul 14 09:54 tom-public.key
    提示:添加--no-emit-version以避免打印版本号,或将相应的设置添加到配置文件。

    3.2.2 导入公钥 Import a public key

    $ gpg --import public.key  (public.key 公钥文件名)

    3.3 使用keyserver密钥服务器

    您可以在公共PGP密钥服务器上注册key,以便其他人无需直接与您联系即可检索您的key:
    $ gpg --send-keys key-id  (key-id 就是公钥 public key)
    警告:密钥一旦提交给密钥服务器,就无法从服务器中删除。
    提示:您可以通过以下方式访问key-id: $ gpg --list-secret-keys --keyid-format=LONG <email>. key-id 密钥ID是sec行中提供的哈希密钥。

    要在密钥服务器上查找key的详细信息而不导入它,请执行以下操作:
    $ gpg --search-keys user-id  (user-id 通常是email)

    要从密钥服务器导入key:
    $ gpg --recv-keys key-id  (key-id 就是公钥 public key)

    警告:
    您应通过将检索到的公钥的指纹与所有者在独立来源上发布的指纹进行比较(例如,直接与该人联系),来验证其真实性。 有关更多信息,请参见Wikipedia:公钥指纹。
    https://en.wikipedia.org/wiki/Public_key_fingerprint
    使用短ID可能会遇到冲突。 将导入所有具有短ID的密钥。 为了避免这种情况,请在接收密钥时使用完整的指纹或长密钥ID。use the full fingerprint or long key ID when receiving a key.

    3.4 Web密钥目录

    Web密钥服务(WKS)协议是密钥分发的新标准,其中电子邮件域提供了自己的称为Web密钥目录(WKD)的密钥服务器。
    当加密到电子邮件地址(例如user@example.com)时,GnuPG(> = 2.1.16)将example.com通过HTTPS 向域()查询公开的OpenPGP密钥(如果尚未在本地密钥环中)。
    请注意,该选项auto-key-locate必须包含wkd(默认值)。
    # gpg --recipient user@example.org --auto-key-locate local,wkd --encrypt doc
     - user@example.org
     - doc
    请参阅GnuPG Wiki,以获取支持WKD的电子邮件提供商的列表。如果您自己控制电子邮件地址的域,则可以按照本指南为您的域启用WKD。要检查是否可以在WKD中找到您的密钥,可以使用此Web界面。

    3.5 加密和解密

    3.5.1 不对称

    您需要#导入用户的公共密钥,然后再将文件或消息加密(选项--encrypt或-e)到该收件人(选项--recipient或-r)。此外,如果尚未创建密钥对,则需要#Create。
     - user-id 通常是公钥的邮件地址
     - doc 要加密的原始文件名
     - doc.gpg 已加密的加密文件名
    要加密名称为doc的文件,请使用:
    $ gpg --recipient user-id --encrypt doc
    要解密(选项--decrypt或-d)使用公开密钥加密的名称为doc.gpg 的文件,请使用:
    $ gpg --output doc --decrypt doc.gpg
    gpg会提示您输入密码,然后解密并将数据从doc .gpg写入doc。如果省略-o(--output)选项,则gpg会将解密后的数据写入stdout。

    技巧:
    添加--armor以使用ASCII装甲加密文件(适用于以文本格式复制和粘贴消息)
    使用或代替不将收件人密钥ID放入加密的消息中。这有助于隐藏消息的接收者,并且是针对流量分析的有限对策。-R user-id--hidden-recipient user-id-r
    添加--no-emit-version以避免打印版本号,或将相应的设置添加到您的配置文件。
    您可以使用gnupg通过将您自己的用户ID用作接收者或通过使用--default-recipient-self标志来加密敏感文档。但是,尽管您总是可以对各种文件进行压缩,然后再对其进行加密,但一次只能处理一个文件。如果要加密目录或整个文件系统,请参阅静态数据加密#可用方法。

    3.5.2 对称

    对称加密不需要生成密钥对,并且可以用来简单地用密码对数据加密。只需使用--symmetric或-c执行对称加密:
    $ gpg -c doc
    下面的例子:
    加密doc使用密码与对称密码
    使用AES-256密码算法加密密码短语
    使用SHA-512摘要算法来处理密码短语
    破坏密码短语以进行65536次迭代
    $ gpg -c --s2k-cipher-algo AES256 --s2k-digest-algo SHA512 --s2k-count 65536 doc

    要doc.gpg使用密码短语解密对称加密并将解密后的内容输出到同一目录中,doc请执行以下操作:
    $ gpg --output doc --decrypt doc.gpg

    4. Key维护

    4.1 备份私钥 Backup your private key

    $ gpg --export-secret-keys --armor <user-id> > privkey.asc
    $ gpg --export-secret-keys --armor tom@tom.tom > tom-privkey.asc
    $ ls -la *.asc
    -rw-r--r-- 1 toma users 3612 Jul 14 15:01 tom-privkey.asc

    To import the backup of your private key:
    $ gpg --import privkey.asc

    4.2 撤销你的密钥对 Revoke a key

    If you lose your secret key or it is compromised, you will want to revoke your key by first importing your public key if you not longer have access to the keypair. Then, you will import your revocation certificate:

    $ gpg --import revocation_certificate.asc
    You now need to make your now-revoked key public. If you used a keyserver, send the key to the keyserver. Otherwise, distribute the revoked key to your colleagues.

    或者通过编辑
    $ gpg --edit-key tom@tom.tom
    ...
    gpg> revkey
    ...
    [ revoked] (1). tom@tom.tom
    gpg> quit
    Save changes? (y/N) y

    $ gpg --list-secret-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
    gpg: next trustdb check due at 2022-07-14
    /home/toma/.gnupg/pubring.kbx
    -----------------------------
    sec   rsa2048 2020-07-14 [SC] [revoked: 2020-07-14]
          EA0648C71CB9EFF989807DB4C1B63A41D1D61D69
    uid           [ revoked] tom@tom.tom

    $ gpg --output timp.sig --sign temp.html
    gpg: no default secret key: No secret key
    gpg: signing failed: No secret key

    4.3 编辑 key

    $ gpg --edit-key tom@tom.tom
    ...
    gpg> clean
    User ID "tom c (tom) <tom@tom.tom>": already clean
    Edit your key
    Running the gpg --edit-key <user-id> command will present a menu which enables you to do most of your key management related tasks.
    Some useful commands in the edit key sub menu:
    > passwd       # change the passphrase
    > clean        # compact any user ID that is no longer usable (e.g revoked or expired)
    > revkey       # revoke a key
    > addkey       # add a subkey to this key
    > expire       # change the key expiration time
    > adduid       # add additional names, comments, and email addresses
    > addphoto     # add photo to key (must be JPG, 240x288 recommended, enter full path to image when prompted)
    更多操作请输入help

    4.4 删除 key

    删除私钥
    $ gpg --delete-secret-key tom@tom.tom
    $ gpg --list-secret-keys

    删除公钥
    $ gpg --delete-keys tom@tom.tom
    $ gpg --list-keys

    5. 签名

    签名证明文件并加盖时间戳。如果文档被修改,签名验证将失败。与使用公钥加密文档的加密不同,签名是使用用户的私钥创建的。然后,签名文档的收件人使用发件人的公钥验证签名。
    以下会用到的参数名称:
     - doc.sig 原始文件的签名文件(文本,软件,iso镜像文件; 软件及iso镜像文件通常使用独立签名)
     - doc 原始文件

    5.1 创建签名

    签名文件: 要签名文件,请使用--sign或-s标志:
    $ gpg --output doc.sig --sign doc
    doc.sig包含原始文件的压缩内容doc和二进制格式的签名,但是该文件未加密。但是,您可以将签名与加密结合使用。

    清除签名文件或消息: 要对文件进行签名而不将其压缩为二进制格式,请使用:
    $ gpg --output doc.sig --clearsign doc
    在此,原始文件的内容doc和签名都以易于阅读的形式存储在中doc.sig。

    进行独立签名: 要创建单独的签名文件以与文档或文件本身分开分发,请使用--detach-sig标志:
    $ gpg --output doc.sig --detach-sig doc
    此处的签名存储在中doc.sig,但的内容doc未存储在其中。此方法通常用于分发软件项目,以允许用户验证该程序尚未被第三方修改。
    $ ls -la *.sig
    -rw-r--r-- 1 toma users 546 Jul 14 15:16 timp-clear.sig
    -rw-r--r-- 1 toma users 310 Jul 14 15:17 timp-detach.sig
    -rw-r--r-- 1 toma users 348 Jul 14 15:15 timp.sig

    5.2 验证签名: 要验证签名,请使用--verify标志:

    $ gpg --verify doc.sig
    doc.sig包含您要验证的签名的签名文件在哪里?

    如果要验证分离的签名,则在验证时必须同时存在签名的数据文件和签名文件。例如,要验证Arch Linux的最新iso,您可以执行以下操作:
    $ gpg --verify archlinux-version.iso.sig
    这里必须位于同一目录中。 archlinux-version.iso

    您还可以使用第二个参数指定签名的数据文件:
    $ gpg --verify archlinux-version.iso.sig /path/to/archlinux-version.iso
    如果除了签名之外还对文件进行了加密,则只需解密该文件,它的签名也将得到验证。

    6. 扩展阅读链接

    https://zh.wikipedia.org/wiki/公开密钥加密
    https://en.wikipedia.org/wiki/Public-key_cryptography
    公开密钥密码学(英语:Public-key cryptography)也称非对称式密码学(英语:Asymmetric cryptography)是密码学的一种算法,它需要两个密钥,一个是公开密钥,另一个是私有密钥;公钥用作加密,私钥则用作解密。使用公钥把明文加密后所得的密文,只能用相对应的私钥才能解密并得到原本的明文,最初用来加密的公钥不能用作解密。由于加密和解密需要两个不同的密钥,故被称为非对称加密;不同于加密和解密都使用同一个密钥的对称加密。公钥可以公开,可任意向外发布;私钥不可以公开,必须由用户自行严格秘密保管,绝不透过任何途径向任何人提供,也不会透露给被信任的要通信的另一方。

    基于公开密钥加密的特性,它还能提供数字签名的功能,使电子文件可以得到如同在纸本文件上亲笔签署的效果。

    公开密钥基础建设透过信任数字证书认证机构的根证书、及其使用公开密钥加密作数字签名核发的公开密钥认证,形成信任链架构,已在TLS实现并在万维网的HTTP以HTTPS、在电子邮件的SMTP以SMTPS或STARTTLS引入。

    另一方面,信任网络则采用去中心化的概念,取代了依赖数字证书认证机构的公钥基础设施,因为每一张电子证书在信任链中最终只由一个根证书授权信任,信任网络的公钥则可以累积多个用户的信任。PGP就是其中一个例子。

    与对称密码学的比较
    对称密码是指在加密和解密时使用同一个密钥的方式,公钥密码则是指在加密和解密时使用不同密钥的方式。

    对称密钥加密牵涉到密钥管理的问题,尤其是密钥交换,它需要通信双方在通信之前先透过另一个安全的渠道交换共享的密钥,才可以安全地把密文透过不安全的渠道发送;对称密钥一旦被窃,其所作的加密将即时失效;而在互联网,如果通信双方分隔异地而素未谋面,则对称加密事先所需要的“安全渠道”变得不可行;非对称加密则容许加密公钥随便散布,解密的私钥不发往任何用户,只在单方保管;如此,即使公钥在网上被截获,如果没有与其匹配的私钥,也无法解密,极为适合在互联网上使用。

    另一方面,公钥解密的特性可以形成数字签名,使数据和文件受到保护并可信赖;如果公钥透过数字证书认证机构签授成为电子证书,更可作为数字身份的认证,这都是对称密钥加密无法实现的。

    不过,公钥加密在在计算上相当复杂,性能欠佳、远远不比对称加密;因此,在一般实际情况下,往往通过公钥加密来随机创建临时的对称秘钥,亦即对话键,然后才通过对称加密来传输大量、主体的数据。

    https://zh.wikipedia.org/wiki/对称加密
    https://en.wikipedia.org/wiki/Symmetric-key_algorithm

    公钥指纹
    https://zh.wikipedia.org/wiki/公开密钥指纹
    https://en.wikipedia.org/wiki/Public_key_fingerprint
    在公开密钥加密中,公开密钥指纹(简称:公钥指纹)是用于标识较长公共密钥字节的短序列。指纹通过应用加密散列函数到一个公共密钥来实现。[1]由于指纹较比生成它们的密钥短得多,因此可以用来简化某些密钥的管理任务。
    PGP开发了PGP单词列表,以方便在语音通道上交换公共密钥指纹。

    https://en.wikipedia.org/wiki/Fingerprint_(computing)
    在计算机科学中,指纹识别算法是一个程序映射任意大的数据项(例如计算机文件),以更短的比特串,其指纹,它唯一地标识所有的实际目的的原始数据[1]正如人指纹用于实际目的来唯一地识别人。此指纹可用于重复数据删除目的。这也称为文件指纹,数据指纹或结构化数据指纹。

    PGP单词列表
    https://zh.wikipedia.org/wiki/PGP词汇表
    https://en.wikipedia.org/wiki/PGP_word_list
    PGP词汇表(即“良好隐私密码法词汇表”,又称生物识别词汇表),是一个通过声音频道传输字节时使用的词汇表,目的是清晰表达内容。1995年由计算机语言学家帕特里克·尤奥拉和PGP的创造者菲尔·齐默尔曼发明。[1][2]PGP词汇表与飞行员使用的北约音标字母类似,但此表中的每一个词的值都与256个字节数值一一对应。
    https://en.wikipedia.org/wiki/NATO_phonetic_alphabet

    https://zh.wikipedia.org/wiki/電子簽名
    https://en.wikipedia.org/wiki/Electronic_signature
    https://zh.wikipedia.org/wiki/數位簽章
    https://en.wikipedia.org/wiki/Digital_signature
    https://zh.wikipedia.org/wiki/摩尔斯电码
    https://en.wikipedia.org/wiki/Morse_code
    有些教授记忆摩尔斯电码的方法是使用一张二叉树。
    https://zh.wikipedia.org/wiki/SOS
    https://en.wikipedia.org/wiki/SOS
    SOS的含义
    日常中,SOS通常被理解为:“Save Our Ship”(拯救我们的船)或“Save Our Souls”(拯救我们的灵魂)。但当初制定规范时是没有意思的,纯粹因摩尔斯电码是全点及全横而方便记载。
    SOS的摩尔斯电码 SOS(···---···)
    S、O、S这三个字母的摩尔斯电码分别是“···”、“---”、“···”,念为“滴滴滴”、“答答答”、“滴滴滴”,也就是“三短音”、“三长音”、“三短音”。
    极为简洁明确,即便是未经训练的民众,也可以轻易的利用手边的器物发出这样的信号,或是加以识别。

    https://zh.wikipedia.org/wiki/霍夫曼编码
    https://en.wikipedia.org/wiki/Huffman_coding

    https://zh.wikipedia.org/wiki/SSH客户端比较
    https://en.wikipedia.org/wiki/Comparison_of_SSH_clients
    https://en.wikipedia.org/wiki/Comparison_of_SSH_servers
    https://en.wikipedia.org/wiki/Comparison_of_remote_desktop_software

     
  • 相关阅读:
    PDE_DATA 定义
    每天写日记争创青年艺术家 2014-5-14
    [Servlet3.0新功能]注释替代配置文件
    Flex4+Spring3+Hibernate3+BlazeDS整合笔记
    Flex博客
    hibernate缓存机制详细分析
    计算机的艺术-算法
    算法/数据结构/数学
    线性表 及Java实现 顺序表、链表、栈、队列
    java 图的邻接矩阵
  • 原文地址:https://www.cnblogs.com/sztom/p/13300322.html
Copyright © 2011-2022 走看看