title: After I send my encrypted data to server and get it back, I can't decrypt it unstable result
body: <p>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?</p>
  
  <p>This is my code to encrypt and decrypt, I think it is not a problem:</p>
  
  <pre><code>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;
  }
  </code></pre>
  
  <p>When sending something:</p>
  
  <pre><code>int encrypted_length=public_encrypt(buffer,st.st_size,publicKey,encrypted);
  
  int decrypted_length= private_decrypt(encrypted,256,privateKey,decrypted);
  </code></pre>
  
  <p>The second line just to test if it can deccrypt well, and it works well all the time</p>
  
  <p>When I am getting data from server, the code is:</p>
  
  <pre><code>int decrypted_length = private_decrypt(buffer,256,privateKey,decrypted);
  </code></pre>
  
  <p>The buffer is what I send. I used md5 to check the content, they are the same thing. After testing, the <code>result</code> in the <code>private_decrypt</code> 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.</p>
  
  <p>I tried to use <code>ERR_get_error()</code> to get the error, its result is 0x407106b, but I can't check this number anywhere.</p>