I have to migrate an Android app to iOS that uses Cipher. So here is the Android code:
PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new DESedeEngine()));
...
byte[] result = Hex.encode(output, 0, output.length);
String resultS = new String(Str.toChars(result));
I tried with lot of stuff for objective-c but can't find a way to get the same string as i get on Java. I used the iOS code from here http://dotmac.rationalmind.net/2009/02/aes-interoperability-between-net-and-iphone/ (and a lot more but all do the same).
And then to get the string on iOS use something like:
NSString* resultS = [encryptedData base64Encoding]
but result strings don't match. Maybe the problem is how i handle the encoding for the NSData (seems that the Java version don't use base64, i'm ok?)
Any ideas?
EDIT 1:
Ok, i made some progress (i hope). Checking the java code they use a block size of 8 and DES/CBC with a key of 24 chars. So i change the code from CocoaFu to this:
- (NSData *)doCipher:(NSData *)dataIn
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 + kCCBlockSizeDES];
uint8_t iv[kChosenCipherBlockSize];
memset((void *) iv, 0x0, (size_t) sizeof(iv));
ccStatus = CCCrypt( encryptOrDecrypt,
kCCAlgorithmDES,
kCCOptionPKCS7Padding,
symmetricKey.bytes,
kCCKeySize3DES,
(const void *)iv,
dataIn.bytes,
dataIn.length,
dataOut.mutableBytes,
dataOut.length,
&cryptBytes);
if (ccStatus != kCCSuccess) {
NSLog(@"CCCrypt status: %d", ccStatus);
}
dataOut.length = cryptBytes;
return dataOut;
}
When i try to encode the "test" message in java i get "f69d7c299597c880" but on iOS (using same key of course) i get "< 91864397 > < 41434eaa >" for 3DES and "< ed660859 > < 4bad6f7f >" for DES. Any ideas on what else could i change?