0

I am using openssl RSA algorithm to encrypt one file and send it to a server and then send it back with socket elements. But I can't get the original data. To be exactly, sometimes. I attach part of my code here, could you tell my why is it?

This is my code to encrypt and decrypt, I think it is not a problem:

int public_encrypt(unsigned char * data,int data_len, unsigned char *encrypted)
{
    FILE *fp=fopen("public.pem","rb");
    RSA *rsa;
    rsa=PEM_read_RSA_PUBKEY(fp,NULL,NULL,NULL);
    //RSA * rsa = createRSA(key,1);
    int result = RSA_public_encrypt(data_len,data,encrypted,rsa,padding);
    return result;
}
int private_decrypt(unsigned char * enc_data,int data_len, unsigned char *decrypted)
{
    FILE *fp=fopen("private.pem","rb");
    RSA *rsa;
    rsa=PEM_read_RSAPrivateKey(fp,NULL,NULL,NULL);
    //RSA * rsa = createRSA(key,0);
    int  result = RSA_private_decrypt(data_len,enc_data,decrypted,rsa,padding);
    return result;
}

When sending something:

int encrypted_length=public_encrypt(buffer,st.st_size,publicKey,encrypted);

int decrypted_length= private_decrypt(encrypted,256,privateKey,decrypted);

The second line just to test if it can deccrypt well, and it works well all the time

When I am getting data from server, the code is:

int decrypted_length = private_decrypt(buffer,256,privateKey,decrypted);

The buffer is what I send. I used md5 to check the content, they are the same thing. After testing, the result in the private_decrypt is -1 in some case. The result is really unstable I can't find the reason. But I can find that if the file have already there, say I run this for second time with same functio, the result is not -1.

I tried to use ERR_get_error() to get the error, its result is 0x407106b, but I can't check this number anywhere.

Weiheng Li
  • 565
  • 8
  • 19
  • What value are you passing as "padding"? – Jay Nov 18 '16 at 05:36
  • Try using "void ERR_error_string_n(unsigned long e, char *buf, size_t len);" function to get the error string which will give an understandable error. – Jay Nov 18 '16 at 05:38
  • RSA_PKCS1_PADDING – Weiheng Li Nov 18 '16 at 06:12
  • it shows `error:0407109F:lib(4):func(113):reason(159)`, I still can't check it... – Weiheng Li Nov 18 '16 at 06:18
  • Use the below functions and pass the appropriate code which you received: const char *ERR_lib_error_string(unsigned long e); const char *ERR_func_error_string(unsigned long e); const char *ERR_reason_error_string(unsigned long e); – Jay Nov 18 '16 at 06:47
  • @Weiheng Li - `openssl errstr 0x407106b` returns *`error:0407106B:rsa routines:RSA_padding_check_PKCS1_type_2:block type is not 02`*; and `openssl errstr 0x0407109F` returns *`error:0407109F:rsa routines:RSA_padding_check_PKCS1_type_2:pkcs decoding error`*. – jww Nov 18 '16 at 09:09
  • Also see [Encryption and decryption error 0x0407106B using OpenSSL](http://stackoverflow.com/q/8275592), [RSA_private_decrypt() failing randomly: block type is not 02](http://stackoverflow.com/q/30511459) and [BadPaddingException decrypting the encrypted data in Android](http://stackoverflow.com/q/24988787). – jww Nov 18 '16 at 09:36
  • error:0407106B:rsa routines:RSA_padding_check_PKCS1_type_2:block type is not 02 it shows like this in my linux – Weiheng Li Nov 18 '16 at 19:17

0 Answers0