我需要一些帮助算法Diffie Hellman在openssl我有素数(p),生成器(g),用户A的私钥和用户B的公钥。我需要计算共享密钥。我写了这段代码,但代码一直执行到这一行
int dhSize = DH_size(dh->priv_key);
这是完整的代码:
#include <stdio.h>
#include <openssl/dh.h>
const char* userA_PrivateKey = "90ff0";
const char* userB_PublicKey = "9d1a59";
const char* p = "66c2fa";
const char* g = "2";
int main(void)
{
DH *dh = DH_new();
BN_dec2bn(&dh->g, g);
BN_hex2bn(&dh->p, p);
BN_hex2bn(&dh->priv_key, userA_PrivateKey);
BIGNUM *pubKeyUserB = NULL;
BN_dec2bn(&pubKeyUserB, userB_PublicKey);
//Compute the shared secret
int secret_size;
unsigned char *secret;
printf(" Compute DH_size \n");
int dhSize = DH_size(dh->priv_key);
printf(" dhSize = %d \n"); //NOT EXECUTED
secret = OPENSSL_malloc(sizeof(unsigned char) * dhSize);
if(0 > (secret_size = DH_compute_key(secret, pubKeyUserB, dh->priv_key)))
{
printf("error \n");
}
return 0;
}
我有两个问题:
1)printf,打印dhSize根本不执行
2)我不确定我是否正确设置了值g, p,priv键?函数DH_compute_key会使用我的g和p吗?
你在犯愚蠢的错误:
>
dhSize
应该输入为DH_size
(~第24行)和DH_size
函数计算struct DH
的大小给定一个const struct DH*
您正在传递它dh-
在使用DH_compute_key
(~第28行)时出现类似的错误,第三个参数应该是dh
而不是dh-
请相应修复并重试