ASP. NET encryption Decryption class

Source: Internet
Author: User
Tags sha1 sha1 encryption
Here you have a ready-made C # Cryptographic decryption class, provided by admin10000.com, including MD5, SHA1, des Encryption and decryption, BASE64 encoding decoding.

Import Namespaces First

      Using System.Web.Security;     Using System.Security.Cryptography;     Using System.Text;     Using System.IO;

<summary>
Encrypthelper from www.Admin10000.com
</summary>

public class encrypthelper{//provides 8 characters as the DES key (the program automatically intercepts the first 8 characters) private static string key = "admin10000.com";                <summary>//des symmetric encryption decrypted key///</summary> public static string key {                get {return key;                } set {key = value; }}///<summary>//MD5 encryption (irreversible encryption)///</summary>//<param name= "pass > Original string to encrypt </param>//<returns></returns> public static string Md5encrypt (String pass ) {System.Security.Cryptography.MD5 MD5 = new System.Security.Cryptography.MD5CryptoServiceProv                Ider ();                byte[] Bytresult = Md5.computehash (System.Text.Encoding.UTF8.GetBytes (pass)); MD5.                Clear ();                String strresult = Bitconverter.tostring (Bytresult); Strresult = Strresult.replace ("-", "" ");                        return strresult;                /* Another method pass = FormsAuthentication.HashPasswordForStoringInConfigFile (pass, "MD5");                return pass; */}///<summary>//SHA1 encryption (irreversible encryption)///</summary>//<param name = "Pass" > Raw string to encrypt </param>//<returns></returns> public static string Sha1encrypt (Stri Ng Pass) {System.Security.Cryptography.SHA1 SHA1 = new System.Security.Cryptography.SHA1CryptoServi                Ceprovider ();                byte[] Bytresult = Sha1.computehash (System.Text.Encoding.UTF8.GetBytes (pass)); SHA1.                Clear ();                String strresult = Bitconverter.tostring (Bytresult);                strresult = Strresult.replace ("-", "" ");                return strresult; /* Another method pass = FormsAuthentication.HashPasswordForStoringInConfigFile (pass, "SHA1 ");                return pass; */}//<summary>//DES encrypted strings///</summary>//<param name= "encrypts Tring "> Strings to be encrypted </param>//<returns> encryption successfully returns the encrypted string, failed to return the source string </returns> public static Strin G Desencrypt (String encryptstring) {try {byte[] RgbKey = ENC Oding. UTF8. GetBytes (key.                        Substring (0, 8));                        byte[] Rgbiv = RgbKey;                        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 ();                        Cstream.close ();                Return convert.tobase64string (Mstream.toarray ());                } catch {return encryptstring; }}///<summary>//des decryption string//</summary>//<param name= "decryptst Ring > string to Decrypt </param>//<returns> decryption successfully returns the decrypted string, failed back source string </returns> public static string Desdecrypt (String decryptstring) {try {byte[] RgbKey = encod Ing. UTF8. GetBytes (key.                        Substring (0, 8));                        byte[] Rgbiv = RgbKey;                        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 ();                        Cstream.close ();                Return Encoding.UTF8.GetString (Mstream.toarray ());                } catch {return decryptstring; }}///<summary>//Encode normal string as BASE64 strings///</summary>//<param name= " str "> Source string </param>//<returns></returns> public static string Base64Encode (String str        ) {return convert.tobase64string (Encoding.UTF8.GetBytes (str)); }///<summary>//Decode BASE64 string///</summary>//<param name= "str" >base64 string   </param>//<returns></returns> public static string Base64decode (String base64str)     {return Encoding.UTF8.GetString (convert.frombase64string (BASE64STR)); }}
  • 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.