[英]Error while generating key with GPGME
I'm trying to generate a new key with GPGME but unfortunately I can't make the following code work: 我正在尝试使用GPGME生成新密钥,但是很遗憾,我无法使以下代码正常工作:
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;
}
}
}
The condition error == GPG_ERR_NO_ERROR
is not true. 条件
error == GPG_ERR_NO_ERROR
不正确。 Indeed none of the error checking conditions is. 实际上,没有任何错误检查条件。 My debugger just tells me that the value of
error
is 117440567.The initialisation looks like this: 我的调试器仅告诉我
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;
I don't get any errors here. 我在这里没有任何错误。 I'm perfectly able to create keys by using GnuPG command line.
我完全能够使用GnuPG命令行创建密钥。 So what does this strange error while creating a key with GPGME mean?
那么,用GPGME创建密钥时出现的奇怪错误是什么意思呢?
UDAPTE: UDAPTE:
Thanks to Auges answer I now can tell it is a GPG_ERR_INV_VALUE
meaning the param string is malformed. 多亏了Auges的回答,我现在才知道它是
GPG_ERR_INV_VALUE
表示参数字符串格式错误。 But ... why? 但为什么? Do I have to separate the values by something more than a blank?
我是否需要将值之间用空格隔开?
UPDATE 更新
I changed the quotes of internal 我更改了内部报价
GnupgKeyParms format=\"internal\"
But now I'm getting a GPG_ERR_GENERAL
. 但是现在我得到了
GPG_ERR_GENERAL
。
UPDATE: 更新:
The string now looks like: 字符串现在看起来像:
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>";
But the application hangs at gpgme_op_genkey
. 但是应用程序挂在
gpgme_op_genkey
。
If you take at look at the documentation you find that gpgme_errot_t
cannot be copared against an error code: 如果您看一下文档,会发现
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.
但是,可以确保仅使用0表示成功(GPG_ERR_NO_ERROR),并且在这种情况下,错误值的所有其他部分也都设置为0。
Still the value of error
should be zero. error
值仍应为零。 With that I can't help you 这样我帮不了你
You could try using gpgme_err_code
to find out which error occurred. 您可以尝试使用
gpgme_err_code
找出发生哪个错误。
The program stopped while generating the key because too less entropy was available on the system (it is a debian VM). 该程序在生成密钥时停止,因为系统上的熵太少(它是debian VM)。 For others that might stumble across this question: You can see how much entropy is available in debian (and other linux distributions) by looking at /proc/sys/kernel/random/entropy_avail.
对于其他可能会偶然发现此问题的人:通过查看/ proc / sys / kernel / random / entropy_avail,您可以查看debian(和其他Linux发行版)中有多少熵。 I installed haveged (available in package manager) and re-run the code.
我安装了Haveged(可在软件包管理器中找到)并重新运行代码。 Everything worked fine.
一切正常。 Many thanks to the mailing list of GnuPG , which helped me figure out this solution.
非常感谢GnuPG的邮件列表 ,它帮助我弄清楚了该解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.