C RSA_private_解密失败
我使用openssl。首先,我创建了私钥/公钥,然后加密字符串并将结果保存在文件中。当我试图解密文件时,我的程序失败了。此外,加密文件每次都不同(我使用md5sum checked)。我错过了什么C RSA_private_解密失败,c,linux,openssl,C,Linux,Openssl,我使用openssl。首先,我创建了私钥/公钥,然后加密字符串并将结果保存在文件中。当我试图解密文件时,我的程序失败了。此外,加密文件每次都不同(我使用md5sum checked)。我错过了什么 /* gcc -lssl queation.c -o test_ssl #openssl genrsa -out test_private.key 1024 #openssl rsa -in test_private.key -pubout -out test_publi
/*
gcc -lssl queation.c -o test_ssl
#openssl genrsa -out test_private.key 1024
#openssl rsa -in test_private.key -pubout -out test_public.key
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<openssl/rsa.h>
#include<openssl/pem.h>
#include<openssl/err.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <linux/netdevice.h>
#include <linux/sockios.h>
#include <linux/if.h>
#include <asm/types.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#include <net/if_arp.h>
#include <netinet/if_ether.h>
#include <netinet/ether.h>
#include <fcntl.h>
#include <sys/socket.h>
#define OPENSSLKEY "test_private.key"
#define PUBLICKEY "test_public.key"
#define BUFFSIZE 1024
#define SIZE 1024
#define LIC_FILE "lic.rn"
#define PRTFL printf("fun = %s, line = %d\n", __FUNCTION__,__LINE__)
static char *ptr_en;
static char *p_en;
static RSA *p_rsa_public;
static FILE *fp_public;
static int flen_public, rsa_public_len;
static char *ptr_de;
static char *p_de;
static RSA *p_rsa_private;
static FILE *fp_private;
static int flen_private, rsa_private_len;
void usage( unsigned char * prog_name)
{
printf("usage: %s\n",
prog_name);
exit(1);
}
int main(int argc , char ** argv)
{
int i, ret , len;
unsigned char buf_plain[32];
unsigned char *buf_en;
unsigned char *raw_buffer;
FILE * pf_tmp;
if( argc != 1)
{
usage(argv[0]);
}
snprintf(buf_plain,sizeof(buf_plain),"this is a test line.\n");
if((fp_public=fopen(PUBLICKEY,"r"))==NULL)
{
perror("open public key file error");
ret = -1;
goto error;
}
if((p_rsa_public = PEM_read_RSA_PUBKEY(fp_public,NULL,NULL,NULL))==NULL)
{
ERR_print_errors_fp(stdout);
ret = -1;
goto error;
}
rsa_public_len=RSA_size(p_rsa_public);
p_en=(unsigned char *)malloc(rsa_public_len+1);
memset(p_en,0,rsa_public_len+1);
//printf("%s(%d)p_en = %p,rsa_public_len = %d\n", __FUNCTION__,__LINE__,p_en,rsa_public_len);
len = RSA_public_encrypt(rsa_public_len,buf_plain,p_en,p_rsa_public,RSA_NO_PADDING);
if (len !=rsa_public_len)
{
fprintf(stderr,"Error: len =%d, rsa_public_len = %d,ciphertext should match length of key\n", len,rsa_public_len);
ret = -1;
goto error;
}
pf_tmp = fopen(LIC_FILE,"w");
if( NULL == pf_tmp )
{
printf("open %s failed\n",LIC_FILE);
ret = -1;
goto error;
}
fwrite(p_en,1,128,pf_tmp);
fclose(pf_tmp);
if((fp_private=fopen(OPENSSLKEY,"r"))==NULL)
{
perror("open private key file error");
ret = -1;
goto error;
}
if((p_rsa_private=PEM_read_RSAPrivateKey(fp_private,NULL,NULL,NULL))==NULL)
{
ERR_print_errors_fp(stdout);
ret = -1;
goto error;
}
rsa_private_len = RSA_size(p_rsa_private);
pf_tmp = fopen(LIC_FILE,"r");
if( NULL == pf_tmp )
{
printf("open %s failed\n",LIC_FILE);
ret = -1;
goto error2;
}
raw_buffer = calloc(rsa_private_len,sizeof(char));
if( NULL == raw_buffer )
{
ret = -1;
goto error;
}
len = fread(raw_buffer, sizeof(char),sizeof(raw_buffer), pf_tmp);
if( len <=0 )
{
ret = -1;
goto error;
}
p_de=(unsigned char *)malloc(rsa_private_len+1);
memset(p_de,0,rsa_private_len+1);
//printf("%s(%d)p_en = %p,rsa_public_len = %d\n", __FUNCTION__,__LINE__,p_en,rsa_public_len);
len =RSA_private_decrypt (rsa_private_len,raw_buffer,p_de,p_rsa_private,RSA_NO_PADDING);
printf("%s(%d) p_de = %s\n",__FUNCTION__,__LINE__,p_de);
if ( len != rsa_private_len )
{
fprintf(stderr,"Error: ciphertext should match length of key\n");
exit(1);
}
error2:
fclose(pf_tmp);
error:
free(ptr_en);
free(ptr_de);
fclose(fp_public);
fclose(fp_private);
RSA_free(p_rsa_public);
RSA_free(p_rsa_private);
return ret;
}
/*
gcc-lssl查询.c-o测试\U ssl
#openssl genrsa-out test_private.key 1024
#openssl rsa-in-test_private.key-pubout-out-test_public.key
*/
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#定义OPENSSLKEY“test\u private.key”
#定义公钥“测试公钥”
#定义大小1024
#定义大小1024
#定义LIC_文件“LIC.rn”
#定义PRTFL printf(“fun=%s,line=%d\n”,“函数”,“行”)
静态字符*ptr_en;
静态字符*p_en;
静态RSA*p_RSA_public;
静态文件*fp_public;
静态int flen_public、rsa_public_len;
静态字符*ptr_de;
静态字符*p_de;
静态RSA*p_RSA_private;
静态文件*fp_private;
静态int flen_private,rsa_private_len;
无效用法(未签名字符*程序名)
{
printf(“用法:%s\n”,
项目名称);
出口(1);
}
int main(int argc,字符**argv)
{
内部一级、内部二级、内部三级;
无符号字符buf_平原[32];
无符号字符*buf_en;
无符号字符*原始缓冲区;
文件*pf_tmp;
如果(argc!=1)
{
用法(argv[0]);
}
snprintf(buf_平原,sizeof(buf_平原),“这是一条测试线。\n”);
if((fp_public=fopen(PUBLICKEY,“r”))==NULL)
{
perror(“打开公钥文件错误”);
ret=-1;
转到错误;
}
if((p_rsa_public=PEM_read_rsa_PUBKEY(fp_public,NULL,NULL))==NULL)
{
错误打印错误fp(标准输出);
ret=-1;
转到错误;
}
rsa_public_len=rsa_大小(p_rsa_public);
p_en=(无符号字符*)malloc(rsa_public_len+1);
memset(p_en,0,rsa_public_len+1);
//printf(“%s(%d)p_en=%p,rsa_public_len=%d\n”,函数,行,p_en,rsa_public_len);
len=RSA_public_encrypt(RSA_public_len、buf_plain、p_en、p_RSA_public、RSA_NO_PADDING);
if(len!=rsa\u public\u len)
{
fprintf(标准,“错误:len=%d,rsa\u public\u len=%d,密文应与密钥长度匹配”,len,rsa\u public\u len);
ret=-1;
转到错误;
}
pf_tmp=fopen(LIC_文件,“w”);
if(NULL==pf_tmp)
{
printf(“打开%s失败\n”,LIC\u文件);
ret=-1;
转到错误;
}
fwrite(p_en,1128,pf_tmp);
fclose(pf_tmp);
if((fp_private=fopen(OPENSSLKEY,“r”))==NULL)
{
perror(“打开私钥文件错误”);
ret=-1;
转到错误;
}
if((p_rsa_private=PEM_read_RSAPrivateKey(fp_private,NULL,NULL))==NULL)
{
错误打印错误fp(标准输出);
ret=-1;
转到错误;
}
rsa_private_len=rsa_大小(p_rsa_private);
pf_tmp=fopen(LIC_文件,“r”);
if(NULL==pf_tmp)
{
printf(“打开%s失败\n”,LIC\u文件);
ret=-1;
转到错误2;
}
raw_buffer=calloc(rsa_private_len,sizeof(char));
if(NULL==原始缓冲区)
{
ret=-1;
转到错误;
}
len=fread(原始缓冲区、sizeof(字符)、sizeof(原始缓冲区)、pf_tmp);
如果(len看起来您没有读取整个文件:
len = fread(raw_buffer, sizeof(char),sizeof(raw_buffer), pf_tmp);
请注意,sizeof(raw\u buffer)
是指针的大小,但您将128字节写入了文件(1024位)。因此,您只需读回4或8字节并尝试解密
尝试读回128字节。由于随机填充,正确操作RSA加密每次都会产生不同的结果。感谢Omri Barel,它现在起作用了。另一个问题是为什么每个加密字符串/文件都不同?我太困惑了。@mrliusz-每个加密都是随机的。它会为eac产生不同的结果h运行该算法。