DES加密/解密类

 /// <summary>
    /// DES加密/解密类。
    /// </summary>
    public class DESEncrypt
    {

        #region ========加密========

        /// <summary>
        /// 加密
        /// </summary>
        /// <param name="Text"></param>
        /// <returns></returns>
        public static string Encrypt(string Text)
        {
            return Encrypt(Text, "ADMIN");
        }
        /// <summary> 
        /// 加密数据 
        /// </summary> 
        /// <param name="Text"></param> 
        /// <param name="sKey"></param> 
        /// <returns></returns> 
        public static string Encrypt(string Text, string sKey)
        {
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            byte[] inputByteArray;
            inputByteArray = Encoding.Default.GetBytes(Text);
            des.Key = ASCIIEncoding.ASCII.GetBytes(Encryption(sKey).Substring(0, 8));
            des.IV = ASCIIEncoding.ASCII.GetBytes(Encryption(sKey).Substring(0, 8));
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();
            StringBuilder ret = new StringBuilder();
            foreach (byte b in ms.ToArray())
            {
                ret.AppendFormat("{0:X2}", b);
            }
            return ret.ToString();
        }

        /// <summary> 
        /// 加密数据 
        /// </summary> 
        /// <param name="textByte">明文数组</param> 
        /// <param name="keyByte">密钥数组</param> 
        /// <returns>成功返回密文数组,失败返回null</returns> 
        public byte[] Encrypt(byte[] textByte, byte[] keyByte)
        {
            if (textByte == null || keyByte == null || textByte.Length == 0)
                return null;

            if (keyByte.Length != 8)
            {
                byte[] keys = new byte[8];
                for (int i = 0; i < 8; i++)
                {
                    if (i < keyByte.Length)
                        keys[i] = keyByte[i];
                    else
                        keys[i] = 0x20;//0x20(16) =32(10) 为空格的UTF8编码
                }
                keyByte = keys;
            }

            DESCryptoServiceProvider des = new DESCryptoServiceProvider
            {
                // java 默认的是ECB模式,PKCS5padding;c#默认的CBC模式,PKCS7padding 所以这里我们默认使用ECB方式
                Mode = CipherMode.ECB,
                Key = keyByte,
                IV = keyByte
            };

            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            try
            {
                CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
                cs.Write(textByte, 0, textByte.Length);
                cs.FlushFinalBlock();

                return ms.ToArray();
            }
            catch
            {
                return null;
            }
        }

        #endregion

        #region ========解密========

        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="Text"></param>
        /// <returns></returns>
        public static string Decrypt(string Text)
        {
            return Decrypt(Text, "ADMIN");
        }
        /// <summary> 
        /// 解密数据 
        /// </summary> 
        /// <param name="Text"></param> 
        /// <param name="sKey"></param> 
        /// <returns></returns> 
        public static string Decrypt(string Text, string sKey)
        {
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            int len;
            len = Text.Length / 2;
            byte[] inputByteArray = new byte[len];
            int x, i;
            for (x = 0; x < len; x++)
            {
                i = Convert.ToInt32(Text.Substring(x * 2, 2), 16);
                inputByteArray[x] = (byte)i;
            }
            
            des.Key = ASCIIEncoding.ASCII.GetBytes(Encryption(sKey).Substring(0, 8));
            des.IV = ASCIIEncoding.ASCII.GetBytes(Encryption(sKey).Substring(0, 8));
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();
            return Encoding.Default.GetString(ms.ToArray());
        }

        /// <summary> 
        /// 解密数据 
        /// </summary> 
        /// <param name="textByte">密文数组</param> 
        /// <param name="keyByte">密钥数组,长度必须等于8</param> 
        /// <returns>成功返回明文数组,失败返回null</returns> 
        public byte[] Decrypt(byte[] textByte, byte[] keyByte)
        {
            if (textByte == null || keyByte == null || textByte.Length == 0)
                return textByte;

            if (keyByte.Length != 8)
            {
                byte[] keys = new byte[8];
                for (int i = 0; i < 8; i++)
                {
                    if (i < keyByte.Length)
                        keys[i] = keyByte[i];
                    else
                        keys[i] = 0x20;//0x20(16) =32(10) 为空格的UTF8编码
                }
                keyByte = keys;
            }

            DESCryptoServiceProvider des = new DESCryptoServiceProvider
            {
                // java 默认的是ECB模式,PKCS5padding;c#默认的CBC模式,PKCS7padding 所以这里我们默认使用ECB方式
                Mode = CipherMode.ECB,
                Key = keyByte,
                IV = keyByte
            };

            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            try
            {
                CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
                cs.Write(textByte, 0, textByte.Length);
                cs.FlushFinalBlock();

                return ms.ToArray();
            }
            catch
            {
                return textByte;
            }
        }

        #endregion

        /// <summary>
        /// MD5/SHA1加密
        /// </summary>
        /// <param name="s"></param>
        /// <param name="isMd5"></param>
        /// <returns></returns>
        public static string Encryption(string s, bool isMd5=true)
        {
            if (isMd5)
            {
                using (var md5 = MD5.Create())
                {
                    var result = md5.ComputeHash(Encoding.UTF8.GetBytes(s));
                    var strResult = BitConverter.ToString(result);
                    return strResult.Replace("-", "").ToUpper();
                }
            }
            using (var sha1 = SHA1.Create())
            {
                var result = sha1.ComputeHash(Encoding.UTF8.GetBytes(s));
                var strResult = BitConverter.ToString(result);
                return strResult.Replace("-", "").ToUpper();
            }
        }
    }
原文地址:https://www.cnblogs.com/nayilvyangguang/p/13054883.html