我使用这些函数来加密和解密文本文件转换成使用RSA_public_encrypt和RSA_private_decryptOpenSSL的çRSA库解密
当启动命令行程序以作为输入公共密钥文件名或私有密钥的输出文本文件文件名,加密过程工作得很好,而解密总是失败。
下面是我调用的加密文件函数,它调用readRSAKeyFromFile返回RSA数据类型,以便稍后处理。
如果我在这里错过了一些东西,请告诉我。
我对C来说很新鲜,我认为试着写一些东西作为测试来理解C基础知识。
您的帮助将是非常赞赏
无效enc_file(字符* pub_key_name,字符* 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;
}
由于可加密数据的大小限制仅限于密钥长度,因此RSA不适用于加密文件。数据加密通常用对称算法完成,如AES。 – zaph