2016-07-02 65 views
0

我使用這些函數來加密和解密文本文件轉換成使用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; 
} 
+1

由於可加密數據的大小限制僅限於密鑰長度,因此RSA不適用於加密文件。數據加密通常用對稱算法完成,如AES。 – zaph

回答

0
fputs((const char *)encrypted_data,file); 

的問題是在這裏。加密的數據是而不是的C風格的字符串,只是將其轉換爲const char *並將它傳遞給一個採用C風格字符串的函數將不起作用。

+0

什麼解決方案可以作爲替代方案? – f0unix

+0

提示:public_key_encryption()返回「加密」數據的長度(由RSA_public_encrypt()返回)... – TonyB

+0

@ f0unix您是否注意到您的代碼忽略了'result'? –