String encryption common to ASP

Source: Internet
Author: User
Tags decrypt md5 encryption sha1

There are three types of encryption used in strings

1, MD5 encryption, this is often used for passwords, one-way encryption, non-decryption, some online decryption can be the majority of the solution, with code can not be achieved, if you do not want to decrypt, after the encryption of a random interception of a good;

2, Base64 bit encryption, usually encrypted after the end of the string will have two = =, can be decrypted;

3, SHA encryption, one-way encryption, security is not MD5 good.

The above non-decryption is relative, these encryption algorithms are cross-platform.

                Using System.Web.Security;        Using System.Security.Cryptography;        <summary>//MD5 functions///</summary>//<param name= "val" > Raw string </param> &LT;RETURNS&GT;MD5 results </returns> public static string Md5string (string val) {if (VA L.trim ().            Equals (String.Empty) = = true) return String.Empty;            Byte[] B = Encoding.UTF8.GetBytes (val); b = New MD5CryptoServiceProvider ().            ComputeHash (b);            string ret = String.Empty; for (int i = 0; i < b.length; i++) ret + = B[i]. ToString ("X").            PadLeft (2, ' 0 ');        return ret; }///<summary>//SHA1 string encryption///</summary>//<param name= "obj" ></para m>//<returns></returns> public static string SHA1 (String obj) {return F Ormsauthentication.hashpasswordforstoringinconfigfile (obJ, "SHA1"); }///<summary>//SHA256 string encryption///</summary>//<param name= "obj" ></pa ram>//<returns></returns> public static string SHA256 (String obj) {//re            Turn System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile (obj, "SHA256");            byte[] Bytvalue = System.Text.Encoding.UTF8.GetBytes (obj);            SHA256 sha256 = new sha256managed ();            byte[] RetVal = Sha256.computehash (Bytvalue);            StringBuilder sb = new StringBuilder (); for (int i = 0; i < retval.length; i++) {sb. Append (Retval[i].            ToString ("X2")); } return SB.        ToString (); }///<summary>//BASE64-bit encryption//</summary>//<param name= "Val" ></par         am>//<returns></returns> public static string EncryptBase64 (string val) {   Return Convert.tobase64string (Encoding.UTF8.GetBytes (Val)); }///<summary>//Base64 bit decryption///</summary>//<param name= "Val" ></para            m>//<returns></returns> public static string DecryptBase64 (string val) {        Return Encoding.Default.GetString (Convert.frombase64string (Val)); }//<summary>//BASE64 bit (MD5 encryption) [Standard, non-MD5 encryption and BASE64 bit encryption]///</summary>//<par Am Name= "val" > Original string </param>//&LT;RETURNS&GT;MD5 result </returns> public static string Encryptba Se64andmd5 (string val) {if (val. Trim ().            Equals (String.Empty) = = true) return String.Empty;            MD5CryptoServiceProvider MD5CSP = new MD5CryptoServiceProvider ();        Return convert.tobase64string (Md5csp.computehash (Encoding.UTF8.GetBytes (Val)));      }

You can also define a key, custom encryption algorithm

        <summary>//DES encrypted strings///</summary>//<param name= "encryptstring" > Pending Encrypted strings </param>///<param Name= "Encryptkey" > Encryption key, required for 8-bit </param>///<returns> encryption for successful return of encryption            After the string, the failure returns the source string </returns> public static string Encode (String encryptstring, String encryptkey) {            Encryptkey = utilsstring.substring (Encryptkey, 8);            Encryptkey = Encryptkey.padright (8, ");            byte[] RgbKey = Encoding.UTF8.GetBytes (encryptkey.substring (0, 8));            Byte[] Rgbiv = {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};            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 ()); }///<summary>//des decryption strings///</summary>//<param name= "decryptstring" > String to decrypt </param>///<param Name= "Decryptkey" > Decryption key, required for 8-bit, same as encryption key </param>//<returns&gt            Decryption succeeded in returning the decrypted string, failed to return to source string </returns> public static string Decode (String decryptstring, String decryptkey) {            Decryptkey = utilsstring.substring (Decryptkey, 8);            Decryptkey = Decryptkey.padright (8, ");            byte[] RgbKey = Encoding.UTF8.GetBytes (Decryptkey);            Byte[] Rgbiv = {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};            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 ()); }

  

String encryption common to ASP

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.