// returns an encrypted string version of an ID suitable for transmission on a URL public static string EncryptID(int unencryptedID) { string encryptedID; byte[] bKey = new byte[8]; byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF }; bKey = System.Text.Encoding.UTF8.GetBytes("!#$a54?3"); // you might want to store the encryption key in an appSetting of your web.config DESCryptoServiceProvider des = new DESCryptoServiceProvider(); Byte[] inputByteArray = System.Text.Encoding.UTF8.GetBytes(unencryptedID.ToString()); MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(bKey, IV), CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); encryptedID = Convert.ToBase64String(ms.ToArray()); return HttpContext.Current.Server.UrlEncode(encryptedID); } public static int DecryptID(string encryptedID) { try { byte[] key = System.Text.Encoding.UTF8.GetBytes("!#$a54?3"); // you might want to store the encryption key in an appSetting of your web.config byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF }; byte[] inputByteArray = new byte[encryptedID.Length]; DESCryptoServiceProvider des = new DESCryptoServiceProvider(); inputByteArray = Convert.FromBase64String(encryptedID); MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(key, IV), CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); System.Text.Encoding encoding = System.Text.Encoding.UTF8; return int.Parse(encoding.GetString(ms.ToArray())); } catch { throw new ApplicationException("An invalid ID was received."); } }