A detailed description of the DES encryption and decryption MD5 in ASP.

Source: Internet
Author: User
Tags md5 encryption
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&gt    ; 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
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.