title: RSA_private_decrypt() failing randomly: block type is not 02
body: <p>I am using openssl 0.9.6g with C++. I have generated RSA public/private keypair. I have <a href="http://openssl-net.sourceforge.net/" rel="nofollow">openssl.net</a> framework which is based on 1.0.0d version of openssl. I am encrypting text using public key with openssl.net and when I try to decrypt in my C++ code, I am getting random failure. Sometimes I am able to retrieve the data but some other times I am getting following error:</p>
  
  <pre><code>error:0407106B:rsa routines:RSA_padding_check_PKCS1_type_2:block type is not 02
  </code></pre>
  
  <p>I am not sure what exactly is happening. If I use the encrypted data and the private key in openssl.net (which originally encrypted the data), I am able to decrypt. </p>
  
  <p>My C++ code is:</p>
  
  <pre><code>const char *p="-----BEGIN RSA PRIVATE KEY-----\n\
  MIICXgIBAAKBgQDOx0DsbBURESmUVivbiHXqJdaJw8j3v27RC5ArUKIFbzgoqge+\n\
  ZA9IcWA+WDjW4auzrzhK3sQTHRbkYCkSZsHdkHFu1viYDtK0Y1PUzm45xGAjOD2R\n\
  TbYTMDyUAwDwERoIamPFLa/kQ/4AQnL6F2C6NQPtnkCITLsB0+pbQLvLzwIDAQAB\n\
  AoGBAJ7aDfNSuQBqieHOLVUyyTs/JEijowFJDG4GDxRnloDMR31d2iMWpXMGvmRG\n\
  zCuwJIMeqgVCi6kPYOXDRepADM+zPo3Qh6hSS5AhzgY27xitjOqZVrJX7J/Y1N+i\n\
  Fd+GPvj7FuOvQTQw9MVkEDQYvTaC3hXVOS5akzqwes5gAL45AkEA6OMl4+ROmDhV\n\
  bBwif4sy4Em1MhJMU8RpWzk6f/ETp7gwejdgnU4oHOLz8d7j5rhBnC1mXlUnsaOA\n\
  3rYimRqDdQJBAONMxKUXtbEOtP34IecM4uC0COXmYiTo9FdUoaoLOxVTBTo2antb\n\
  aEZHvGJJFhvn4ucWtDju15rGCkwhNImDPbMCQQDLNjm/y14RUUaomTw5EtuH/okP\n\
  cgwMJBzSrvMu5MuD1CLo4h155dL2scqvSRCQKEOH5c2njqKeTM+Mhl1WP15pAkEA\n\
  0lYp6rREGDKJ3jMN/Gd6U4GGcmh9Dbbc1Sul5ls6M4N95N6SLtDpb4/WjgUgBnbR\n\
  DBOAdubecYUt5kEcAtyF7QJAYU0OICV21B6t8lsoULHTYDOMm5P7PuMN6w3F+h0Z\n\
  sdyMfD6pBKBw/bBNwApBaypIKK9leLdbebor23reKBi+9w==\n\
  -----END RSA PRIVATE KEY-----";
  strKey = p;
  BIO *pBPK = NULL;
  RSA *pPrivKey;
  pBPK = BIO_new_mem_buf ( ( void* ) strKey.data(), -1 );
  pPrivKey = PEM_read_bio_RSAPrivateKey ( pBPK, NULL, NULL, NULL );
  BIO_free ( pBPK );
  unsigned char* from = ( unsigned char* ) strData.data();
  unsigned char* to = new unsigned char[ RSA_size ( pPrivKey )];
  int res=RSA_private_decrypt(RSA_size ( pPrivKey ), from,to, pPrivKey, 1 );
  </code></pre>
  
  <p>And my openssl.net code is:</p>
  
  <pre><code>string publicKey = @"-----BEGIN PUBLIC KEY-----
  MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDOx0DsbBURESmUVivbiHXqJdaJ
  w8j3v27RC5ArUKIFbzgoqge+ZA9IcWA+WDjW4auzrzhK3sQTHRbkYCkSZsHdkHFu
  1viYDtK0Y1PUzm45xGAjOD2RTbYTMDyUAwDwERoIamPFLa/kQ/4AQnL6F2C6NQPt
  nkCITLsB0+pbQLvLzwIDAQAB
  -----END PUBLIC KEY-----";
  Encoding enc = Encoding.ASCII;
  string text = "1234";
  byte[] msg = enc.GetBytes(text);
  CryptoKey key = CryptoKey.FromPublicKey(publicKey, "");
  RSA rsa = key.GetRSA();
  byte[] res = rsa.PublicEncrypt(msg, RSA.Padding.PKCS1);
  rsa.Dispose();
  strRes = Convert.ToBase64String(res);
  </code></pre>
  
  <p>Base64 encrypted data:</p>
  
  <pre><code>xJTlJeLlm3mufFAdUWFUPfF8KmzFEpIL5K2ibFnnmq4zNECW1conDsWytwGCrqPKOlbmIuhBlg2cUqw6FC6ntyOBSwgRiUht1mBKK+91e7M0h0jb7UTxEmpue+3sVCgjIW6I9GJDo5EA9q5xZA7xzlBh8DGJqOatpLbOt+jJDOw=
  </code></pre>
  
  <p>Original data: 1234</p>
  
  <p>RSA_private_decrypt() returns -1. </p>