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

AES加解密

程序员文章站 2023-11-04 12:05:34
一个程序最忌讳的就是把自己的数据暴露给其他人,一个加解密可以让你感觉安全性好一些 直接上代码 一个加密类 加密 解密 如有疑问请咨询 ......

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

直接上代码

 

一个加密类

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);//解密
  //解密之后就是一个字符串(你传入的参数是什么就是什么)
}

 

如有疑问请咨询