3,881
社区成员
发帖
与我相关
我的任务
分享
/*********************************************
* 函数功能:保存私钥到文件
* 传入参数:RSA结构体
* 传出参数:无
* 返 回 值:成功返回1否则0
********************************************/
int saveprikey(RSA *rsa)
{
FILE *file;
if (NULL == rsa)
{
printf("RSA not initial.\n");
return 0;
}
file = fopen("prikey.key","wb");
if (NULL == file )
{
printf("create file 'prikey.key' failed!\n");
return 0;
}
PEM_write_RSAPrivateKey(file, rsa, NULL, NULL, 512, NULL, NULL);
fclose(file);
return 1;
}
/****************************************
*函数功能:读取密钥存储文件,获取私钥
*传入参数:rsa RSA结构体指针
*传出参数:无
*返回值 :rsa RSA结构体指针,失败为NULL
****************************************/
RSA* getprikey(RSA *rsa)
{
FILE *file;
if (NULL == rsa)
{
printf("RSA not initial!\n");
return NULL;
}
file = fopen("prikey.key", "rb");
if (NULL == file)
{
printf("open file 'prikey.key' failed!\n");
return NULL;
}
PEM_read_RSAPrivateKey(file, &rsa, NULL, NULL);
fclose(file);
return rsa;
}
BIO *bio = NULL;
bio = BIO_new(BIO_s_file());
if (BIO_read_filename(bio, filename) <= 0)
{
return ;
}
RSA *rsa = d2i_RSAPublicKey(bio, NULL);