3DES encryption and decryption,

Source: Internet
Author: User
Tags pkcs7

3DES encryption and decryption,

C # 3DES encryption and decryption, available for JAVA and PHP

Using System;
Using System.Security.Cryptography;
Using System.Text;

Namespace TT.Utilities.Encrypt
{
    Public class DES3
    {
        /// <summary>
        /// utf-8 encoding
        /// Encryption mode ECB, padding type PKCS7
        /// </summary>
        /// <param name="str_content"></param>
        /// <param name="str_keys">24-bit key</param>
        /// <returns></returns>
        Public static string DES3_Encrypt(string str_content, string str_keys)
        #region
        {
            Encoding encoding = Encoding.UTF8;

            Byte[] content = encoding.GetBytes(str_content);
            Byte[] keys = encoding.GetBytes(str_keys);

            TripleDESCryptoServiceProvider tdsc = new TripleDESCryptoServiceProvider();

            / / specify the key length, the default is 192
            tdsc.KeySize = 128;
            / / Use the specified key and IV (encryption vector)
            tdsc.Key = keys;
            //tdsc.IV = IV;
            //encryption mode, offset
            tdsc.Mode = CipherMode.ECB;
            tdsc.Padding = PaddingMode.PKCS7;
            / / Perform encryption conversion operation
            ICryptoTransform ct = tdsc.CreateEncryptor();
            //8 is critical, the result of the encryption is an 8-byte array
            Byte[] results = ct.TransformFinalBlock(content, 0, content.Length);

            String base64String = Convert.ToBase64String(results);
            Return base64String;
        }
        #endregion

        /// <summary>
        /// utf-8 encoding
        /// Encryption mode ECB, padding type PKCS7
        /// </summary>
        /// <param name="base64_content"></param>
        /// <param name="str_keys">24-bit key</param>
        /// <returns></returns>
        Public static string DES3_Decrypt(string base64_content, string str_keys)
        #region
        {
            Encoding encoding = Encoding.UTF8;

            Byte[] content = Convert.FromBase64String(base64_content);
            Byte[] keys = encoding.GetBytes(str_keys);

            TripleDESCryptoServiceProvider tdsc = new TripleDESCryptoServiceProvider();

            / / specify the key length, the default is 192
            tdsc.KeySize = 128;
            / / Use the specified key and IV (encryption vector)
            tdsc.Key = keys;
            //tdsc.IV = IV;
            //encryption mode, offset
            tdsc.Mode = CipherMode.ECB;
            tdsc.Padding = PaddingMode.PKCS7;
            / / Perform encryption conversion operation
            ICryptoTransform ct = tdsc.CreateDecryptor();
            //8 is critical, the result of the encryption is an 8-byte array
            Byte[] results = ct.TransformFinalBlock(content, 0, content.Length);

            String oriString = encoding.GetString(results);
            Return oriString;
        }
        #endregion
    }
} 

 


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.