Common string encryption and decryption methods

Source: Internet
Author: User

Using system;using system.collections.generic;using system.io;using system.linq;using System.Security.Cryptography; Using system.text;using system.text.regularexpressions;using system.web.security;namespace HuaTong.General.Utility {//<summary>///Encryption tool class///</summary> public class Encrypthelper {//default key Priv Ate static string Aeskey = "[45/*yuidse.        e;] ";        private static string Deskey = "[&hdn72]"; <summary>//AES encryption///</summary> public static string Aesencrypt (string value, string _aeskey = null) {if (string.            IsNullOrEmpty (_aeskey)) {_aeskey = Aeskey;            } byte[] Keyarray = Encoding.UTF8.GetBytes (_aeskey);            byte[] Toencryptarray = Encoding.UTF8.GetBytes (value);            RijndaelManaged Rdel = new RijndaelManaged ();            Rdel.key = Keyarray;            Rdel.mode = CIPHERMODE.ECB; Rdel.padding =PADDINGMODE.PKCS7;            ICryptoTransform ctransform = Rdel.createencryptor ();            byte[] Resultarray = Ctransform.transformfinalblock (toencryptarray, 0, toencryptarray.length);        Return convert.tobase64string (resultarray, 0, resultarray.length); }//<summary>//AES decryption//</summary> public static string Aesdecrypt (Strin G value, String _aeskey = null) {try {if (string.                IsNullOrEmpty (_aeskey)) {_aeskey = Aeskey;                } byte[] Keyarray = Encoding.UTF8.GetBytes (_aeskey);                byte[] Toencryptarray = convert.frombase64string (value);                RijndaelManaged Rdel = new RijndaelManaged ();                Rdel.key = Keyarray;                Rdel.mode = CIPHERMODE.ECB;                rdel.padding = PADDINGMODE.PKCS7;                ICryptoTransform ctransform = Rdel.createdecryptor (); Byte[] Resultarray = Ctransform.transformfinalblock (toencryptarray, 0, toencryptarray.length);            Return Encoding.UTF8.GetString (resultarray); } catch {return string.            Empty; }}///<summary>//DES encryption//</summary> public static string DESENCR Ypt (string value, string _deskey = null) {if (string.            IsNullOrEmpty (_deskey)) {_deskey = Deskey;            } byte[] Keyarray = Encoding.UTF8.GetBytes (_deskey);            byte[] Toencryptarray = Encoding.UTF8.GetBytes (value);            DESCryptoServiceProvider Rdel = new DESCryptoServiceProvider ();            Rdel.key = Keyarray;            Rdel.mode = CIPHERMODE.ECB;            rdel.padding = PADDINGMODE.PKCS7;            ICryptoTransform ctransform = Rdel.createencryptor (); byte[] Resultarray = Ctransform.transformfinalblock (toencryptarray, 0, Toencryptarray.            Length);        Return convert.tobase64string (resultarray, 0, resultarray.length); }///<summary>//des decryption//</summary> public static string Desdecrypt (Strin G value, String _deskey = null) {try {if (string.                IsNullOrEmpty (_deskey)) {_deskey = Deskey;                } byte[] Keyarray = Encoding.UTF8.GetBytes (_deskey);                byte[] Toencryptarray = convert.frombase64string (value);                DESCryptoServiceProvider Rdel = new DESCryptoServiceProvider ();                Rdel.key = Keyarray;                Rdel.mode = CIPHERMODE.ECB;                rdel.padding = PADDINGMODE.PKCS7;                ICryptoTransform ctransform = Rdel.createdecryptor ();                byte[] Resultarray = Ctransform.transformfinalblock (toencryptarray, 0, toencryptarray.length);          Return Encoding.UTF8.GetString (resultarray);  } catch {return string.            Empty; }} public static string MD5 (string value) {byte[] result = Encoding.UTF8.GetBytes (value)            ;            MD5 MD5 = new MD5CryptoServiceProvider ();            byte[] Output = Md5.computehash (result); return bitconverter.tostring (Output).        Replace ("-", "" "); The public static string HMACMD5 (string value, String hmackey) {HMACSHA1 hmacsha1 = new HMACSHA1 (E Ncoding. UTF8.            GetBytes (Hmackey));            Byte[] result = System.Text.Encoding.UTF8.GetBytes (value);                        byte[] Output = Hmacsha1.computehash (result); return bitconverter.tostring (Output).        Replace ("-", "" ");        }//<summary>//Base64 codes//</summary>//<returns></returns> public static string Base64Encode (string value) {string result = Convert.tobase64string (encoding.d Efault.            GetBytes (value));        return result;        }//<summary>//Base64 decoding//</summary>//<returns></returns> public static string Base64decode (string value) {string result = Encoding.Default.GetString (conver            T.frombase64string (value));        return result; }    }}

Common string encryption and decryption methods

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.