Using System; Using System.Security.Cryptography; Using System.Text; <summary> DES encryption/Decryption class. Litianping </summary> public class Desencrypt { Public Desencrypt () { } #region ======== Encryption ======== <summary> Encryption </summary> <param name= "Text" ></param> <returns></returns> public static string Encrypt (String Text) { Return Encrypt (Text, "Maticsoft"); } <summary> Encrypt data </summary> <param name= "Text" ></param> <param name= "SKey" ></param> <returns></returns> public static string Encrypt (String Text, String SKey) { DESCryptoServiceProvider des = new DESCryptoServiceProvider (); Byte[] Inputbytearray; Inputbytearray = Encoding.Default.GetBytes (Text); Des. Key = ASCIIEncoding.ASCII.GetBytes (System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile ( SKey, "MD5"). Substring (0, 8)); DES.IV = ASCIIEncoding.ASCII.GetBytes (System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile (SKey, "MD5"). Substring (0, 8)); System.IO.MemoryStream ms = new System.IO.MemoryStream (); CryptoStream cs = new CryptoStream (MS, Des. CreateEncryptor (), cryptostreammode.write); Cs. Write (Inputbytearray, 0, inputbytearray.length); Cs. FlushFinalBlock (); StringBuilder ret = new StringBuilder (); foreach (Byte b in Ms.) ToArray ()) { Ret. AppendFormat ("{0:x2}", b); } return ret. ToString (); } #endregion #region ======== Decryption ======== <summary> Decrypt </summary> <param name= "Text" ></param> <returns></returns> public static string Decrypt (String Text) { Return Decrypt (Text, "Maticsoft"); } <summary> Decrypting data </summary> <param name= "Text" ></param> <param name= "SKey" ></param> <returns></returns> public static string Decrypt (String Text, String SKey) { DESCryptoServiceProvider des = new DESCryptoServiceProvider (); int Len; len = TEXT.LENGTH/2; byte[] Inputbytearray = new Byte[len]; int x, I; for (x = 0; x < len X + +) { i = Convert.ToInt32 (text.substring (x * 2, 2), 16); INPUTBYTEARRAY[X] = (byte) i; } Des. Key = ASCIIEncoding.ASCII.GetBytes (System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile ( SKey, "MD5"). Substring (0, 8)); DES.IV = ASCIIEncoding.ASCII.GetBytes (System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile (SKey, "MD5"). Substring (0, 8)); System.IO.MemoryStream ms = new System.IO.MemoryStream (); CryptoStream cs = new CryptoStream (MS, Des. CreateDecryptor (), cryptostreammode.write); Cs. Write (Inputbytearray, 0, inputbytearray.length); Cs. FlushFinalBlock (); Return Encoding.Default.GetString (Ms. ToArray ()); } #endregion } |