欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

ASP.Net Core数据加密

程序员文章站 2023-12-30 16:22:16
...

这里写自定义目录标题

数据加密

数据加密分为可逆和不可逆的。

可逆加密

  //默认**向量
        private static byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
        private static string DESKey = "CQYRZHXG";
        /// <summary>
        /// <summary>
        /// 加密【可逆】
        /// </summary>
        /// <param name="Text">需要加密的字符串</param>
        /// <returns></returns>
        public static string Encrypt(string Text)
        {
            return EncryptDES(Text, DESKey);
        }
        /// <summary> 
        /// 加密数据
        /// </summary> 
        /// <param name="Text"></param> 
        /// <param name="sKey"></param> 
        /// <returns></returns> 
        /// DES加密字符串
        /// </summary>
        /// <param name="encryptString">待加密的字符串</param>
        /// <param name="encryptKey">加***,要求为8位</param>
        /// <returns>加密成功返回加密后的字符串,失败返回源串</returns>
        public static string EncryptDES(string encryptString, string encryptKey)
        {
            try
            {
                byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));
                byte[] rgbIV = Keys;
                byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
                DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();
                MemoryStream mStream = new MemoryStream();
                CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
                cStream.Write(inputByteArray, 0, inputByteArray.Length);
                cStream.FlushFinalBlock();
                return Convert.ToBase64String(mStream.ToArray());
            }
            catch
            {
                return encryptString;
            }
        }



        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="Text">需要解密的字符串</param>
        /// <returns></returns>
        public static string Decrypt(string Text)
        {
            if (!string.IsNullOrEmpty(Text))
            {
                return DecryptDES(Text, DESKey);
            }
            else
            {
                return "";
            }
        }
        /// <summary>
        /// DES解密字符串
        /// </summary>
        /// <param name="decryptString">待解密的字符串</param>
        /// <param name="decryptKey">解***,要求为8位,和加***相同</param>
        /// <returns>解密成功返回解密后的字符串,失败返源串</returns>
        public static string DecryptDES(string decryptString, string decryptKey)
        {
            try
            {
                byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey);
                byte[] rgbIV = Keys;
                byte[] inputByteArray = Convert.FromBase64String(decryptString);
                DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider();
                MemoryStream mStream = new MemoryStream();
                CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
                cStream.Write(inputByteArray, 0, inputByteArray.Length);
                cStream.FlushFinalBlock();
                return Encoding.UTF8.GetString(mStream.ToArray());
            }
            catch
            {
                return decryptString;
            }
        }

注意:加密后的字符串可能包含‘/’或‘+’等特殊字符,所以若前端拿到后需要再次传入后台,那就需要转码【
例如:
某个请求返回加密后的数据,加密后返回的字符串为“2jsd9sdje+jxidnwmi/6==” 若直接将再次它传回台进行解密时,后台接收到的将会是“2jsd9sdje jxidnwmi/6==”,会将’+‘转为空格。所以需要进行转码:encodeURIComponent(“2jsd9sdje+jxidnwmi/6==”)这样后台接收到的就是原来的加密后的字符串了

不可逆加密

/// <summary>
        /// 用户密码加密【不可逆】
        /// </summary>
        /// <param name="userPwd">用户密码</param>
        /// <returns></returns>
        public string UserPwdEncrypt(string userPwd)
        {
            if (string.IsNullOrEmpty(userPwd)) userPwd = "";
             MD5 md5Hash = MD5.Create();
            byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(userPwd));

            // Create a new Stringbuilder to collect the bytes
            // and create a string.
            StringBuilder sBuilder = new StringBuilder();

            // Loop through each byte of the hashed data 
            // and format each one as a hexadecimal string.
            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }

            // Return the hexadecimal string.
            return sBuilder.ToString();

        }
相关标签: C# ASP.NETCore c#

上一篇:

下一篇: