In the first place I would like to thank you all for the help I have received reading this website. Thank you very much.
The problem I'm facing is that I have to encrypt strings using objective-c (Iphone) and decrypt them using ColdFusion in a web server. The algorithm I would like to use is AES-128.
At the moment, I managed to encrypt/decrypt in both sites but separately. All I am encrypting in objective-c can be decrypted in objective-c but not in ColdFusion. Basically, the result of the encryption is not the same.
I have code as simple and clean as I could to post it here.
My objective-c output is this one:
Encrypted data: BHmXSHXWOH6McXsttNTgpL5EQmfPCebjVShkZOeHBC8=
My ColdFusion output is this one:
Encrypted data: G+tdEOfQTtVCQGxW3N5uzlu0mGabRKNxuIdAXArQE80=
As you can see they are different :( I think that the problem could be in the Objective-C code because the ColdFusion one is pretty simple. But I'm a bit lost right now, honestly. Any help would be very appreciated.
Here is a copy of the code I'm using:
Objective-C code:
// Starting point of the application.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
NSString *strData = @"This is a plain string.";
NSString *strKey = @"12345678123456781234567812345678";
NSString *strIv = @"1234567812345678";
NSData *data = [NSData dataWithData:[strData dataUsingEncoding:NSUTF8StringEncoding]];
NSData *iv = [NSData dataWithData:[strIv dataUsingEncoding:NSUTF8StringEncoding]];
NSData *key = [NSData dataWithData:[strKey dataUsingEncoding:NSUTF8StringEncoding]];
NSData *encryptedData = [self doCipher:data iv:iv key:key context:kCCEncrypt];
NSLog(@"Encrypted data: %@",[self base64forData:encryptedData]);
}
// Method to encrypt/decrypt data
- (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,
kCCOptionPKCS7Padding,
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;
}
// Method to base64 encode data
- (NSString*)base64forData:(NSData*)theData {
const uint8_t* input = (const uint8_t*)[theData bytes];
NSInteger length = [theData length];
static char table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
NSMutableData* data = [NSMutableData dataWithLength:((length + 2) / 3) * 4];
uint8_t* output = (uint8_t*)data.mutableBytes;
NSInteger i;
for (i=0; i < length; i += 3) {
NSInteger value = 0;
NSInteger j;
for (j = i; j < (i + 3); j++) {
value <<= 8;
if (j < length) {
value |= (0xFF & input[j]);
}
}
NSInteger theIndex = (i / 3) * 4;
output[theIndex + 0] = table[(value >> 18) & 0x3F];
output[theIndex + 1] = table[(value >> 12) & 0x3F];
output[theIndex + 2] = (i + 1) < length ? table[(value >> 6) & 0x3F] : '=';
output[theIndex + 3] = (i + 2) < length ? table[(value >> 0) & 0x3F] : '=';
}
return [[[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding] autorelease];
}
ColdFusion code. I got it from here:
<cfcontent type="text/html; charset=utf-8">
<cfset thePlainData = "This is a plain string." />
<cfset theKey = toBase64("12345678123456781234567812345678") />
<cfset theAlgorithm = "AES/CBC/PKCS5Padding" />
<cfset theEncoding = "base64" />
<cfset theIV = "1234567812345678" />
<cfset encryptedString = encrypt(thePlainData, theKey, theAlgorithm, theEncoding, theIV) />
<cfoutput>Encrypted data: #encryptedString#</cfoutput>