AES interoperability between .Net and iPhone

One of my projects requires encrypting data on the iPhone and decrypting it using .Net. This is easy to do with the Common Crypto library in the iPhone SDK and the AesCryptoServiceProvider class in .Net, but the encryption parameters have to be the same for it to work.

I couldn’t figure it out, but the geniuses at StackOverflow did, so I am posting my results here. The zip file includes a basic iPhone app and a .Net console project with helpful classes to do the encryption/decryption and base64 conversion. I didn’t write most of the code – thanks to Blue Beetle for the .Net code and Greg Haygood for the Objective C.

Download zip.

12 Comments

DavidApril 8th, 2009 at 5:56 am

Thanks for posting this code!

RyanMay 6th, 2009 at 10:52 am

This is great, thanks for the example, it works perfectly.

[...] and searched the web. Then I found the following blog by David Veksler — http://dotmac.rationalmind.net/2009/02/aes-interoperability-between-net-and-iphone/ , who posted the sample code for iPhone and .net. It really helped me a lot and saved ONE DAY for [...]

checccoMay 28th, 2009 at 4:07 pm

Hi, this is just what i was looking for… but i need decryption in PHP…:(

I’m thinking to buy this: http://www.phpaes.com on their site there’s a demo utility to encrypt or decrypt strings…but i cant get the same result as the iphone program you provided… would you please try, too? maybe im messing with mode and iv..thanks so much

MarkAugust 6th, 2009 at 2:41 pm

hi, a quick question
i get encrypted data to export as NSString say “u+8tQsPIHhr1Ll5TKBdbdSZmLEX/cD/xYY34kLPIPFc=”

which is good, if I decrypt encrypted data it works but when I take this export string (above) and try to created NSDATA object
NSData* encData = [NSData dataWithBase64EncodedString:"u+8tQsPIHhr1Ll5TKBdbdSZmLEX/cD/xYY34kLPIPFc="];

then i cannot get encoded string decoded.

Here is full sample:

NSString * _secret = @”My Encryption Key”;
NSString * _key = @”1234123412341234″;

StringEncryption *crypto = [[[StringEncryption alloc] init] autorelease];
NSData *_secretData = [_secret dataUsingEncoding:NSUTF8StringEncoding];
CCOptions padding = kCCOptionPKCS7Padding;
NSData *encryptedData = [crypto encrypt:_secretData key:[_key dataUsingEncoding:NSUTF8StringEncoding] padding:&padding];

NSString* encDataToExport = [encryptedData base64EncodingWithLineLength:0];

// do reverse -> from encrypted data to unencrypted

NSData* encData = [NSData dataWithBase64EncodedString:encDataToExport]; // gives me 427 bytes somehow!!!!!!!!!

NSData* decryptedData = [crypto decrypt:encData key:[_key dataUsingEncoding:NSUTF8StringEncoding] padding:&padding]; // returns nil

NSString* str = [[NSString alloc] initWithData:decryptedData encoding:NSUTF8StringEncoding];
NSLog(@”str: %@”,str);

I think i am not converting encoded string correctly to base64 data.
What am I missing?

Thank you
Mark

SaagAugust 28th, 2009 at 11:06 am

thank you dude!
You got me out of my illiteracy…

joofusNovember 7th, 2009 at 11:21 am

@ mark:
had the same problem, there’s a bug in the base 64 file.

ShivNovember 19th, 2009 at 12:44 am

Excellent work dude.
Thanks a lot.

locoVJDecember 16th, 2009 at 10:48 am

Thanks for the Code.
When I use this code on iPhone for hello/hello the encoded string is not QA+Ul+r6Zmr7yHipMcHSbQ==
Can you please verify there is ssome problem with the Base64
Thanks,
locoVJ

bbbFebruary 23rd, 2010 at 4:03 pm

Because this was so helpful…

There seems to be a bug in the base64 file. The line with inbuf[3] outbuf[4] should be inbuf[4] outbuf[3].

And you need to use the base64 stuff if you are working off the .NET example.. So something like

NSData * encdata = [NSData dataWithBase64DecodedString:the_encrypted64strfromweb];

NSData* decData = [crypto decrypt:encdata ….

————–

Oh, and just use a 16 character key right off the bat….

Thanks

BenFebruary 25th, 2010 at 2:30 am

Thanks for the code. It is very useful for me.
And I would also like to thanks bbb. Otherwise, I cannot still find a way to decrypt the base32 string.

Denny FerrassoliMarch 3rd, 2010 at 12:47 pm

Thank you so much for finding this! I was trying to figure this out over a year ago and eventually had to move on.

Leave a comment

Your comment