3

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

4

1 回答 1

2

有没有办法不导入用户的密钥环

为防止加载用户密钥环,您需要将上下文的 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;
}
于 2015-04-26T00:02:50.653 回答