C # 3DES Encryption decryption

Source: Internet
Author: User

    /// <summary>    ///3DES Encryption/// </summary>    /// <param name= "OriginalValue" >Encrypt Data</param>    /// <param name= "key" >a 24-character key string</param>    /// <param name= "IV" >initialization vector string of 8-bit characters</param>    /// <returns></returns>     Public stringDesencrypt (stringOriginalValue,stringKeystringIV) {symmetricalgorithm sa;        ICryptoTransform CT;        MemoryStream MS;        CryptoStream CS; byte[] byt; SA=NewTripleDESCryptoServiceProvider (); Sa. Key=Encoding.UTF8.GetBytes (key); Sa.iv=Encoding.UTF8.GetBytes (IV); CT=SA.        CreateEncryptor (); Byt=Encoding.UTF8.GetBytes (OriginalValue); Ms=NewMemoryStream (); CS=NewCryptoStream (MS, CT, cryptostreammode.write); Cs. Write (Byt,0, Byt.        Length); Cs.        FlushFinalBlock (); Cs.        Close (); returnconvert.tobase64string (Ms.    ToArray ()); }     /// <summary>    ///3DES Decryption/// </summary>    /// <param name= "Data" >Decrypt Data</param>    /// <param name= "key" >24-character key string (required and encrypted)</param>    /// <param name= "IV" >initialization vector string of 8-bit characters (same as required for encryption)</param>    /// <returns></returns>     Public Static stringDesdecrypst (stringDatastringKeystringIV) {symmetricalgorithm MCSP=NewTripleDESCryptoServiceProvider (); Mcsp.key=Encoding.UTF8.GetBytes (key); Mcsp.iv=Encoding.UTF8.GetBytes (IV);        ICryptoTransform CT;        MemoryStream MS;        CryptoStream CS; byte[] byt; CT=Mcsp.createdecryptor (Mcsp.key, MCSP.IV); Byt=convert.frombase64string (data); Ms=NewMemoryStream (); CS=NewCryptoStream (MS, CT, cryptostreammode.write); Cs. Write (Byt,0, Byt.        Length); Cs.        FlushFinalBlock (); Cs.        Close (); returnEncoding.UTF8.GetString (Ms.    ToArray ()); }


Call Method:

String Deskey = "1DCRM4GORY8KODSGV1PPUHLB";//24-bit key
String Desiv = "Qcsj2skr"; 8-bit vector

var request = Desencrypt ("123456789", Deskey, Desiv);//encryption

var request = Desdecrypt ("123456789", Deskey, Desiv);//decryption



C # 3DES Encryption decryption

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.