How can I make my AES encryption identical between Java and Objective-C (iPhone)? How can I make my AES encryption identical between Java and Objective-C (iPhone)? objective-c objective-c

How can I make my AES encryption identical between Java and Objective-C (iPhone)?


Since the CCCrypt takes an IV, does it not use a chaining block cipher method (such as CBC)? This would be consistant with what you see: the first block is identical, but in the second block the Java version applies the original key to encrypt, but the OSX version seems to use something else.

EDIT:

From here I saw an example. Seems like you need to pass the kCCOptionECBMode to CCCrypt:

ccStatus = CCCrypt(encryptOrDecrypt,        kCCAlgorithm3DES,        kCCOptionECBMode, <-- this could help        vkey, //"123456789012345678901234", //key        kCCKeySize3DES,        nil, //"init Vec", //iv,        vplainText, //"Your Name", //plainText,        plainTextBufferSize,        (void *)bufferPtr,        bufferPtrSize,        &movedBytes);

EDIT 2:

I played around with some command line to see which one was right. I thought I could contribute it:

$ echo "Now then and what is this nonsense all about. Do you know?" | openssl enc -aes-128-ecb -K $(echo 1234567890123456 | xxd -p) -iv 0 | xxd 0000000: 7a68 ea36 8288 c73d f7c4 5d8d 2243 2577  zh.6...=..]."C%w0000010: e66b 32f9 772b 6679 d7c0 cb69 037b 8740  .k2.w+fy...i.{.@0000020: 883f 8211 7482 29f4 7239 84be b50b 5aea  .?..t.).r9....Z.0000030: eaa7 519b 65e8 fa26 a1bb de52 083b 478f  ..Q.e..&...R.;G.


I spent a few weeks decrypting a base64 encoded, AES256 encrypted string. Encryption was done by CCCrypt (Objective-C)on an iPad. The decryption was to be done in Java (using Bouncy Castle).

I finally succeeded and learnt quite a lot in the process. The encryption code was exactly the same as above (I guess it's taken from the Objective-C sample in the iPhone developer documentation).

What CCCrypt() documentation does NOT mention is that it uses CBC mode by default (if you don't specify an option like kCCOptionECBMode). It does mention that the IV, if not specified, defaults to all zeros (so IV will be a byte array of 0x00, 16 members in length).

Using these two pieces of information, you can create a functionally identical encryption module using CBC (and avoid using ECB which is less secure) on both Java and OSx/iphone/ipad(CCCrypt).

The Cipher init function will take the IV byte array as a third argument:

cipher.init(Cipher.ENCRYPT_MODE, keySpec, IV).


For anyone else who needs this, disown was absolutely spot on... the revised call to create the crypt in objective-c is as follows (note you need the ECB mode AND the padding)...

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