iphoneioscryptographyaessecurity-framework

AES encryption in iPhone kccparamerror on device


I am encrypting large files like image files (may have any size - some KBs to large MBs). I am using the following code for encryption, which is working fine on iPhone Simulator 5.1:

+ (NSData *)encryptedDataForData:(NSData *)data key:(NSData *)key error:(NSError **)error {
   size_t outLength;
   NSMutableData * cipherData = [NSMutableData dataWithLength:data.length + kCCBlockSizeAES128];
   CCCryptorStatus result = CCCrypt(kCCEncrypt,
                 kCCAlgorithmAES128, 
                 kCCOptionPKCS7Padding, 
                 key.bytes, 
                 key.length, 
                 NULL,
                 data.bytes, 
                 data.length, 
                 cipherData.mutableBytes,
                 cipherData.length, 
                 &outLength); 

   if (result == kCCSuccess) {
       cipherData.length = outLength;
   } 
   else {

    NSLog(@"errorcode: %d", result);


    return nil;
}

   return cipherData;
}

However, when I am using the same code to encrypt some image on the device - iPhone 5.1.1,

this encryption gives me a kCCParamError (-4300). I have the same values in simulator - but it's working fine there. Any help please?


Solution

  • I was using a wrong key.

    My key consisted of 42 characters.

    Rather, it should have had 24 characters. (using key = 123456789012345678901234 worked).