【问题标题】:C GPGMe ignore user keyringC GPGMe 忽略用户密钥环
【发布时间】:2015-04-16 13:43:54
【问题描述】:

我最近完成了一个程序,它将公钥下载到内存中,然后用所有公钥创建一个加密消息。但是,我在创建仅包含我下载的密钥的列表时遇到了一些困难。首次下载时,它们存储在gpgme_data_t 中。我找不到将其直接转换为gpgme_key_t 的函数。因此,我只是将它们导入到新的上下文中。但是,当我再次导出密钥以便为gpgme_op_encrypt 构建列表时,我最终得到了本地密钥环中的其他密钥。我尝试设置disable-gpgconf,但这并没有改变任何东西。我还尝试将GNUPGHOME 设置为 tmp 目录,但是当我调用 encrypt 时这会导致分段错误。有没有办法不导入用户的密钥环或将gpgme_data_tchar* 转换为gpgme_key_t

【问题讨论】:

    标签: c encryption gnupg gpgme


    【解决方案1】:

    有没有办法不导入用户的keyring

    为防止加载用户密钥环,您需要将上下文的 GnuPG 主目录设置为其他位置。

    下面是一个没有任何错误检查的例子

    #include <gpgme.h>
    #include <locale.h>
    
    int main() {
        gpgme_ctx_t ctx;  // the context
        gpgme_error_t err; // errors
        gpgme_key_t key; // the key
        gpgme_keylist_result_t result; // the keylist results
    
        setlocale (LC_ALL, ""); // set the locale
        gpgme_set_locale (NULL, LC_CTYPE, setlocale (LC_CTYPE, NULL)); // set gpgme locale
        gpgme_check_version(NULL); // initialize gpgme
    
        gpgme_new (&ctx); // initialize the context
    
        gpgme_ctx_set_engine_info (ctx, GPGME_PROTOCOL_OpenPGP, NULL, "/tmp/xyz"); // set the context GNUPGHOME to "/tmp/xyz"
    
        gpgme_op_keylist_start (ctx, NULL, 0); // start the keylist
    
        while (!(err = gpgme_op_keylist_next (ctx, &key))) { // loop through the keys in the keyring
            fprintf(stdout, "Key ID: %s\n", key->subkeys->keyid); // print out the keyid
            gpgme_key_unref (key); // release the key reference
        }
    
        gpgme_release(ctx); // release the context, all done
    
        return 0;
    }
    

    【讨论】:

    • gpgme_ctx_set_engine_info (ctx, GPGME_PROTOCOL_OpenPGP, NULL, "/tmp/xyz"); 实际上打破了它,没有那条线它像正常一样工作
    • @735Tesla - 到底发生了什么?
    • 它不会产生任何输出。我得到的错误是它没有初始化。
    • 如果您不将密钥导入“/tmp/xyz”处生成的新密钥环,则不会有任何输出。重点是绕过用户密钥环,这将是一个空的密钥环。
    • 我取出所有其他密钥后导入密钥
    猜你喜欢
    • 1970-01-01
    • 2020-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多