gpt4 book ai didi

c++ - 读取受 3DES 保护的 DSA 私钥

转载 作者:行者123 更新时间:2023-11-30 05:46:06 29 4
gpt4 key购买 nike

以下是我成功创建 key 对并存储在文件中的方法。问题出现在读取受密码保护的私钥时,我总是得到 NULL。解决这个问题的任何提示?谢谢。

FILE *dsa_privatekey_file;
FILE *dsa_publickey_file;

const char *pkeykey = "password";

int result = 0;

DeleteFile("dsapub.pem");
DeleteFile("dsapriv.pem");

dsa_publickey_file = fopen("dsapub.pem", "r");
dsa_privatekey_file = fopen("dsapriv.pem", "r");

if (dsa_privatekey_file == NULL || dsa_publickey_file == NULL)
{
if (dsa_privatekey_file != NULL)
{
fclose(dsa_privatekey_file);
}
if (dsa_publickey_file != NULL)
{
fclose(dsa_publickey_file);
}

dsa_publickey_file = fopen("dsapub.pem", "w");
dsa_privatekey_file = fopen("dsapriv.pem", "w");

DSA* dsa = DSA_new();
result = DSA_generate_parameters_ex(dsa, 2048, NULL, 0, NULL, NULL, NULL);
result = DSA_generate_key(dsa);

result = PEM_write_DSAPrivateKey(dsa_privatekey_file, dsa, EVP_des_ede3_cbc(), NULL, 0, NULL, (void *)pkeykey);
//result = PEM_write_DSAPrivateKey(dsa_privatekey_file, dsa, EVP_des_ede3_cbc(), (unsigned char *) pkeykey, strlen(pkeykey), NULL, NULL);
//result = PEM_write_DSAPrivateKey(dsa_privatekey_file, dsa, NULL, NULL, 0, NULL, NULL);

result = PEM_write_DSA_PUBKEY(dsa_publickey_file, dsa);

fclose(dsa_privatekey_file);
fclose(dsa_publickey_file);

DSA_free(dsa);

dsa_publickey_file = fopen("dsapub.pem", "r");
dsa_privatekey_file = fopen("dsapriv.pem", "r");
}

DSA *dsa_sign = PEM_read_DSAPrivateKey(dsa_privatekey_file, NULL, default_set_password, (void *)pkeykey);

DSA *dsa_verify = PEM_read_DSA_PUBKEY(dsa_publickey_file, NULL, NULL, NULL);

这是我用来硬插入密码的简单函数,但无论如何它似乎没有被调用。

static int default_set_password(char *buf, int size, int rwflag, void *descr){ memcpy(buf, descr, size); return size; }

最佳答案

调用

OpenSSL_add_all_algorithms(); 

需要按预期工作。

关于c++ - 读取受 3DES 保护的 DSA 私钥,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29102645/

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