加密工具类

public class Encrypt
    {
        /// <summary>
        /// MD5加密方法
        /// </summary>
        /// <param name="str">传入一个字符串</param>
        /// <returns></returns>
        public static string GetMd5(string str, bool toLower = true)
        {
            if (String.IsNullOrWhiteSpace(str))
            {
                Logger.Info("MD5参数空");
                return string.Empty;
            }
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            string a = BitConverter.ToString(md5.ComputeHash(Encoding.UTF8.GetBytes(str)));
            a = a.Replace("-", "");
            return toLower ? a.ToLower() : a;
        }

        /// <summary>  
        /// SHA1 加密,返回大写字符串  
        /// </summary>  
        /// <param name="content">需要加密字符串</param>  
        /// <param name="encode">指定加密编码</param>  
        /// <returns>返回40位大写字符串</returns>  
        public static string SHA1(string content, Encoding encode)
        {
            try
            {
                SHA1 sha1 = new SHA1CryptoServiceProvider();
                byte[] bytes_in = encode.GetBytes(content);
                byte[] bytes_out = sha1.ComputeHash(bytes_in);
                sha1.Dispose();
                string result = BitConverter.ToString(bytes_out);
                result = result.Replace("-", "");
                return result;
            }
            catch (Exception ex)
            {
                throw new Exception("SHA1加密出错:" + ex.Message);
            }
        }

        /// <summary>
        /// des加密
        /// </summary>
        /// <param name="datastr">需要加密的报文</param>
        /// <param name="keystr">秘钥</param>
        /// <returns></returns>
        public static string DesEncrypt(string datastr, string keystr)
        {
            DESCryptoServiceProvider desc = new DESCryptoServiceProvider();

            byte[] key = System.Text.Encoding.ASCII.GetBytes(keystr);
            byte[] data = System.Text.Encoding.Unicode.GetBytes(datastr);

            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms, desc.CreateEncryptor(key, key), CryptoStreamMode.Write);

            cs.Write(data, 0, data.Length);
            cs.FlushFinalBlock();

            return System.Convert.ToBase64String(ms.ToArray());
        }

        /// <summary>
        /// 8个bit位,是DES算法的初始化
        /// </summary>
        private static readonly byte[] Keys = new byte[] { 0x12, 0x34, 0x56, 120, 0x90, 0xab, 0xcd, 0xef };

        /// <summary>
        /// des解密
        /// </summary>
        /// <param name="datastr">需要解密的报文</param>
        /// <param name="keystr">秘钥</param>
        /// <returns></returns>
        public static string DesDecrypt(string decryptString, string decryptKey)
        {
            byte[] result = null;
            try
            {
                decryptKey = decryptKey.Length > 8 ? decryptKey.Substring(0, 8) : decryptKey;
                decryptKey = decryptKey.PadRight(8, ' ');
                byte[] bytes = Encoding.UTF8.GetBytes(decryptKey);
                byte[] keys = Keys;
                //判断是否是base64的字符串  
                if (Regular.IsBase64(decryptString) == false) //转换异常出现位置1,自己处理  
                    return string.Empty;
                byte[] buffer = Convert.FromBase64String(decryptString);
                DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
                MemoryStream stream = new MemoryStream();
                ICryptoTransform tran = provider.CreateDecryptor(bytes, keys);
                CryptoStream stream2 = new CryptoStream(stream, tran, CryptoStreamMode.Write);
                stream2.Write(buffer, 0, buffer.Length);
                // stream2.FlushFinalBlock(); //不处理异常情况  
                try
                {
                    stream2.FlushFinalBlock(); //转换异常出现位置2,使用try/catch 处理  
                }
                catch
                {
                    return string.Empty;
                }
                result = stream.ToArray();
                stream.Dispose();
                return Encoding.UTF8.GetString(result);
            }
            catch (Exception ex)
            {
                Logger.Error(ex.Message);
                return string.Empty;
            }
        }

        /// <summary>
        /// Base64加密
        /// </summary>
        /// <param name="encodeType">加密采用的编码方式</param>
        /// <param name="source">待加密的明文</param>
        /// <returns></returns>
        public static string Base64Encode(Encoding encodeType, string source)
        {
            string encode = string.Empty;
            byte[] bytes = encodeType.GetBytes(source);
            try
            {
                encode = Convert.ToBase64String(bytes);
            }
            catch
            {
                encode = source;
            }
            return encode;
        }

        /// <summary>
        /// Base64解密
        /// </summary>
        /// <param name="encodeType">解密采用的编码方式,注意和加密时采用的方式一致</param>
        /// <param name="result">待解密的密文</param>
        /// <returns>解密后的字符串</returns>
        public static string Base64Decode(Encoding encodeType, string result)
        {
            string decode = string.Empty;
            byte[] bytes = Convert.FromBase64String(result);
            try
            {
                decode = encodeType.GetString(bytes);
            }
            catch
            {
                decode = result;
            }
            return decode;
        }
    }
还在找我的道
原文地址:https://www.cnblogs.com/TimLiuDream/p/9897972.html