我最近完成了一个将公钥下载到内存中的程序,然后用它们创建一个加密消息。但是,我在创建一个只下载密钥的列表时遇到了一些困难。当它们第一次下载时,它们被存储在gpgme_data_t中。我无法找到一个直接将其转换为gpgme_key_t的函数。因此,我将它们导入到一个新的上下文中。但是,当我再次导出密钥以便为gpgme_op_encrypt构建一个列表时,我最终使用了本地密钥环中的其他密钥。我试着设置disable-gpgconf,但这并没有改变任何事情。我还尝试将GNUPGHOME设置为tmp目录,但当我调用encrypt时,这导致了分段错误。是否有方法不导入用户的密钥环或将gpgme_data_t或char*转换为gpgme_key_t
发布于 2015-04-26 00:02:50
有没有办法不导入用户的密钥环?
为了防止加载用户密钥环,您需要将上下文的GnuPG homedir设置为其他地方。
下面是一个没有错误检查的示例
#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;
}https://stackoverflow.com/questions/29676626
复制相似问题