
| home | AJAX (6) || C#.NET (5) || Coldfusion Development (16) || DHTML (14) || Flash Development (19) || jQuery (4) || MSSQL (2) || UNIX (10) |
| 4.17.08 | Simple AES (Rijndael) C# Encrypt & Decrypt functions |
I simply re-purposed Bobby Derosa’s Triple DES (3DES) Encrypt() and Decrypt() functions to provide a simple and straight-forward way to use AES symmetrical encryption safe for use on such things as UTF-8 and/or HTTP GET string compatibility. I’m using this in ASP.NET AJAX-enabled applications.
I’m only using a 256bit (32byte) key for these example functions.
Encrypt()
RijndaelManaged rDel = new RijndaelManaged();
rDel.Key = keyArray;
rDel.Mode = CipherMode.ECB;
rDel.Padding = PaddingMode.PKCS7;
ICryptoTransform cTransform = rDel.CreateEncryptor();
byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
return Convert.ToBase64String(resultArray, 0, resultArray.Length);
}
Download this code: rijndael.encrypt.cs
Encrypt()
RijndaelManaged rDel = new RijndaelManaged();
rDel.Key = keyArray;
rDel.Mode = CipherMode.ECB;
rDel.Padding = PaddingMode.PKCS7;
ICryptoTransform cTransform = rDel.CreateDecryptor();
byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
return UTF8Encoding.UTF8.GetString(resultArray);
}
Download this code: rijndael.decrypt.cs
No comments