我正在使用这些函数使用RSA_public_encrypt和RSA_private_decrypt将文本文件加密和解密为输出文本文件
在启动命令行程序作为输入公钥文件名或私钥文件名时,加密过程正常工作,而解密总是失败。
下面是我调用的加密文件函数,调用readRSAKeyFromFile返回RSA数据类型,以便以后处理。
如果我遗失了什么,请告诉我。
我是C的新手,我想尝试写一些东西作为测试来理解C基础知识。
非常感谢您的帮助
void enc_file(char * pub_key_name,char * file_name){
RSA *rsa = readRSAKeyFromFile(pub_key_name, 1);
char *data_read_from_file;
long fileSize = 0;
unsigned char *encrypted_data = (unsigned char*)malloc( RSA_size(rsa) ) ;
FILE * stream = fopen (file_name, "rb");
//Seek to the end of the file to determine the file size
fseek(stream, 0L, SEEK_END);
fileSize = ftell(stream);
fseek(stream, 0L, SEEK_SET);
//Allocate enough memory (add 1 for the \0, since fread won't add it)
data_read_from_file = malloc(fileSize+1);
//Read the file
size_t size=fread(data_read_from_file,1,fileSize,stream);
data_read_from_file[size]= 0; // Add terminating zero.
fclose(stream);
int result = public_key_encryption(data_read_from_file, encrypted_data, rsa);
free(data_read_from_file);
FILE * file = fopen("encrypted_data.txt","w+");
fputs((const char *)encrypted_data,file);
fclose(file);
printf(" %s \n", encrypted_data );
if( result == -1 ){
perror("Couldn't encrypt file");
}else{
printf("[*] Successfully encrypted data \n" );
}
}
void dec_file(char *priv_key_name, char *file_name){
RSA *rsa = readRSAKeyFromFile(priv_key_name, 0);
char *data_read_from_file;
long fileSize = 0;
unsigned char *decrypted_data = (unsigned char*)malloc( RSA_size(rsa) ) ;
FILE * stream = fopen (file_name, "rb");
//Seek to the end of the file to determine the file size
fseek(stream, 0L, SEEK_END);
fileSize = ftell(stream);
fseek(stream, 0L, SEEK_SET);
//Allocate enough memory (add 1 for the \0, since fread won't add it)
data_read_from_file = malloc(fileSize+1);
//Read the file
size_t size=fread(data_read_from_file,1,fileSize,stream);
data_read_from_file[size]= 0; // Add terminating zero.
fclose(stream);
int result = private_key_decryption(data_read_from_file, decrypted_data, rsa);
free(data_read_from_file);
FILE * file = fopen("encrypted_data.txt","w+");
fputs((const char *)decrypted_data,file);
fclose(file);
printf(" %s \n", decrypted_data );
if( result == -1 ){
perror("Couldn't encrypt file");
}else{
printf("[*] Successfully decrypted data \n" );
}
}
RSA * readRSAKeyFromFile(char * filename,int is_public){
FILE * rsa_pkey_file = fopen(filename,"r");
if(rsa_pkey_file == NULL){
printf("ERROR opening file :: %s \n",filename);
return NULL;
}
// RSA * rsa_key= RSA_new();
RSA *rsa_pkey = NULL;
if(is_public == 1 ){
PEM_read_RSA_PUBKEY(rsa_pkey_file, &rsa_pkey, NULL, NULL);
}else{
PEM_read_RSAPrivateKey(rsa_pkey_file, &rsa_pkey, NULL, NULL);
}
return rsa_pkey;
}
int public_key_encryption( char *data, unsigned char *encrypted, RSA *rsa_key){
int result = RSA_public_encrypt( (int)strlen(data), (const unsigned char*)data, encrypted, rsa_key, RSA_PKCS1_PADDING ) ;
return result;
}
int private_key_decryption(char * data, unsigned char *decrypted, RSA *rsa_key){
int result = RSA_private_decrypt((int)strlen(data),(const unsigned char *)data,(unsigned char*)decrypted,rsa_key,RSA_PKCS1_PADDING);
return result;
}
答案 0 :(得分:0)
fputs((const char *)encrypted_data,file);
问题出在这里。加密数据不是C风格的字符串,只需将其转换为const char *
并将其传递给采用C风格字符串的函数将无效。