3DES加解密类

using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace GT.Common.DES
{
    /// <summary>
    /// 3DES加解密类
    /// </summary>
    public class DESHelper
    {
       
        //构造一个对称算法
        private SymmetricAlgorithm mCSP = new TripleDESCryptoServiceProvider();
        #region 加密解密函数
        /// <summary>
        /// 字符串的加密
        /// </summary>
        /// <param name="Value">要加密的字符串</param>
        /// <param name="sKey">密钥,必须32位</param>
        /// <param name="sIV">向量,必须是8个字符</param>
        /// <returns>加密后的字符串</returns>
        public string EncryptString(string Value, string sKey,string sIV)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(sKey))
                {
                    sKey = key;
                }
                if (string.IsNullOrWhiteSpace(sIV))
                {
                    sIV = iv;
                }
                ICryptoTransform ct;
                MemoryStream ms;
                CryptoStream cs;
                byte[] byt;
                mCSP.Key = Convert.FromBase64String(sKey);
                byte[] biv=Encoding.Default.GetBytes (sIV);

                mCSP.IV = biv;
                //指定加密的运算模式
                mCSP.Mode = System.Security.Cryptography.CipherMode.ECB;
                //获取或设置加密算法的填充模式
                mCSP.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
                ct = mCSP.CreateEncryptor(mCSP.Key, mCSP.IV);//创建加密对象
                byt = Encoding.UTF8.GetBytes(Value);
                ms = new MemoryStream();
                cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
                cs.Write(byt, 0, byt.Length);
                cs.FlushFinalBlock();
                cs.Close();
                return Convert.ToBase64String(ms.ToArray());
            }
            catch (Exception ex)
            {
                return ("Error in Encrypting " + ex.Message);
            }
        }
        /// <summary>
        /// 解密字符串
        /// </summary>
        /// <param name="Value">加密后的字符串</param>
        /// <param name="sKey">密钥,必须32位</param>
        /// <param name="sIV">向量,必须是12个字符</param>
        /// <returns>解密后的字符串</returns>
        public string DecryptString(string Value, string sKey, string sIV)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(sKey))
                {
                    sKey = key;
                }
                if (string.IsNullOrWhiteSpace(sIV))
                {
                    sIV = iv;
                }
                ICryptoTransform ct;//加密转换运算
                MemoryStream ms;//内存流
                CryptoStream cs;//数据流连接到数据加密转换的流
                byte[] byt;
               
                //将3DES的密钥转换成byte
                mCSP.Key = Convert.FromBase64String(sKey);
                //将3DES的向量转换成byte
                mCSP.IV = Encoding.Default.GetBytes(sIV);
                mCSP.Mode = System.Security.Cryptography.CipherMode.ECB;
                mCSP.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
                ct = mCSP.CreateDecryptor(mCSP.Key, mCSP.IV);//创建对称解密对象
                byt = Convert.FromBase64String(Value);
                ms = new MemoryStream();
                cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
                cs.Write(byt, 0, byt.Length);
                cs.FlushFinalBlock();
                cs.Close();
                return Encoding.UTF8.GetString(ms.ToArray());
            }
            catch (Exception ex)
            {
                return ("Error in Decrypting " + ex.Message);
            }
        }
        #endregion

        #region KEY

         //<!--不要轻易修改,一定要保证跟生产环境的一致,防止丢失-->
         //<!--秘钥32位-->
         public static readonly string key = "aybcwdefgh1ntv2x3qr4u5o6igklszmp";
         //<!--向量8位-->
         public static readonly string iv = "85632147";

         #endregion

    }

}

 

posted @ 2019-09-02 14:02  萌橙  阅读(406)  评论(0编辑  收藏  举报