This article mainly introduces the example code of the DES encryption and decryption MD5 encryption helper class in ASP. Or it's a good one to see.
public class Trialhelper {//default key vector private static byte[] Keys = {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF} ; <summary>//DES encrypted strings///</summary>//<param name= "encryptstring" > Strings to be encrypted </param> <param name= "Encryptkey" > Encryption key, requires 8-bit </param>//<returns> encryption to successfully return the encrypted string, failed to return the source string </returns> ; public static string Encryptdes (String encryptstring, String encryptkey = "") {try {if (string). IsNullOrEmpty (Encryptkey) | | Encryptkey.length < 8) {Encryptkey = "winform01"; } byte[] RgbKey = Encoding.UTF8.GetBytes (encryptkey.substring (0, 8)); 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), CryptostreaMmode.write); Cstream.write (Inputbytearray, 0, inputbytearray.length); Cstream.flushfinalblock (); Return convert.tobase64string (Mstream.toarray ()); } catch {return encryptstring; }}///<summary>//des decryption string///</summary>//<param name= "decryptstring" > String to Decrypt < /param>//<param name= "Decryptkey" > Decryption key, required for 8-bit, same as encryption key </param>//<returns> decryption succeeded in returning decrypted string, failed to return SOURCE string </returns> public static string Decryptdes (String decryptstring, String decryptkey = "") {try { if (string. IsNullOrEmpty (Decryptkey) | | Decryptkey.length < 8) {Decryptkey = "winform01"; } byte[] RgbKey = Encoding.UTF8.GetBytes (decryptkey.substring (0, 8)); byte[] Rgbiv = Keys; byte[] Inputbytearray = convert.frombase64string (decryptstring); DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider (); MemoryStream mstream = newMemoryStream (); 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; }}///<summary>//MD5 Data encryption///</summary>//<param name= "Sdatain" > Encrypted fields </param> <returns> Encrypted strings </returns> public static string GetMD5 (String sdatain) {system.security. Cryptography.md5cryptoserviceprovider MD5 = new System.Security.Cryptography.MD5CryptoServiceProvider (); Byte[] Bytvalue, Bythash; Bytvalue = System.Text.Encoding.UTF8.GetBytes (Sdatain); Bythash = Md5.computehash (Bytvalue); MD5. Clear (); String stemp = ""; for (int i = 0; i < bythash.length; i++) {stemp + = Bythash[i]. ToString ("X"). PadLeft (2, ' 0 '); }return stemp; } }
Call:
Get login information loginrecord.name = TbName.Text.Trim (); Loginrecord.md5pwd = TRIALHELPER.GETMD5 (Tbpwd.password); Save to database MD5 encryption mode LOGINRECORD.PWD = Trialhelper.encryptdes (Tbpwd.password);//Remember Password des encryption, save to local