C:无法解密消息Openssl

时间:2015-12-01 16:30:46

标签: c encryption file-io openssl rsa

我是加密的新手,所以我决定创建一个简单的程序,打开文件加密数据,将其放入etest.txt,然后打开此文件解密并将其放入detest.txt。我知道这听起来真的很受欢迎,但是出于教育目的。所以这是我的代码。

#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);它会显示一切正常但它会导致问题,因为msg缓冲区被前几个字节覆盖第二个fread()函数覆盖了。 对不起,我的问题听起来很傻。希望你能帮忙。感谢。

1 个答案:

答案 0 :(得分:1)

你正在使用fwrite(decrypt, 256, 1, out);这是错误的。size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream)第二个参数是要读取的每个元素的字节大小而第三个是元素的数量,每个元素的大小都是字节大小