This article mainly introduces C # common string encryption and decryption method package code, the need for friends can refer to the
Code as follows://Method I//must add to the system.web reference//using System.Web.Security; <summary>///SHA1 Encrypted string///</summary>///<param name= "source" > Source string </param>///<returns > Encrypted string </returns> public string SHA1 (string source) { return FormsAuthentication.HashPasswordForStoringInConfigFile (source, "SHA1"); ///<summary>///MD5 Cryptographic string///</summary>///<param name= "source" > Source string </param>///<returns > Encrypted string </returns> public string MD5 (string source) { return FormsAuthentication.HashPasswordForStoringInConfigFile (source, "MD5");; } /Method II (Reversible encryption decryption)://using System.Security.Cryptography; public string Encode (string data) { byte[] Bykey = System.Text.ASCIIEncoding.ASCII.GetBytes (key_64); byte[] Byiv = System.Text.ASCIIEncoding.ASCII.GetBytes (iv_64); DESCryptoServiceProvider CryptoProvider = new DESCryptoServiceProvider (); INT i =Cryptoprovider.keysize; MemoryStream ms = new MemoryStream (); CryptoStream CST = new CryptoStream (MS, Cryptoprovider.createencryptor (Bykey, Byiv), CryptoStreamMode.Write); StreamWriter sw = new StreamWriter (CST); SW. Write (data); SW. Flush (); CST. FlushFinalBlock (); SW. Flush (); Return convert.tobase64string (Ms. GetBuffer (), 0, (int) Ms. Length); public string Decode (string data) { byte[] Bykey = System.Text.ASCIIEncoding.ASCII.GetBytes (key_64);   ; byte[] Byiv = System.Text.ASCIIEncoding.ASCII.GetBytes (iv_64); byte[] Byenc; Try { BYENC = convert.frombase64string (data); } &nbs P Catch { return null; } Descryptoserviceprovid Er cryptoprovider = new DESCryptoServiceProvider (); MemorystreaM ms = new MemoryStream (BYENC); CryptoStream CST = new CryptoStream (MS, Cryptoprovider.createdecryptor (Bykey, Byiv), CryptoStreamMode.Read); StreamReader sr = new StreamReader (CST); /Method III (MD5 irreversible)://using System.Security.Cryptography; MD5 irreversible encryption//32-bit encryption public string getmd5_32 (string s, String _input_charset) { MD5 MD5 = new Md5cryptoservice Provider (); byte[] t = Md5.computehash (encoding.getencoding (_input_charset). GetBytes (s)); StringBuilder sb = new StringBuilder (32); for (int i = 0; i < t.length i++) { SB. Append (T[i]. ToString ("X"). PadLeft (2, ' 0 ')); } return SB. ToString (); }//16-bit encryption public static string Getmd5_16 (String convertstring) { MD5CryptoServiceProvider MD5 = new Md5crypt Oserviceprovider (); String t2 = bitconverter.tostring (Md5.computehash UTF8Encoding.Default.GetBytes (convertstring)), 4, 8); t2 = t2. Replace ("-", ""); return T2; } /method four (symmetric encryption)://using System.IO; Using System.Security.Cryptography; Private SymmetricAlgorithm Mobjcryptoservice; private string Key; <summary> ///Symmetric cryptographic class constructors ///</summary> public Symmetricmethod () { Mobjcryptoservice = new RijndaelManaged (); Key = "Guz (%&hj7x89h$yubi0456ftmat5&fvhufcy76*h% hilj$lhj!y6& (*jkp87jh7;}///<summary > ///access key ///</summary> ///<returns> key </returns> & nbsp Private byte[] Getlegalkey () { string stemp = Key; Mobjcryptoservice.generatekey (); &NB Sp byte[] Byttemp = Mobjcryptoservice.key; int keylength = byttemp.length; if (Stemp.length > Keylength) stemp = stemp.substring (0, keylength); ELSE if(Stemp.length < Keylength) stemp = Stemp.padright (Keylength, ""); return ASCIIEncoding.ASCII.GetBytes (stemp); ///<summary> ///get initial vector iv ///</summary> ///<returns> First-class vector iv& Lt;/returns> Private byte[] Getlegaliv () { string stemp = "E4ghj*ghg7!rnifb&95guy86gfgh UB#ER57HBH (U%G6HJ ($jhWk 7&!hg4ui% $HJK "; MOBJCRYPTOSERVICE.GENERATEIV (); byte[] Byttemp = MOBJCRYPTOSERVICE.IV; int ivlength = byttemp.length; if (Stemp.length > Ivlength) stemp = stemp.substring (0, ivlength); ELSE if (Stemp.length < ivlength) stemp = Stemp.padright (Ivlength, ""); return ASCIIEncoding.ASCII.GetBytes (stemp); ///<summary> ///encryption method ///</summary> ///<param name= "Source" > strings to be encrypted</param> ///<returns> Encrypted string </returns> public string Encrypto (string SOURCE) { byte[] Bytin = UTF8Encoding.UTF8.GetBytes (source); MemoryStream ms = new Memorystrea M (); Mobjcryptoservice.key = Getlegalkey (); MOBJCRYPTOSERVICE.IV = Getlegaliv (); ICryptoTransform encrypto = Mobjcryptoservice.createencryptor (); CryptoStream cs = new CryptoStream (MS, Encrypto, CryptoStreamMode.Write); CS. Write (bytin, 0, bytin.length); CS. FlushFinalBlock (); Ms. Close (); byte[] Bytout = Ms. ToArray (); return convert.tobase64string (bytout); ///<summary> ///decryption method ///</summary> ///<param name= "Source" > strings to decrypt </param> ///<returns> decrypted string </returns> public string Decrypto (string Source) { byte[] Bytin = ConVert. FromBase64String (Source); MemoryStream ms = new MemoryStream (bytin, 0, bytin.length); Mobjcryptoservice.key = Getlegalkey (); MOBJCRYPTOSERVICE.IV = Getlegaliv (); ICryptoTransform encrypto = Mobjcryptoservice.createdecryptor (); CryptoStream cs = new CryptoStream (MS, Encrypto, CryptoStreamMode.Read); StreamReader sr = new StreamReader (CS); return Sr. ReadToEnd (); //Method V://using System.IO; Using System.Security.Cryptography; Using System.Text; Default key vector private static byte[] keys = {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF}; /**//**//**////<summary>///des encrypted string keleyi.com///</summary>///<param name= "EncryptString" > The strings to be encrypted </param>///<param name= "Encryptkey" > Encryption keys, which require a successful return of the encrypted string for 8-bit </param>///<returns> encryption. Failed to return source string </returns> public static string Encryptdes (String encryptstring, String encryptkey) { try &nbsP { byte[] Rgbkey = Encoding.UTF8.GetBytes (encryptkey.substring (0, 8)); &NB Sp byte[] Rgbiv = Keys; byte[] Inputbytearray = Encoding.UTF8.GetBytes (encryptstring); DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider (); MemoryStream mstream = new MemoryStream (); CryptoStream cstream = new CryptoStream (Mstream, Dcsp.createencryptor (Rgbkey, Rgbiv), Cryptos Treammode.write); Cstream.write (inputbytearray, 0, inputbytearray.length); Cstream.flushfinalblock (); return convert.tobase64string (Mstream.toarray ()); } catch { return encryptstring }}/* *//**//**////<summary>///des decryption string keleyi.com///</summary>///<param name= "decryptstring" > The string to be decrypted </param>///<param name= "Decryptkey" > Decryption key, required 8-bit, same as encryption key </param>///<returns> Decryption successfully returned decrypted string, failed back source string </returns> public static string Decryptdes (String decryptstring, String decryptkey) { & nbsp Try { byte[] Rgbkey = Encoding.UTF8.GetBytes (Decryptkey); & nbsp byte[] Rgbiv = Keys; byte[] Inputbytearray = convert.frombase64string (decryptstring); DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider (); MemoryStream mstream = new MemoryStream (); CryptoStream cstream = new CryptoStream (Mstream, DCSP. CreateDecryptor (Rgbkey, Rgbiv), cryptostreammode.write); Cstream.write (inputbytearray, 0, inputbytearray.length); Cstream.flushfinalblock (); return Encoding.UTF8.GetString (Mstream.toarray ()); } catch { return decryptstring }&NB Sp /Method VI (File encryption)://using System.IO; Using System.Security.Cryptography; Using System.Text; Encrypted files private static void EncryptData (String inname, String outname, byte[] Deskey, byte[] desiv) { //creat E The file streams to handle the input and output files. FileStream fin = new FileStream (inname, FileMode.Open, FileAccess.Read); FileStream fout = new FileStream (outname, FileMode.OpenOrCreate, FileAccess.Write); Fout. SetLength (0); //create variables to help with read and write. byte[] bin = new byte[100]; This is intermediate storage for the encryption. Long Rdlen = 0; //this is the total number of bytes written. Long Totlen = fin. Length; //this is the total length of the input FIle. int Len; //this is the number of bytes to written in a Tim E. des des = new descryptoserviceprovider (); CryptoStream encstream = new CryptoStream (Fout, Des. CreateEncryptor (Deskey, Desiv), cryptostreammode.write); //read from the input file, then encrypt and write to the output file. while (Rdlen < Totlen) { len = fin. Read (Bin, 0, 100); Encstream.write (bin, 0, Len); Rdlen = Rdlen + len; } Encstream.close (); Fout. Close (); Fin. Close (); }//Decrypt files private static void Decryptdata (String inname, String outname, byte[] Deskey, byte[] desiv) { //cre Ate the file streams to handle the input and output files. FileStream fin = new FileStream (inname, FileMode.Open, fileaccesS.read); FileStream fout = new FileStream (outname, FileMode.OpenOrCreate, FileAccess.Write); Fout. SetLength (0); //create variables to help with read and write. byte[] bin = new byte[100]; This is intermediate storage for the encryption. Long Rdlen = 0; //this is the total number of bytes written. Long Totlen = fin. Length; //this is the total length of the input file. int Len; //this is the number of bytes to written in a Tim E. des des = new descryptoserviceprovider (); CryptoStream encstream = new CryptoStream (Fout, Des. CreateDecryptor (Deskey, Desiv), cryptostreammode.write); //read from the input file, then encrypt and write to the output file. while (Rdlen < Totlen) {   len = fin. Read (Bin, 0, 100); Encstream.write (bin, 0, Len); Rdlen = Rdlen + len; } Encstream.close (); Fout. Close (); Fin. Close (); return Sr. ReadToEnd (); } Detailed source reference: http://www.jb51.net/article/44133.htm