ronIDX
ronIDX

Reputation: 768

AES encryption/decryption iOs and .Net

I used CocoaSecurity and RNCryptor to encrypt NSString on iOs app, and on the server side (.NET) tried to decrypt it using one of the many function found on the web, but no luck. Also AES decryption online tools, fail to decrypt.

Can somebody provides a working example of NSString encryption on iOS and decryption of it in .NET (VB or C#) using AES256?

Upvotes: 3

Views: 1152

Answers (2)

ronIDX
ronIDX

Reputation: 768

Thanks zaph. Your answer helps me a lot.

As suggested, using RNCryptor on iOS and RNCryptor-cs in .Net I am able to encrypt data from iOS and then decrypt them on .Net.

Here a small example, how I achieve that:

On iOS side:

NSData* data = [@"mySecretMessage" dataUsingEncoding:NSUTF8StringEncoding];
NSError* error;
NSData* encrypted = [RNEncryptor encryptData:data
                                withSettings:kRNCryptorAES256Settings
                                    password:@"mySecretPassword"
                                       error:&error];
NSString* encryptedDataAsString = [encrypted base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithCarriageReturn];
//encryptedDataAsString = AwFnpL/jHjAYNkNnfBRUwl0pMwyHnM8uo2dojFk+rC7x9LnaFz+T1KaTjxSXoxF6Q4mzT+yl5RLuKZZuaiDlY5dXBw6TEyEXNJ8CxG9ZDZB3nQ==

On .Net side (using Visual Basic):

Dim decryptor As RNCryptor.Decryptor = New RNCryptor.Decryptor

MessageBox.Show(decryptor.Decrypt("AwFnpL/jHjAYNkNnfBRUwl0pMwyHnM8uo2dojFk+rC7x9LnaFz+T1KaTjxSXoxF6Q4mzT+yl5RLuKZZuaiDlY5dXBw6TEyEXNJ8CxG9ZDZB3nQ==", "mySecretPassword"))
//MessageBox output = mySecretMessage

Upvotes: 2

zaph
zaph

Reputation: 112857

If you are using RNCryptor on iOS for .net use RNCryptor-cs. RNCryptor is more than just encrypt/decrypt, it is a complete secure protocol, see RNCryptor-Spec for details on the RNCryptor protocol. RNEncryptor is being actively maintained.

CocoaSecurity it is not terrible but I would not use it, it uses SHA to derive the encryption key from from a password and that is not good, the current practice is to use a key derivation function such as PBKDF2 which is much more secure. You will have to match the protocol on .NET and that is not detailed, you will have to read the code to figure it out. It has not been updated in a year or more.

Upvotes: 1

Related Questions