2

I am encrypting a value in PHP and another programmer is decrypting it in an iPhone app. Neither of us could be called knowledgeable in encryption. Everything looks correct from the information I have found on this site and elsewhere. The iPhone guy is saying he gets a success return code from the decryption process, but a null value for the data.

Here is the PHP side:

$key = getproperty("cryptkey","testkeytobechanged");
srand();
$iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC), MCRYPT_RAND);
if (strlen($iv_base64 = rtrim(base64_encode($iv), '=')) != 22) return false;
$encrypted = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_128, md5($key), $iv_base64."::::".$authority->get_mobiuser()->get_message(), MCRYPT_MODE_CBC, $iv));
$rv = $iv_base64.$encrypted;

This is what is on the iPhone side:

   //We have compared the hex values after base64 decoding enough that I am satisfied there is not an issue at that point.

   NSString *b = [[dict objectForKey:@"data"]objectForKey:@"nlbal"];
   NSData *iv = [NSData dataFromBase64String:[[b substringToIndex:22]stringByAppendingString:@"=="]];
   NSLog(@"IV %@", [iv description]);
   NSString *e = [b substringFromIndex:22];
   NSData *encrypted = [NSData dataFromBase64String:e];
   NSLog(@"ENC %@", encrypted);
   NSUInteger dataLength = [encrypted length];
   uint8_t unencryptedData[dataLength + kCCKeySizeAES128];
   size_t unencryptedLength;
   //NSData *un = [encrypted AES128DecryptWithKey:[SessionManager md5:@"testkeytobechanged"] iv:iv];



   //From my research, I understand this to be the correct form to match the encryption I use on the PHP side
   CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt, 
                                          kCCAlgorithmAES128, 0, 
                                          [[SessionManager md5:@"testkeytobechanged"]UTF8String], 
                                          kCCKeySizeAES128, [iv bytes], 
                                          [encrypted bytes], dataLength, 
                                          unencryptedData, dataLength, &unencryptedLength);

   NSString *output = [[NSString alloc] initWithBytes:unencryptedData length:unencryptedLength encoding:NSUTF8StringEncoding];
   // output = [[NSString alloc]initWithData:un encoding:NSUTF8StringEncoding];
   //NSLog(@"un %@", [un description]);
   NSLog(@"OUTPUT %i %@", cryptStatus, output);

As I said above, this code, apparently, produces a success code but has a null value.

1
  • I got my hands on the iPhone side of this code and played around with it. The above code produces some bytes of data. But, NSString is not able to convert into text (returns null), and the byte value returned is not the same thing twice. I suspect that there is something wrong with how the key,iv,encoded data are being converted from base64 encoding to bytes but I don't know exactly what is wrong. The IV is generated by mcrypt and converted to base64 then deconverted into NSData and sent to CCCrypt. Does that seem right? They key looks like it might be used differently on either side?
    – Andrew
    Aug 22, 2012 at 18:11

0

Browse other questions tagged or ask your own question.