2015-12-01 30 views
0

我是新的密碼學,所以我決定創建一個簡單的程序,打開一個文件加密數據,將其放入etest.txt,然後打開該文件解密並將其放入detest.txt。我知道這聽起來很真實,但它的教育目的。所以這裏是我的代碼。C:無法解密郵件Openssl

#include <openssl/rsa.h> 
#include <openssl/pem.h> 
#include <openssl/err.h> 
#include <stdio.h> 
#include <string.h> 

int main(void) { 
size_t pri_len;   // Length of private key 
size_t pub_len;   // Length of public key 
char *pri_key;   // Private key 
char *pub_key;   // Public key 
char *msg = malloc(256); // Message to encrypt 
char *encrypt = NULL; // Encrypted message 
char *decrypt = NULL; // Decrypted message 
char *err;    // Buffer for any error messages 

// Generate key pair 
RSA *keypair = RSA_generate_key(2048, 3, NULL, NULL); 
FILE *in = fopen("test.txt", "rb"); 
FILE *out = fopen("etest.txt", "wb"); 

if(in == NULL) 
{ 
    printf("in Error is %d (%s).\n", errno, strerror(errno)); 
} 
if(out == NULL) 
{ 
    printf("out Error is %d (%s).\n", errno, strerror(errno)); 
} 

encrypt = malloc(RSA_size(keypair)); 
for(;;) 
{ 
    //213 because of padding 
    memset(msg, '\0', 256); 
    memset(encrypt, '\0', 256); 
    fread(msg, 213, 1, in); 

    if((RSA_public_encrypt(strlen(msg), (unsigned char*)msg, (unsigned char*)encrypt, 
             keypair, RSA_PKCS1_OAEP_PADDING)) == -1) { 
     ERR_load_crypto_strings(); 
     ERR_error_string(ERR_get_error(), err); 
     fprintf(stderr, "Error encrypting message: %s\n", err);   
    } 

    if(fwrite(encrypt, 256, 1, out) != 1) 
    { 
     printf("fwrite Error is %d (%s).\n", errno, strerror(errno)); 
    } 

    if(feof(in)) 
    { 
     break; 
    }  
} 

fclose(in); 
fclose(out); 

in = fopen("etest.txt", "rb"); 
out = fopen("dtest.txt", "wb"); 

if(in == NULL) 
{ 
    printf("in Error is %d (%s).\n", errno, strerror(errno)); 
} 
if(out == NULL) 
{ 
    printf("out Error is %d (%s).\n", errno, strerror(errno)); 
} 

decrypt = malloc(RSA_size(keypair)); 

for(;;) 
{ 
    //I use malloc because if i didnt it would from file and if it filled the msg and if this function would execute second time it would not overwrite the whole buffer and would cause problem 
    memset(decrypt, '\0', 256); 
    memset(msg, '\0', 256); 

    fread(msg, 256, 1, in); 


    if(RSA_private_decrypt(256, (unsigned char*)msg, (unsigned char*)decrypt, 
          keypair, RSA_PKCS1_OAEP_PADDING) == -1) { 
     ERR_load_crypto_strings(); 
     ERR_error_string(ERR_get_error(), err); 
     fprintf(stderr, "Error decrypting message: %s\n", err); 
    } 

    fwrite(decrypt, 256, 1, out); 

    if(feof(in)) 
    { 
     break; 
    } 
} 

fclose(in); 
fclose(out); 
RSA_free(keypair); 
return 0; 

}

當我運行代碼,它給我回錯誤說:Error decrypting message: error:0407A079:rsa routines:RSA_padding_check_PKCS1_OAEP:oaep decoding error,但如果我刪除此代碼memset(msg, '\0', 256);它表明一切正常,但因爲味精緩衝與前幾個字節覆蓋它導致的問題第二個fread()功能被覆蓋。 對不起,如果我的問題聽起來很愚蠢。希望你能幫助。謝謝。

+0

請嘗試以合理的方式格式化您的代碼:刪除許多空行並縮進您的代碼。 –

回答

1

您正在使用的是fwrite(decrypt, 256, 1, out);這是錯誤的。 size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream)第二個參數是要讀取的每個元素的大小(字節)第三個參數是元素的數量,每個元素的大小爲字節。