CommonCrypto works differently in IPA archive than in debugger

| | August 7, 2015

I’ve been pulling my hair out over this one. I’m using CommonCrypto in my iOS application to encrypt data which I then send to a Windows server which decrypts it. This works perfectly in the Xcode (latest version) development environment both on my iPhone 5 (iOS 6), iPad 3 (iOS 6) and the simulator (on Mac OS X 10.8.2).

The very simple code I’m using to encrypt is this:

    CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding,
                                      keyPtr, kCCKeySizeAES128, // oorspronkelijk 256
                                      keyPtr /* initialization vector (optional) */,
                                      [self bytes], dataLength, /* input */
                                      buffer, bufferSize, /* output */
                                      &numBytesEncrypted);

Again, this works great when I’m debugging from Xcode.

But when I build the IPA (archive file, ie. what Apple reviews for the App Store) and send encrypted data to the Windows server, the server reports that the “padding is invalid”. There are no code differences between the two!

I suspect that something is different between CommonCrypto in these two build modes, but I can’t figure out what that would be. And I tried getting the CommonCrypto library from Apple’s open source site and building it, with the intent of compiling it into my code, but I have not been able to build it successfully.

Has anyone else run into this problem? Is there some compiler option I’m missing here?

* EDIT *

I found the flag that is causing the problem: If I set “Optimization Level” to “Smallest/Fastest”, “Fastest”, “Faster”, or “Fast” – it fails. But if I set it to “None”, it works. So something in the optimization is causing the encryption to break!

Leave a Reply