0

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:

For example(first is encryption result and second base 64 decoding result. Decoding first char * directly works pretty well.)

encrypted - ßÁ¨£®Òz>Ô‹n.€Ö∫BÔ–∂ü∏ÕD⁄UÖáµ)ûKufi wÆ&_è”eëõ~gK∂¶$kŸƒ∫ª`ÔfΩ˙˛{∆_MªÔëbP Q¶fl±Ü;!ü•◊s>ħ∆◊⁄≤ò˙ˇCWôVÂzôzíö≤ÙU¶?⁄l[*H?o\ñ>ƒ<‘4mœ“Lr Íhh

decoded string - ßÁ¨£®Òz>Ô‹n.€Ö∫BÔ–∂ü∏ÕD⁄UÖáµ)ûKufi wÆ&_è”eëõ~gK∂¶$kŸƒ∫ª`ÔfΩ˙˛{∆_MªÔëbP Q¶fl±Ü;!ü•◊s>ħ∆◊⁄≤ò˙ˇCWôVÂzôzíö≤ÙU¶?⁄l[*H?o\ñ>ƒ<‘4mœ“Lr Íhh

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;

}

+(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 < 0) return nil;

RSA_free(privKey);

return [NSString stringWithUTF8String: (const char *)ptext];

}

Base 64 encoding-decoding is done with this:

http://www.iphonedevsdk.com/forum/iphone-sdk-development/21689-base-64-string-help.html#post98080

Olga Dalton
  • 829
  • 3
  • 15
  • 25
  • Those strings aren't the same (there is a newline after ¶). – trojanfoe May 04 '12 at 15:01
  • I think your code is very broken. Why are you allocating `key_size` for the encrypted data? Does `strlen(encrypted)` return the right length (I doubt). You never close the `FILE *` and never free the memory you allocate. I'm not surprised it doesn't work. – trojanfoe May 04 '12 at 15:05
  • Having just looked at the reference (http://www.openssl.org/docs/crypto/RSA_public_encrypt.html), it looks like these methods are for encryption/decryption of session keys and not for general-purpose message encryption. You'd normally using DES et al for general-purpose encryption I believe. – trojanfoe May 04 '12 at 15:21
  • @OlgaDalton - Can you please guide me how to do it with out base64 encoding-decoding if possible please share sample code. I am having issues with RSA. my post - http://stackoverflow.com/questions/16711713/rsa-decryption-in-ios?noredirect=1#comment24057964_16711713 – Nitesh Meshram May 24 '13 at 11:33

1 Answers1

-1

Main problem was in base64 encoding+decoding class. Switched to QSutilities and everything works.

Olga Dalton
  • 829
  • 3
  • 15
  • 25