gpt4 book ai didi

c - LibCrypt 如何使用低级 api 获取我生成的公钥

转载 作者:太空宇宙 更新时间:2023-11-04 03:13:13 24 4
gpt4 key购买 nike

在我的项目中,为了实现组 key 协议(protocol),我决定为 Diffie Hellman 使用 OpenSSL 的低级 API(代码片段取自 documentation)

#include <libssl/dh.h>
// Some code here

DH *privkey;
int codes;
int secret_size;

/* Generate the parameters to be used */
if(NULL == (privkey = DH_new())) handleErrors();
if(1 != DH_generate_parameters_ex(privkey, 2048, DH_GENERATOR_2, NULL)) handleErrors();

if(1 != DH_check(privkey, &codes)) handleErrors();
if(codes != 0)
{
/* Problems have been found with the generated parameters */
/* Handle these here - we'll just abort for this example */
printf("DH_check failed\n");
abort();
}

/* Generate the public and private key pair */
if(1 != DH_generate_key(privkey)) handleErrors();

/* Send the public key to the peer.
* How this occurs will be specific to your situation (see main text below)
*/

// Another code here

//Cleanups
OPENSSL_free(secret);
BN_free(pubkey);
DH_free(privkey);

但是如何从生成的 DH 结构生成公钥?

最佳答案

如果您阅读 DH_generate_key 的文档,确实如此(正如评论所说)。

DH_generate_key() expects dh to contain the shared parameters dh->p and dh->g. It generates a random private DH value unless dh->priv_key is already set, and computes the corresponding public value dh->pub_key, which can then be published.

因此,Diffie Hellman 交换的公共(public)“ key ”部分位于“privkey->pub_key”中,您将其与共享参数“privkey->p”一起发布>”和“privkey->g”到另一边。

关于c - LibCrypt 如何使用低级 api 获取我生成的公钥,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54334633/

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