title: RSA decryption with openSSL
body: <p>I have a problem with RSA encrypted and Base 64 encoded text decryption. When i decrypt directly encrypted text(unsigned char *) then everything is okay and I get correct result. But when I do base64 encoding, than openssl fails to decrypt data, although base64 decoded data is exactly same as was encrypted data:</p>
  
  <p>For example(first is encryption result and second base 64 decoding result. Decoding first char * directly works pretty well.)</p>
  
  <p>encrypted - ßÁ¨£®Òz>Ô‹n.€Ö∫BÔ–∂ü∏ÕD⁄UÖáµ)ûKuﬁ wÆ&amp;_è”eëõ~gK∂¶$kŸƒ∫ª`ÔfΩ˙˛{∆_MªÔëbP QÂ¶ﬂ±Ü;!ü•◊s>Ä§∆◊⁄≤ò˙ˇCWôVÂzôzíö≤ÙU¶?⁄l[*H?o\ñ>ƒ&lt;‘4mœ“Lr
  Íhh</p>
  
  <p>decoded string - ßÁ¨£®Òz>Ô‹n.€Ö∫BÔ–∂ü∏ÕD⁄UÖáµ)ûKuﬁ wÆ&amp;_è”eëõ~gK∂¶$kŸƒ∫ª`ÔfΩ˙˛{∆_MªÔëbP QÂ¶ﬂ±Ü;!ü•◊s>Ä§∆◊⁄≤ò˙ˇCWôVÂzôzíö≤ÙU¶?⁄l[*H?o\ñ>ƒ&lt;‘4mœ“Lr
  Íhh</p>
  
  <p>Code:</p>
  
  <pre><code>+(NSString *) rsaEncryptedStringFromText: (NSString *) text
  {
  const char *message = [text UTF8String];
  
  NSLog(@"message - %s", message);
  
  int bufSize;
  
  NSString *keyFilePath = [[NSBundle mainBundle] pathForResource:@"publicKey" ofType:@"pem"];
  
  FILE *keyfile = fopen([keyFilePath UTF8String], "r");
  
  RSA *rsa = PEM_read_RSA_PUBKEY(keyfile, NULL, NULL, NULL);
  
  if (rsa == NULL)
  {
      return nil;
  }
  
  int key_size = RSA_size(rsa);
  
  unsigned char *encrypted = (unsigned char *) malloc(key_size);
  
  bufSize = RSA_public_encrypt(strlen(message), (unsigned char *) message, encrypted, rsa, RSA_PKCS1_PADDING);
  
  if (bufSize == -1)
  {
      RSA_free(rsa);
      return nil;
  }
  
  NSLog(@"encrypted - %s", encrypted);
  
  NSData *encryptedData = [NSData dataWithBytes:encrypted length:strlen((const char *)encrypted)];
  
  NSString *base64 = [encryptedData base64Encoding];
  
  RSA_free(rsa);
  
  return base64;
  </code></pre>
  
  <p>}</p>
  
  <pre><code>+(NSString *) rsaDecryptToStringFromText: (NSString *) text
  {
  //NSLog(@"text - %@", text);
  
  NSData *decodedData = [NSData dataWithBase64EncodedString: text];
  
  unsigned char* message = (unsigned char*) [decodedData bytes];
  
  NSLog(@"decoded string - %s", message);
  
  RSA *privKey = NULL;
  FILE *priv_key_file;
  unsigned char *ptext;
  
  NSString *keyFilePath = [[NSBundle mainBundle] pathForResource:@"privateKeyPair" ofType:@"pem"];
  
  priv_key_file = fopen([keyFilePath UTF8String], "rb");
  
  ERR_print_errors_fp(priv_key_file);
  
  privKey = PEM_read_RSAPrivateKey(priv_key_file, NULL, NULL, NULL);
  
  int key_size = RSA_size(privKey);
  ptext = malloc(key_size);
  
  int outlen = RSA_private_decrypt(key_size, (const unsigned char*)message, ptext, privKey, RSA_PKCS1_PADDING);
  
  if(outlen &lt; 0) return nil;
  
  RSA_free(privKey);
  
  return [NSString stringWithUTF8String: (const char *)ptext];
  </code></pre>
  
  <p>}</p>
  
  <p>Base 64 encoding-decoding is done with this: </p>
  
  <p><a href="http://www.iphonedevsdk.com/forum/iphone-sdk-development/21689-base-64-string-help.html#post98080" rel="nofollow">http://www.iphonedevsdk.com/forum/iphone-sdk-development/21689-base-64-string-help.html#post98080</a></p>