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.