C# 3DES 加密

    public class DESHelper
    {
        #region 3des加密

        /// 
        /// 3des ecb模式加密
        /// 
        /// 待加密的字符串
        /// 密钥
        /// 加密矢量:只有在CBC解密模式下才适用
        /// 运算模式
        /// 加密后的字符串
        public static string Encrypt3Des(string aStrString, string aStrKey, CipherMode mode = CipherMode.ECB, string iv = "12345678")
        {
            try
            {
                var des = new TripleDESCryptoServiceProvider
                {
                    Key = Encoding.UTF8.GetBytes(aStrKey),
                    Mode = mode
                };
                if (mode == CipherMode.CBC)
                {
                    des.IV = Encoding.UTF8.GetBytes(iv);
                }
                var desEncrypt = des.CreateEncryptor();
                byte[] buffer = Encoding.UTF8.GetBytes(aStrString);
                buffer = Encoding.Convert(Encoding.GetEncoding("UTF-8"), Encoding.GetEncoding("GB2312"), buffer);
                return Convert.ToBase64String(desEncrypt.TransformFinalBlock(buffer, 0, buffer.Length));
            }
            catch (Exception e)
            {
                return string.Empty;
            }
        }

        #endregion

        #region 3des解密

        /// 
        /// des 解密
        /// 
        /// 加密的字符串
        /// 密钥
        /// 解密矢量:只有在CBC解密模式下才适用
        /// 运算模式
        /// 解密的字符串
        public static string Decrypt3Des(string aStrString, string aStrKey, CipherMode mode = CipherMode.ECB, string iv = "12345678")
        {
            try
            {
                var des = new TripleDESCryptoServiceProvider
                {
                    Key = Encoding.UTF8.GetBytes(aStrKey),
                    Mode = mode,
                    Padding = PaddingMode.PKCS7
                };
                if (mode == CipherMode.CBC)
                {
                    des.IV = Encoding.UTF8.GetBytes(iv);
                }
                var desDecrypt = des.CreateDecryptor();
                var result = "";
                byte[] buffer = Convert.FromBase64String(aStrString);
                result = Encoding.UTF8.GetString(desDecrypt.TransformFinalBlock(buffer, 0, buffer.Length));
                return result;
            }
            catch (Exception e)
            {
                return string.Empty;
            }
        }
        #endregion

    }


测试:注意key字符长度为24

        static void Main(string[] args)
        {
            //IV
            string IV = "20160120";

            //KEY
            string KEY = "012345678901234567891234";

            //data json string
            string dataJsonString = "TEST";
            Console.WriteLine("加密之前:"+ dataJsonString);

            //3des 加密
            string Encrypt3Des = DESHelper.Encrypt3Des(dataJsonString, KEY, CipherMode.CBC, IV);
            Console.WriteLine("加密之后:" + Encrypt3Des);

            //3des 解密
            string Decrypt3Des = DESHelper.Decrypt3Des(Encrypt3Des, KEY, CipherMode.CBC, IV);
            Console.WriteLine("解密密之后:" + Decrypt3Des);

            Console.ReadKey();
        }
C# 3DES 加密_第1张图片

你可能感兴趣的:([11],算法结构,[01],.NET随笔,3DES)