日期:2014-05-17  浏览次数:20801 次

C# 3DES加解密
项目当中为了和别人统一写了一个加解密的东西.加密完全无压力..解密就找不着门儿 求对应的解密方法

方法调用 TripleDESEncrypt("12345678", "H5O79RCD6A11KOVS") 
12345678使用key H5O79RCD6A11KOVS 加密结果 231D90593CB2269D

/// <summary>
        /// 加密方法
        /// </summary>
        /// <param name="pToEncrypt">源字符串</param>
        /// <param name="sKey">加密KEY</param>
        /// <returns>加密好的字符串</returns>
        public static string TripleDESEncrypt(string pToEncrypt, string sKey)
        {
            try
            {
                StringBuilder ret = new StringBuilder();
                TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
                des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
                des.Mode = CipherMode.ECB;
                des.Padding = PaddingMode.Zeros;
                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);

                byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);
                cs.Write(inputByteArray, 0, inputByteArray.Length);
                cs.FlushFinalBlock();

                foreach (byte b in ms.ToArray())
                {
                    ret.AppendFormat("{0:X2}", b);
                }
                return ret.ToString();
            }
            catch (Exception ex)
            {
                return ex.ToString();
            }
        }