C # Cryptographic decryption (DES,3DES,MD5,BASE64) class

Source: Internet
Author: User
Tags key string pkcs7

Original: C # cryptographic decryption (DES,3DES,MD5,BASE64) class

Save!

public sealed class Encryptutils {#region Base64 encryption/decryption//<summary>//BASE64 encryption//        </summary>//<param name= "Input" > string to be encrypted </param>///<returns></returns>         public static string Base64encrypt (string input) {return Base64encrypt (input, New utf8encoding ()); }//<summary>//BASE64 encryption///</summary>/<param name= "Input" >         Strings that need to be encrypted </param>///<param name= "encode" > Character encoding </param>//<returns></returns> public static string Base64encrypt (string input, Encoding encode) {return Convert.tobase64strin G (encode.        GetBytes (input)); }///<summary>//BASE64 decryption///</summary>/<param name= "Input" > the string to be decrypted </param>//<returns></returns> public static string Base64decrypt (String input{return Base64decrypt (input, New utf8encoding ()); }///<summary>//BASE64 decryption///</summary>/<param name= "Input" > the string to be decrypted        </param>//<param name= "encode" > Character encoding </param>//<returns></returns> public static string Base64decrypt (string input, Encoding encode) {return encode.        GetString (convert.frombase64string (input));        } #endregion #region des encryption Decrypt//<summary>//DES encryption//</summary> <param name= "Data" > Encrypted data </param>//<param name= "key" >8 bit character key string </param>//&lt ;p Aram Name= "IV" >8-bit character initialization vector string </param>///<returns></returns> public static string DES Encrypt (string data, string key, String iv) {byte[] Bykey = System.Text.ASCIIEncoding.ASCII.GetBytes (ke            y); byte[] Byiv = System.teXt.            ASCIIEncoding.ASCII.GetBytes (iv);            DESCryptoServiceProvider CryptoProvider = new DESCryptoServiceProvider ();            int i = cryptoprovider.keysize;            MemoryStream ms = new MemoryStream ();            CryptoStream CST = new CryptoStream (MS, Cryptoprovider.createencryptor (Bykey, Byiv), cryptostreammode.write);            StreamWriter SW = new StreamWriter (CST); Sw.            Write (data); Sw.            Flush (); Cst.            FlushFinalBlock (); Sw.            Flush (); Return convert.tobase64string (Ms. GetBuffer (), 0, (int) Ms.        Length); }///<summary>//des decryption//</summary>/<param name= "Data" > Decrypt Data </par am>//<param name= "key" >8 bit character key string (required and encrypted) </param>//<param Name= "IV" >8 bit character initialization vector word (same as required and encrypted) </param>///<returns></returns> public static string Desdecrypt (String data, String key, String iv) {byte[] Bykey= System.Text.ASCIIEncoding.ASCII.GetBytes (key);            byte[] Byiv = System.Text.ASCIIEncoding.ASCII.GetBytes (iv);            Byte[] Byenc;            try {Byenc = convert.frombase64string (data);            } catch {return null;            } DESCryptoServiceProvider CryptoProvider = new DESCryptoServiceProvider ();            MemoryStream ms = new MemoryStream (BYENC);            CryptoStream CST = new CryptoStream (MS, Cryptoprovider.createdecryptor (Bykey, Byiv), cryptostreammode.read);            StreamReader sr = new StreamReader (CST); return Sr.        ReadToEnd (); } #endregion #region MD5 encryption//<summary>//MD5 encryption//</summary>// /<param name= "Input" > string required for encryption </param>//<returns></returns> public static string  Md5encrypt (String input) {return Md5encrypt (input, New utf8encoding ());      }///<summary>//MD5 encryption//</summary>/<param name= "input" > Required encryption        String </param>//<param name= "encode" > Character encoding </param>//<returns></returns> public static string Md5encrypt (string input, Encoding encode) {MD5 MD5 = new Md5cryptoserviceprovi            Der (); byte[] t = Md5.computehash (encode.            GetBytes (input));            StringBuilder sb = new StringBuilder (32); for (int i = 0; i < t.length; i++) sb. Append (T[i]. ToString ("X").            PadLeft (2, ' 0 ')); Return SB.        ToString (); }///<summary>//MD5 file stream encryption///</summary>//<param name= "SR" ></param            >///<returns></returns> public static string Md5encrypt (Stream stream) {            MD5 Md5serv = Md5cryptoserviceprovider.create ();            byte[] buffer = Md5serv.computehash (stream);StringBuilder sb = new StringBuilder (); foreach (byte var in buffer) sb. Append (var.            ToString ("X2")); Return SB.        ToString (); }///<summary>//MD5 encryption (return 16-bit encryption string)///</summary>//<param name= "input" >& lt;/param>//<param name= "encode" ></param>///<returns></returns> Publi C Static string md5encrypt16 (string input, Encoding encode) {MD5CryptoServiceProvider MD5 = new MD5CRYP            Toserviceprovider (); string result = Bitconverter.tostring (Md5.computehash (encode.            GetBytes (Input)), 4, 8); result = result.            Replace ("-", "" ");        return result;            #endregion #region 3DES encryption to decrypt public static string Des3encrypt (string data, string key) {            TripleDESCryptoServiceProvider DES = new TripleDESCryptoServiceProvider (); Des.            Key = ASCIIEncoding.ASCII.GetBytes (key);Des.            Mode = CIPHERMODE.CBC; Des.            Padding = PADDINGMODE.PKCS7; ICryptoTransform desencrypt = DES.            CreateEncryptor ();            byte[] Buffer = ASCIIEncoding.ASCII.GetBytes (data);        Return convert.tobase64string (Desencrypt.transformfinalblock (Buffer, 0, buffer.length)); } public static string Des3decrypt (string data, string key) {TripleDESCryptoServiceProvider DES            = new TripleDESCryptoServiceProvider (); Des.            Key = ASCIIEncoding.ASCII.GetBytes (key); Des.            Mode = CIPHERMODE.CBC; Des.            Padding = System.Security.Cryptography.PaddingMode.PKCS7; ICryptoTransform desdecrypt = DES.            CreateDecryptor ();            string result = "";                try {byte[] Buffer = convert.frombase64string (data);            result = ASCIIEncoding.ASCII.GetString (Desdecrypt.transformfinalblock (Buffer, 0, buffer.length));           } catch (Exception e) { } return result; } #endregion}

  

C # Cryptographic decryption (DES,3DES,MD5,BASE64) class

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.