Comments on: AES interoperability between .Net and iPhone http://automagical.rationalmind.net/2009/02/12/aes-interoperability-between-net-and-iphone/ n. 1: automatic, but with an element of magic. 2: too complex to understand and/or explain Fri, 04 Mar 2016 06:52:31 +0000 hourly 1 By: Kumar http://automagical.rationalmind.net/2009/02/12/aes-interoperability-between-net-and-iphone/#comment-22409 Fri, 04 Mar 2016 06:52:31 +0000 http://dotmac.rationalmind.net/?p=96#comment-22409 Hi All,

we have RijndaelManaged algorithm written in .net. same thing I wanted to convert in objective c . Please help me on this.

Please find the c charp code.

private string EncryptKey(string key)
{
byte[] objIV = null;
byte[] objSecretKey = null;
string strSecretKey = GetConfigValue(MyPatients.Utils.Constants.SECRET_KEY);
string strIVKey = GetConfigValue(MyPatients.Utils.Constants.IV_KEY);
string strEncrypted = null;

if ((strSecretKey.Length > 0))
{
objSecretKey = HexToByte(strSecretKey);
}

if ((strIVKey.Length > 0))
{
objIV = HexToByte(strIVKey);
}

byte[] aibUrlArray = System.Text.Encoding.UTF8.GetBytes(key);
byte[] EncryptedStringArray = null;

// instantiate a Rijndael Object
RijndaelManaged objRijndael = new RijndaelManaged();

// initializing the Rijndael Encryptor
objRijndael.Mode = CipherMode.CBC;
objRijndael.Padding = PaddingMode.PKCS7;
objRijndael.KeySize = 256;
objRijndael.BlockSize = 128;

// Creating an encryptor
ICryptoTransform objEncryptor = objRijndael.CreateEncryptor(objSecretKey, objIV);

// Creating a memory string
System.IO.MemoryStream objMemoryStream = new System.IO.MemoryStream();
CryptoStream objCryptoStream = new CryptoStream(objMemoryStream, objEncryptor, CryptoStreamMode.Write);

objCryptoStream.Write(aibUrlArray, 0, aibUrlArray.Length);
objCryptoStream.FlushFinalBlock();

// Encrypted data to the array
EncryptedStringArray = objMemoryStream.ToArray();

// Create the encrypted string:
strEncrypted = ByteToHex(EncryptedStringArray);

return strEncrypted;
}

Thanks in advance.

I tried to download the zip but it is giving me error.

]]>
By: Arun http://automagical.rationalmind.net/2009/02/12/aes-interoperability-between-net-and-iphone/#comment-22106 Thu, 05 Nov 2015 13:58:57 +0000 http://dotmac.rationalmind.net/?p=96#comment-22106 i want same for android and cordova

]]>
By: Ricardo Órfão http://automagical.rationalmind.net/2009/02/12/aes-interoperability-between-net-and-iphone/#comment-21062 Mon, 03 Aug 2015 19:49:46 +0000 http://dotmac.rationalmind.net/?p=96#comment-21062 The link is not working. Can you provide a new one? Thank you.

]]>
By: Mr.Abc http://automagical.rationalmind.net/2009/02/12/aes-interoperability-between-net-and-iphone/#comment-9066 Sun, 22 Jun 2014 19:35:08 +0000 http://dotmac.rationalmind.net/?p=96#comment-9066 Thanks a lot for posting this code. I have one doubt , how I can get NSString instead of NSData when a text is encrypted???
Or how I can send these encrypted NSData to .Net method?
Can anyone Help me????

]]>
By: Murat http://automagical.rationalmind.net/2009/02/12/aes-interoperability-between-net-and-iphone/#comment-6250 Wed, 12 Mar 2014 16:43:15 +0000 http://dotmac.rationalmind.net/?p=96#comment-6250 Hi,

Have you solve the problem? I have the same issue. Server-side encrypted data is not get decrypt in iOS.

]]>
By: Lyubomir http://automagical.rationalmind.net/2009/02/12/aes-interoperability-between-net-and-iphone/#comment-6236 Tue, 04 Mar 2014 15:57:14 +0000 http://dotmac.rationalmind.net/?p=96#comment-6236 The method for encryption and decryption would be much better if you have included a salt in the plaintext so that the cypertext is always random. This is not so hard modification, but necessary to prevent men in the middle attacks.
Otherwise useful code to start with!

]]>
By: Ian Phillipchuk http://automagical.rationalmind.net/2009/02/12/aes-interoperability-between-net-and-iphone/#comment-6202 Thu, 13 Feb 2014 18:40:41 +0000 http://dotmac.rationalmind.net/?p=96#comment-6202 Hey Joe, there seems to be a problem in your code listing for the StringEncryption.m class. This section here:

int x = 9;
for (int i = passcode.length; i < kChosenCipherKeySize/*16*/; i++)
{
//passcode = [passcode stringByAppendingString:@"9"];
passcode = [passcode stringByAppendingFormat:@"%d", x];
x–;
if (x 0)
[reversedStr appendString:
[NSString stringWithFormat:@"%C", [originalString characterAtIndex:–len]]];

return reversedStr;
}

Doesn't seem to make sense. It looks like it's missing a couple of brackets and some variables. Could you post the complete function? That would help me a whole tonne.

]]>
By: Marcel http://automagical.rationalmind.net/2009/02/12/aes-interoperability-between-net-and-iphone/#comment-6200 Thu, 13 Feb 2014 15:37:56 +0000 http://dotmac.rationalmind.net/?p=96#comment-6200 Thx a lot. Regards.

]]>
By: Joe Ruggiero http://automagical.rationalmind.net/2009/02/12/aes-interoperability-between-net-and-iphone/#comment-6197 Wed, 12 Feb 2014 15:38:40 +0000 http://dotmac.rationalmind.net/?p=96#comment-6197 I hope this helps.

]]>
By: Joe Ruggiero http://automagical.rationalmind.net/2009/02/12/aes-interoperability-between-net-and-iphone/#comment-6196 Wed, 12 Feb 2014 15:38:07 +0000 http://dotmac.rationalmind.net/?p=96#comment-6196 c# Code

I use the following two(2) global variables:

private static int eKeySize = 32; //16
private static int cipherKeySize = eKeySize * 8;

The following routines encrypt/decrypt based on a 32bit key size:

private string EncryptString(string plainSourceStringToEncrypt, string passPhrase)
{
//Set up the encryption objects
using (AesCryptoServiceProvider acsp = GetProvider(Encoding.Default.GetBytes(passPhrase)))
{
byte[] sourceBytes = Encoding.ASCII.GetBytes(plainSourceStringToEncrypt);
ICryptoTransform ictE = acsp.CreateEncryptor();

//Set up stream to contain the encryption
MemoryStream msS = new MemoryStream();

//Perform the encrpytion, storing output into the stream
CryptoStream csS = new CryptoStream(msS, ictE, CryptoStreamMode.Write);
csS.Write(sourceBytes, 0, sourceBytes.Length);
csS.FlushFinalBlock();

//sourceBytes are now encrypted as an array of secure bytes
byte[] encryptedBytes = msS.ToArray(); //.ToArray() is important, don’t mess with the buffer

//return the encrypted bytes as a BASE64 encoded string
return Convert.ToBase64String(encryptedBytes);
}
}

///
/// Decrypts a BASE64 encoded string of encrypted data, returns a plain string
///
/// an Aes encrypted AND base64 encoded string
/// The passphrase.
/// returns a plain string
private string DecryptString(string base64StringToDecrypt, string passphrase)
{
try
{
//Set up the encryption objectsr
using (AesCryptoServiceProvider acsp = GetProvider(Encoding.Default.GetBytes(passphrase)))
{
byte[] RawBytes = Convert.FromBase64String(base64StringToDecrypt);
ICryptoTransform ictD = acsp.CreateDecryptor();

//RawBytes now contains original byte array, still in Encrypted state

//Decrypt into stream
MemoryStream msD = new MemoryStream(RawBytes, 0, RawBytes.Length);
CryptoStream csD = new CryptoStream(msD, ictD, CryptoStreamMode.Read);
//csD now contains original byte array, fully decrypted

//return the content of msD as a regular string
return (new StreamReader(csD)).ReadToEnd();
}
}
catch (Exception e)
{
throw new Exception(“Unable to Decrypt Data: [” + base64StringToDecrypt + “] –> ” + e.Message);
}
}

private AesCryptoServiceProvider GetProvider(byte[] key)
{
AesCryptoServiceProvider result = new AesCryptoServiceProvider();
result.BlockSize = 128;
result.KeySize = cipherKeySize; // 256;
result.Mode = CipherMode.CBC;
result.Padding = PaddingMode.PKCS7;

result.GenerateIV();
result.IV = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

byte[] RealKey = GetKey(key, result);
result.Key = RealKey;
// result.IV = RealKey;
return result;
}

private byte[] GetKey(byte[] suggestedKey, SymmetricAlgorithm p)
{
byte[] kRaw = suggestedKey;
List kList = new List();

for (int i = 0; i < cipherKeySize /* p.LegalKeySizes[0].MaxSize*//*MinSize*/; i += 8)
{
kList.Add(kRaw[(i / 8) % kRaw.Length]);
}
byte[] k = kList.ToArray();
return k;
}
}
}

]]>