gpt4 book ai didi

c++ - 使用 GPGME 生成 key 时出错

转载 作者:行者123 更新时间:2023-11-30 03:45:11 26 4
gpt4 key购买 nike

我正在尝试使用 GPGME 生成一个新 key ,但遗憾的是我无法使以下代码工作:

 std::string def = "<GnupgKeyParms format='internal'>"
" Key-Type: default "
" Subkey-Type: default"
" Name-Real: Joe Tester"
" Name-Comment: with stupid passphrase"
" Name-Email: joe2@foo.bar"
" Expire-Date: 0"
" Passphrase: abc"
" </GnupgKeyParms>";

gpgme_error_t error = gpgme_op_genkey(mContext, def.c_str(), NULL, NULL);

if(GPG_ERR_INV_VALUE == error){
std::cout << "Value error";
}
if(GPG_ERR_NOT_SUPPORTED == error){
std::cout << "Not supported error";
}
if(GPG_ERR_GENERAL == error){
std::cout << "general error";
}
if(error == GPG_ERR_NO_ERROR){
gpgme_genkey_result_t res = gpgme_op_genkey_result(mContext);
if(res->primary && res->sub){
result = true;
}
}
}

条件 error == GPG_ERR_NO_ERROR 不成立。事实上,没有一个错误检查条件是。我的调试器只是告诉我 error 的值为 117440567。初始化如下所示:

gpgme_engine_info_t info;
gpgme_error_t error;
const char * CONFIG_DIR = "/";

// Initializes gpgme
gpgme_check_version(NULL);

// Initialize the locale environment.
setlocale(LC_ALL, "");
gpgme_set_locale(NULL, LC_CTYPE, setlocale(LC_CTYPE, NULL));
#ifdef LC_MESSAGES
gpgme_set_locale(NULL, LC_MESSAGES, setlocale(LC_MESSAGES, NULL));
#endif
error = gpgme_set_engine_info(GPGME_PROTOCOL_OpenPGP, NULL,
CONFIG_DIR);
if(error)
return false;
error = gpgme_new(&mContext);
if(error)
return false;
// Check OpenPGP
error = gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP);
if(error)
return false;
// load engine info
error = gpgme_get_engine_info(&info);
if(error)
return false;
while(info && info->protocol != gpgme_get_protocol(mContext)) {
info = info->next;
}

if(error)
return false;

我在这里没有收到任何错误。我完全能够使用 GnuPG 命令行创建 key 。那么在使用 GPGME 创建 key 时出现的这个奇怪错误是什么意思呢?

UDAPTE:

感谢 Auges 的回答,我现在可以说它是 GPG_ERR_INV_VALUE,这意味着参数字符串格式错误。但为什么?我必须用空格以外的东西来分隔值吗?

更新

我更改了内部引号

GnupgKeyParms format=\"internal\"

但现在我得到一个GPG_ERR_GENERAL

更新:

字符串现在看起来像:

std::string def = "<GnupgKeyParms format=\"internal\"> \n"
" Key-Type: default \n"
" Subkey-Type: default \n"
" Name-Real: Joe Tester3 \n"
" Name-Comment: with stupid passphrase \n"
" Name-Email: joe3@foo.bar \n"
" Expire-Date: 0 \n"
" Passphrase: abc \n"
" </GnupgKeyParms>";

但应用程序卡在 gpgme_op_genkey

最佳答案

如果你看看documentation您发现 gpgme_errot_t 无法与错误代码进行比较:

... the error value can not be directly compared against an error code, but the accessor functions described below must be used. However, it is guaranteed that only 0 is used to indicate success (GPG_ERR_NO_ERROR), and that in this case all other parts of the error value are set to 0, too.

error 的值仍然应该为零。我帮不了你了

您可以尝试使用 gpgme_err_code 找出发生的错误。

关于c++ - 使用 GPGME 生成 key 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34994224/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com