1 usingSystem;2 usingSystem.Data;3 usingSystem.Configuration;4 usingsystem.web;5 usingSystem.Web.Security;6 usingSystem.Web.UI;7 usingSystem.Web.UI.WebControls;8 usingSystem.Web.UI.WebControls.WebParts;9 usingSystem.Web.UI.HtmlControls;Ten usingSystem.Xml; One usingSystem.Data.SqlClient; A usingSystem.IO; - usingSystem.Net; - usingSystem.Text; the usingSystem.Text.RegularExpressions; - usingSystem.Security.Cryptography; - - + /// <summary> - ///Encrypt + /// </summary> A /// <param name= "Ptoencrypt" >the string to encrypt</param> at /// <param name= "SKey" >Encryption Key Example: "ABCDEFGH"</param> - /// <returns></returns> - Public stringEncrypt (stringptoencrypt) - { - stringSKey ="ABCDEFGH"; -DESCryptoServiceProvider des =NewDESCryptoServiceProvider ();//Put the string in a byte array in byte[] Inputbytearray =Encoding.Default.GetBytes (ptoencrypt); - //byte[] Inputbytearray=encoding.unicode.getbytes (ptoencrypt); toDes. Key = ASCIIEncoding.ASCII.GetBytes (SKey);//establish keys and offsets for encrypted objects +DES.IV = ASCIIEncoding.ASCII.GetBytes (SKey);//GetBytes method of using Asciiencoding.ascii method in original text -MemoryStream ms =NewMemoryStream ();//make the input password must be entered in English text theCryptoStream cs =NewCryptoStream (MS, Des. CreateEncryptor (), cryptostreammode.write); *Cs. Write (Inputbytearray,0, inputbytearray.length); $ cs. FlushFinalBlock ();Panax NotoginsengStringBuilder ret =NewStringBuilder (); - foreach(byteBinchMs. ToArray ()) the { +Ret. AppendFormat ("{0:x2}", b); A } the ret. ToString (); + returnret. ToString (); - } $ /// <summary> $ ///decryption - /// </summary> - /// <param name= "Ptodecrypt" >to decrypt a string</param> the /// <param name= "SKey" >Decryption Key Example: "ABCDEFGH"</param> - /// <returns></returns>Wuyi Public stringDecrypt (stringptodecrypt) the { - stringSKey ="ABCDEFGH"; WuDESCryptoServiceProvider des =NewDESCryptoServiceProvider (); - byte[] Inputbytearray =New byte[Ptodecrypt.length/2]; About for(intx =0; X < Ptodecrypt.length/2; X + +) $ { - inti = (Convert.ToInt32 (ptodecrypt.substring (x *2,2), -)); -INPUTBYTEARRAY[X] = (byte) I; - } ADes. Key = ASCIIEncoding.ASCII.GetBytes (SKey);//establishes the key and offset of the encrypted object, which is important and cannot be modified +DES.IV =ASCIIEncoding.ASCII.GetBytes (SKey); theMemoryStream ms =NewMemoryStream (); -CryptoStream cs =NewCryptoStream (MS, Des. CreateDecryptor (), cryptostreammode.write); $Cs. Write (Inputbytearray,0, inputbytearray.length); the cs. FlushFinalBlock (); theStringBuilder ret =NewStringBuilder ();//Create Stringbuild object, Createdecrypt use Stream object, must turn decrypted text into stream object the returnSystem.Text.Encoding.Default.GetString (Ms. ToArray ()); the}