#region ======== Encryption ========//<summary>//encryption//</summary>//<param name = "Text" ></param>///<returns></returns> public static string Encrypt (string text) { Return Encrypt (Text, "Tony"); }///<summary>//Encryption 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, "Tony"); }///<summary>///Decrypt 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
ASP. NET MD5 Data encryption and decryption