CURRENT PROJECTS
loading
CATEGORIES AND POSTS
loading
overset
DEVELOPMENT LOG FOR JIM PALMER
Posted 04/17/2008 in C#.NET


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()
public static string Encrypt(string toEncrypt) {
	byte[] keyArray = UTF8Encoding.UTF8.GetBytes("12345678901234567890123456789012"); // 256-AES key
	byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);
	RijndaelManaged rDel = new RijndaelManaged();
	rDel.Key = keyArray;
	//rDel.Mode = CipherMode.ECB; // http://msdn.microsoft.com/en-us/library/system.security.cryptography.ciphermode.aspx
	rDel.Padding = PaddingMode.PKCS7; // better lang support
	ICryptoTransform cTransform = rDel.CreateEncryptor();
	byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
	return Convert.ToBase64String(resultArray, 0, resultArray.Length);
}

Encrypt()
public static string Decrypt(string toDecrypt) {
	byte[] keyArray = UTF8Encoding.UTF8.GetBytes("12345678901234567890123456789012"); // AES-256 key
	byte[] toEncryptArray = Convert.FromBase64String(toDecrypt);
	RijndaelManaged rDel = new RijndaelManaged();
	rDel.Key = keyArray;
	//rDel.Mode = CipherMode.ECB; // http://msdn.microsoft.com/en-us/library/system.security.cryptography.ciphermode.aspx
	rDel.Padding = PaddingMode.PKCS7; // better lang support
	ICryptoTransform cTransform = rDel.CreateDecryptor();
	byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
	return UTF8Encoding.UTF8.GetString(resultArray);
}
comments
loading
new comment
NAME
EMAIL ME ON UPDATES
EMAIL (hidden)
URL
MESSAGE TAGS ALLOWED: <code> <a> <pre class="code [tab4|tabX|inline|bash]"> <br>
PREVIEW COMMENT
TURING TEST
gravatar