错误信息:
RSA_private_decrypt: No such file or directory,这里面哪有文件啊?求解惑
解密代码:
#define PRIKEY "./prikey.pem"
char *decrypted=NULL;
char *my_decrypt(unsigned char *str, char *prikey_path)
{
OpenSSL_add_all_algorithms();//有些算法必须这样初始化。
//打开私钥文件
FILE* priv_fp=fopen(PRIKEY,"r");
if(priv_fp==NULL){
printf("failed to open PRIKEY file %s!\n", PRIKEY);
return NULL;
}
// 从文件中读取私钥
RSA *rsa2 = PEM_read_RSAPrivateKey(priv_fp, NULL, NULL, NULL);
if(rsa2==NULL){
printf("unable to read private key!\n");
return NULL;
}
// 用私钥解密
int rsa_len = RSA_size(rsa2);
printf("rsa_len: %d\n", rsa_len);
//char *decrypted=NULL;
decrypted = (char *)malloc(1024);
memset(decrypted, 0, 1024);
printf("strlen(str): %d\n", strlen(str));
int len=RSA_private_decrypt(rsa_len, str, decrypted, rsa2,RSA_PKCS1_PADDING);// RSA_NO_PADDING
if(len==-1){
perror("RSA_private_decrypt:");
return NULL;
}
fclose(priv_fp);
return decrypted;
}