C#教程之AES加解密

pk10开奖历史

VB.net 2010 视频教程 VB.net 2010 视频教程 VB.net 2010 视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
pk10开奖历史 > 编程开发 > C#编程 >
  • C#教程之AES加解密

  • 2019-06-28 14:19 来源:未知

一个程序最忌讳的就是把自己的数据暴露给其他人,一个加解密可以让你感觉安全性好一些

直接上代码

 

一个加密类

复制代码
using System;
using System.IO;
using System.Security.Cryptography;
using System。Text;
namespace Common
{
    public class AECHelper
    {
        const string AES_IV = "";//16位    

        static string key = "";
        /// <summary>        
        /// AES加密算法  
        /// </summary>  
        /// <param name="input">明文字符串</param>  
        /// <param name="key">密钥(32位)</param>  
        /// <returns>字符串</returns>  
        public static string EncryptByAES(string input)
        {
            byte[] keyBytes = Encoding.UTF8.GetBytes(key.Substring(0, 32));
            using (AesCryptoServiceProvider aesAlg = new AesCryptoServiceProvider())
            {
                aesAlg。Key = keyBytes;
                aesAlg.IV = Encoding.UTF8.GetBytes(AES_IV.Substring(0, 16));

                ICryptoTransform encryptor = aesAlg。CreateEncryptor(aesAlg。Key, aesAlg。IV);
                using (MemoryStream msEncrypt = new MemoryStream())
                {
                    using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                    {
                        using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                        {
                            swEncrypt.Write(input);
                        }
                        byte[] bytes = msEncrypt。ToArray();
                        return ByteArrayToHexString(bytes);
                    }
                }
            }
        }

        /// <summary>  
        /// AES解密  
        /// </summary>  
        /// <param name="input">密文字节数组</param>  
        /// <param name="key">密钥(32位)</param>  
        /// <returns>返回解密后的字符串</returns>  
        public static string DecryptByAES(string input)
        {
            byte[] inputBytes = HexStringToByteArray(input);
            byte[] keyBytes = Encoding.UTF8.GetBytes(key.Substring(0, 32));
            using (AesCryptoServiceProvider aesAlg = new AesCryptoServiceProvider())
            {
                aesAlg.Key = keyBytes;
                aesAlg。IV = Encoding.UTF8.GetBytes(AES_IV.Substring(0, 16));

                ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
                using (MemoryStream msEncrypt = new MemoryStream(inputBytes))
                {
                    using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, decryptor, CryptoStreamMode。Read))
                    {
                        using (StreamReader srEncrypt = new StreamReader(csEncrypt))
                        {
                            return srEncrypt。ReadToEnd();
                        }
                    }
                }
            }
        }

        /// <summary>
        /// 将指定的16进制字符串转换为byte数组
        /// </summary>
        /// <param name="s">16进制字符串(如:“7F 2C 4A”或“7F2C4A”都可以)</param>
        /// <returns>16进制字符串对应的byte数组</returns>
        public static byte[] HexStringToByteArray(string s)
        {
            s = s.Replace(" ", "");
            byte[] buffer = new byte[s.Length / 2];
            for (int i = 0; i < s.Length; i += 2)
                buffer[i / 2] = (byte)Convert.ToByte(s.Substring(i, 2), 16);
            return buffer;
        }

        /// <summary>
        /// 将一个byte数组转换成一个格式化的16进制字符串
        /// </summary>
        /// <param name="data">byte数组</param>
        /// <returns>格式化的16进制字符串</returns>
        public static string ByteArrayToHexString(byte[] data)
        {
            StringBuilder sb = new StringBuilder(data.Length * 3);
            foreach (byte b in data)
            {
                //16进制数字
                sb.Append(Convert.ToString(b, 16).PadLeft(2, '0'));
                //16进制数字之间以空格隔开
                //sb.Append(Convert.ToString(b, 16).PadLeft(2, '0').PadRight(3, ' '));
            }
            return sb.ToString().ToUpper();
        }
    }
}
复制代码

 

加密

复制代码
  Endresult = CryptoJS.EncryptByAES(Endresult);//加密
  string callback = HttpContext.Current.Request["jsoncallback"];
   if (!string.IsNullOrEmpty(callback))
   {
       context.Response.Write(callback + "({result:'" + Endresult + "'})");
   }
   else
   {
      context.Response.Write(Endresult);
   }
复制代码

解密

复制代码
string strHead = "";string ReqMessageBody = "";if (strHead != "")
{
   if (ReqMessageBody != "")
   {
        ReqMessageBody = ReqMessageBody.Replace(" ", "+");
        ReqMessageBody = CryptoJS.DecryptByAES(ReqMessageBody);//解密
   }
   strHead = strHead.Replace(" ", "+");
   strHead = CryptoJS.DecryptByAES(strHead);//解密
  //解密之后就是一个字符串(你传入的参数是什么就是什么)
}
复制代码

 

如有疑问请咨询

 

在学习的过程中记录一些笔记及经验
相关教程
pk10开奖历史_3c*6kK7Z3 pk10开奖历史_M^MLBVWr5 pk10开奖历史pk10开奖历史_8=+^@h+4t pk10开奖历史_D~B7qlyhk pk10开奖历史_ZvWjGzqfR pk10开奖历史_duq2cPTeh pk10开奖历史_NFg!ipHD3 pk10开奖历史_?3WX8+leg pk10开奖历史pk10开奖历史_NgIZ&uCV~ pk10开奖历史_?QMO~AwMv