在我的项目中,为了实现组密钥协议,我决定为 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
结构中我如何生成公钥?