0

I have to generate encrypted key on my php server and send it to the ipad app to decrypt it.

What I did in the php server side :

    $iv = mcrypt_create_iv(32);
    $privatEencryptKey = "1111";
    $data = "2222";
    $encryptedData = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $privateEncryptKey, base64_encode($data), MCRYPT_MODE_CBC, $iv);
    $decryptedData = base64_decode(mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $privateEncryptkey, $encryptedData, MCRYPT_MODE_CBC, $iv));

    echo base64_encode($encryptedData); //output = WT7LorzZ1EQo2BeWxawW3Q==
    echo $decryptedData; // output = 2222
    echo base64_encode($iv); // output = fZTj4BxWSdCYQW/scUHvx9QoiTNXmxNrGWb/n7eFkR4= 

and in the xcode I import the sercurity.framwork and I added 3rd party for base64 (encoding & decoding) and I use the (CommonCryptor.h) also and here is my code :

+ (NSData *)doCipher:(NSData *)dataIn
              iv:(NSData *)iv
             key:(NSData *)symmetricKey
         context:(CCOperation)encryptOrDecrypt{
         CCCryptorStatus ccStatus   = kCCSuccess;
         size_t          cryptBytes = 0;    // Number of bytes moved to buffer.
         NSMutableData  *dataOut    = [NSMutableData dataWithLength:dataIn.length + kCCBlockSizeAES128];

         ccStatus = CCCrypt( encryptOrDecrypt,
                   kCCAlgorithmAES128,
                   0,
                   symmetricKey.bytes,
                   kCCKeySizeAES128,
                   iv.bytes,
                   dataIn.bytes,
                   dataIn.length,
                   dataOut.mutableBytes,
                   dataOut.length,
                   &cryptBytes);

        if (ccStatus != kCCSuccess) {
              NSLog(@"CCCrypt status: %d", ccStatus);
        }

      dataOut.length = cryptBytes;
      return dataOut;
  }

  + (void) testCipher{
        NSData *dataIn = [[@"WT7LorzZ1EQo2BeWxawW3Q==" base64DecodedString] dataUsingEncoding:NSUTF8StringEncoding];
        NSData *key = [@"1111" dataUsingEncoding:NSUTF8StringEncoding];
        NSData *iv = [[@"fZTj4BxWSdCYQW/scUHvx9QoiTNXmxNrGWb/n7eFkR4=" base64DecodedString] dataUsingEncoding:NSUTF8StringEncoding];

        NSData *dataOut = [Utils doCipher:dataIn iv:iv key:key context:kCCDecrypt];
        NSString* strOut = [[[NSString alloc] initWithData:dataOut
                                          encoding:NSUTF8StringEncoding] base64DecodedString];
        NSLog(@"%@", strOut);
  }

I got the nil for strOut..... :(

Any help please......

4
  • Provide in this order: encrypted Base64 data, decrypted Base64 data, decrypted string. Commented Jan 16, 2013 at 0:44
  • The supplied key is 32 bits but AES128 needs 128 bits. The iv is 256 bits but only 128 bits are needed. Commented Jan 16, 2013 at 0:49
  • Thanks Zaph, sorry but I'm a novice in cryptography field .... how to generate the 128 bit key and IV (in PHP to use for encryption) and pass the generated keys to reuse them in objective c for decryption ??? .... What I did is echo encoded64 key and iv to my test page.php and copy paste the values to my objective c class and decode64 these values and pass them to the CCCrypt function after converting them to NSData object... am I right? Commented Jan 17, 2013 at 19:35
  • I used RNCryptor iOS library, and It works for me Commented Jan 25, 2013 at 14:09

1 Answer 1

1
  1. You should use 16-byte Key and IV for AES-128. Mcrypt_encrypt otherwise pads this with zero.
  2. Most likely you should manually add PKCS#5 padding to the input since the mcrypt_encrypt pads data with zero, which is not common practice.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.