Home > code > AES interoperability between .Net and iPhone

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.

  1. David
    April 8th, 2009 at 05:56 | #1

    Thanks for posting this code!

  2. Ryan
    May 6th, 2009 at 10:52 | #2

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

  3. checcco
    May 28th, 2009 at 16:07 | #3

    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

  4. Mark
    August 6th, 2009 at 14:41 | #4

    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

  5. Saag
    August 28th, 2009 at 11:06 | #5

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

  6. joofus
    November 7th, 2009 at 11:21 | #6

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

  7. Shiv
    November 19th, 2009 at 00:44 | #7

    Excellent work dude.
    Thanks a lot.

  8. locoVJ
    December 16th, 2009 at 10:48 | #8

    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

  9. bbb
    February 23rd, 2010 at 16:03 | #9

    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

  10. Ben
    February 25th, 2010 at 02:30 | #10

    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.

  11. March 3rd, 2010 at 12:47 | #11

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

  12. Rpmobile
    April 8th, 2010 at 12:19 | #12

    Just discovered this post while searching for iphone->.NET crypto interop. Did the base 64 bug get addressed?

    Thanks!

  13. jeet
    April 18th, 2010 at 00:37 | #13

    you guys are chittah (jaguars) – kudos and many thanks!

  14. jeet
    April 18th, 2010 at 04:48 | #14

    by the way guys, I am looking for interoperability between .Net and iPhone for compression mechanism, currently I am trying gZip that available in both platforms, but luck is not my way,
    strings which i compressed from both mechanism gives me different decompressed strings

    many thanks
    Jeet

  15. jeet
    April 19th, 2010 at 08:52 | #15

    I am not taking my words back… you guys are great, but I struck at ground :( I was testing both versions in and out.
    I am encrypting a string in objective-c and also encrypting the same string in C# using AES and am seeing some strange issues. The first part of the result matches up to a certain point but then it is different. Why?

    I am using a source string of “this is going to be test and fingers are crossed” Using a key of “1234567891123456″

    The result from Objective C is 5U6TAlyma3GbR5UYqyk7d7mdTY1Jy9obUTwlOaL0/wn72s7IVZQPi1zydeonLSqP

    The result from C# is
    5U6TAlyma3GbR5UYqyk7d7mdTY1Jy9obUTwlOaL0/wn72s7IVZQPi1zydeonLSqPwifBaSjG51fj6y4S
    j7cS7w==

    you notice that initial portion of strings are same but then it starting differs.

    I’ve not added/modified anything except for those string and key.
    Please assist… I am in do/die situation :(

    thanks
    Jeet

  16. Guna
    June 1st, 2010 at 19:52 | #16

    Thanks for the code, it gives the clear idea for encrypting data on the iPhone and decrypting it using .Net.

  17. Karloz
    June 3rd, 2010 at 18:45 | #17

    YOU ARE THE MAN !!!!

    saved me !

    thanks everyone for fixing the Base64 Problem !!!!

  18. Mitesh Khatri
    July 23rd, 2010 at 03:19 | #18

    Thanks. very nice code. it helps very much…

  19. Ken
    September 2nd, 2010 at 02:04 | #19

    Just for info, you have a error in the md5data function :
    old : NSString* temp = [NSString stringWithFormat:
    @”02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X”,

    New : NSString* temp = [NSString stringWithFormat:
    @”%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X”,

    (miss % on the beginning of the string)

    But the code helpes me some, thank

  1. May 14th, 2009 at 17:55 | #1