char my_encrypt(char str, char *pubkey_path)
{
// 打开公钥文件
FILE pub_fp=fopen(PUBKEY,"r");
if(pub_fp==NULL){
printf("failed to open PUBKEY file %s!\n", PUBKEY);
return ;
}
// 从文件中读取公钥
RSA rsa1=PEM_read_RSA_PUBKEY(pub_fp, NULL, NULL, NULL);
if(rsa1==NULL)
{
printf("unable to read public key!\n");
return ;
}
fclose(pub_fp);
printf("strlen(str): %d\n",strlen(str));
printf("RSA_size(rsa1): %d\n",RSA_size(rsa1));
char *encrypted=NULL;
encrypted = (char *)malloc(1024);
memset(encrypted, 0, 1024);
// 用公钥加密
int len=RSA_public_encrypt(strlen(str), str, encrypted, rsa1, RSA_PKCS1_PADDING);
if(len==-1 )
{
printf("failed to encrypt\n");
return ;
}
return encrypted;
}