【问题标题】:Why can't I run gpg in non-interactive mode successfully?为什么我不能在非交互模式下成功运行 gpg?
【发布时间】:2016-05-09 11:11:29
【问题描述】:

我正在编写一个使用gpg 加密文件的脚本。在命令行使用gpg 进行测试/实验期间,我发现了一些奇怪的行为。这工作得很好:

$ cat myFile.txt | gpg --encrypt -r 'jdoe@gmail.com'
gpg: B2D17635: There is no assurance this key belongs to the named user

pub  4096R/B2D17635 2016-01-31 John Doe (I am now a real person.) <jdoe@gmail.com>
 Primary key fingerprint: B17F 98BA 1DA9 3FE1 A08F  1443 509D 87ED 32AF 2078
      Subkey fingerprint: BB63 42DA 8FAD 194A E1C9  1F6D 39BA 73B9 B2D1 7635

It is NOT certain that the key belongs to the person named
in the user ID.  If you *really* know what you are doing,
you may answer the next question with yes.

Use this key anyway? (y/N) y
�
Nϴ��[�mDZ.@�Bc���J������z�{p���%
<GIBBERISH SNIPPED>
i�)��/&N��t�Z�8�#�I<�Bq�!�K?�vQ�I�H6&+��(

但我不喜欢这样,因为我必须交互式地输入“y”。我希望它假设“是”并在不需要任何交互性的情况下进行加密。所以我使用--batch--yes 开关运行了以下命令。为什么失败了?

$ cat myFile.txt | gpg --encrypt --batch --yes -r 'jdoe@gmail.com'
gpg: B2D17635: There is no assurance this key belongs to the named user
gpg: [stdin]: encryption failed: unusable public key

【问题讨论】:

标签: gnupg


【解决方案1】:

您必须将--always-trust 添加到您的命令中:

echo "test" | gpg --batch --yes --always-trust --encrypt --armor -r "mail@example.com"

【讨论】:

    【解决方案2】:

    您从 GnuPG 收到的错误是因为公钥在您的密钥环中不受信任/验证。因为您的 OP 声明您正在运行测试,所以您可能希望查看为我自己的实验编写的帮助程序脚本中的代码,GnuPG_Gen_Key.sh,特别是下面复制/修改的函数。

    #!/usr/bin/env bash
    Var_gnupg_import_key="${1}"
    Var_gnupg_import_key_trust="${2}"
    Func_import_gnupg_key_edit_trust(){
        _gnupg_import_key="${1:-${Var_gnupg_import_key}}"
        gpg --no-tty --command-fd 0 --edit-key ${_gnupg_import_key} <<EOF
    trust
    ${Var_gnupg_import_key_trust}
    quit
    EOF
    }
    Func_import_gnupg_key(){
        _gnupg_import_key="${1:-${Var_gnupg_import_key}}"
        if [ -f "${_gnupg_import_key}" ]; then
            echo "# ${Var_script_name} reports: importing key file [${_gnupg_import_key}]"
            gpg --no-tty --command-fd 0 --import ${_gnupg_import_key} <<EOF
     trust
    ${Var_gnupg_import_key_trust}
    quit
    EOF
        else
            _grep_string='not found on keyserver'
            gpg --dry-run --batch --search-keys ${_gnupg_import_key} --keyserver ${Var_gnupg_key_server} | grep -qE "${_grep_string}"
            _exit_status=$?
            if [ "${_exit_status}" != "0" ]; then
                _key_fingerprint="$(gpg --no-tty --batch --dry-run --search-keys ${_gnupg_import_key} | awk '/key /{print $5}' | tail -n1)"
                _key_fingerprint="${_key_fingerprint//,/}"
                if [ "${#_key_fingerprint}" != "0" ]; then
                    echo "# ${Var_script_name} reports: importing key [${_key_fingerprint}] from keyserver [${Var_gnupg_key_server}]"
                    gpg --keyserver ${Var_gnupg_key_server} --recv-keys ${_key_fingerprint}
                    Func_import_gnupg_key_edit_trust "${_gnupg_import_key}"
                else
                    echo "# ${Var_script_name} reports: error no public key [${_gnupg_import_key}] as file or on key server [${Var_gnupg_key_server}]"
                fi
            else
                echo "# ${Var_script_name} reports: error no public key [${_gnupg_import_key}] as file or on key server [${Var_gnupg_key_server}]"
            fi
        fi
    }
    

    可以使用上述方法信任公钥,也可以使用以下命令让 GnuPG 忽略信任问题。

    gpg --armor --always-trust -r 'jdoe@gmail.com' -e myFile.txt -o myFile.txt.gpg
    

    请注意,我添加了 --armor 选项,因为 OP 中的输出基于截断的输出看起来错过了该选项。

    【讨论】:

      猜你喜欢
      • 2021-07-25
      • 1970-01-01
      • 2017-02-05
      • 2017-03-15
      • 2022-06-30
      • 2023-03-09
      • 1970-01-01
      • 1970-01-01
      • 2023-03-17
      相关资源
      最近更新 更多