1

在我的项目中,为了实现组密钥协议,我决定为 Diffie Hellman 使用 OpenSSl 的低级 API(代码片段取自文档

#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结构中我如何生成公钥?

4

1 回答 1

1

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

DH_generate_key() 期望 dh 包含共享参数 dh->p 和 dh->g。除非已经设置了 dh->priv_key,否则它会生成一个随机的私有 DH 值,并计算相应的公共值 dh->pub_key,然后可以将其发布。

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

于 2019-01-24T00:31:14.607 回答