C# AES 加密与解密

AES 算法加密(ECB模式) 将明文加密,加密后进行base64编码,返回密文

        /// <summary>
        /// AES 算法加密(ECB模式) 将明文加密,加密后进行base64编码,返回密文
        /// </summary>
        /// <param name="EncryptStr">明文</param>
        /// <param name="Key">密钥</param>
        /// <returns>加密后base64编码的密文</returns>
        public static string AesEncryptor_Base64(string EncryptStr, string Key)
        {
            try
            {
                //byte[] keyArray = Encoding.UTF8.GetBytes(Key);
                byte[] keyArray = Convert.FromBase64String(Key);
                byte[] toEncryptArray = Encoding.UTF8.GetBytes(EncryptStr);

                RijndaelManaged rDel = new RijndaelManaged();
                rDel.Key = keyArray;
                rDel.Mode = CipherMode.ECB;
                rDel.Padding = PaddingMode.PKCS7;

                ICryptoTransform cTransform = rDel.CreateEncryptor();
                byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

                return Convert.ToBase64String(resultArray, 0, resultArray.Length);
            }
            catch (Exception ex)
            {
                return null;
            }
        }

AES 算法解密(ECB模式) 将密文base64解码进行解密,返回明文

        /// <summary>
        /// AES 算法解密(ECB模式) 将密文base64解码进行解密,返回明文
        /// </summary>
        /// <param name="DecryptStr">密文</param>
        /// <param name="Key">密钥</param>
        /// <returns>明文</returns>
        public static string AesDecryptor_Base64(string DecryptStr, string Key)
        {
            try
            {
                //byte[] keyArray = Encoding.UTF8.GetBytes(Key);
                byte[] keyArray = Convert.FromBase64String(Key);
                byte[] toEncryptArray = Convert.FromBase64String(DecryptStr);

                RijndaelManaged rDel = new RijndaelManaged();
                rDel.Key = keyArray;
                rDel.Mode = CipherMode.ECB;
                rDel.Padding = PaddingMode.PKCS7;

                ICryptoTransform cTransform = rDel.CreateDecryptor();
                byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

                return Encoding.UTF8.GetString(resultArray);//  UTF8Encoding.UTF8.GetString(resultArray);
            }
            catch (Exception ex)
            {
                return null;
            }
        }

AES 算法加密(ECB模式) 将明文加密,加密后进行Hex编码,返回密文

        /// <summary>
        ///AES 算法加密(ECB模式) 将明文加密,加密后进行Hex编码,返回密文
        /// </summary>
        /// <param name="str">明文</param>
        /// <param name="key">密钥</param>
        /// <returns>加密后Hex编码的密文</returns>
        public static string AesEncryptor_Hex(string str, string key)
        {
            if (string.IsNullOrEmpty(str)) return null;
            Byte[] toEncryptArray = Encoding.UTF8.GetBytes(str);

            System.Security.Cryptography.RijndaelManaged rm = new System.Security.Cryptography.RijndaelManaged
            {
                Key = StrToHexByte(key),
                Mode = System.Security.Cryptography.CipherMode.ECB,
                Padding = System.Security.Cryptography.PaddingMode.PKCS7
            };

            System.Security.Cryptography.ICryptoTransform cTransform = rm.CreateEncryptor();
            Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

            return ToHexString(resultArray);
        }

AES 算法解密(ECB模式) 将密文Hex解码后进行解密,返回明文

        /// <summary>
        ///AES 算法解密(ECB模式) 将密文Hex解码后进行解密,返回明文
        /// </summary>
        /// <param name="str">密文</param>
        /// <param name="key">密钥</param>
        /// <returns>明文</returns>
        public static string AesDecryptor_Hex(string str, string key)
        {
            if (string.IsNullOrEmpty(str)) return null;
            Byte[] toEncryptArray = StrToHexByte(str);

            System.Security.Cryptography.RijndaelManaged rm = new System.Security.Cryptography.RijndaelManaged
            {
                Key = StrToHexByte(key),
                Mode = System.Security.Cryptography.CipherMode.ECB,
                Padding = System.Security.Cryptography.PaddingMode.PKCS7
            };

            System.Security.Cryptography.ICryptoTransform cTransform = rm.CreateDecryptor();
            Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

            return Encoding.UTF8.GetString(resultArray);
        }

byte数组Hex编码与字符串进行Hex解码(Hex.decodeHex())

        /// <summary>
        /// byte数组Hex编码
        /// </summary>
        /// <param name="bytes">需要进行编码的byte[]</param>
        /// <returns></returns>
        public static string ToHexString(byte[] bytes) // 0xae00cf => "AE00CF "
        {
            string hexString = string.Empty;
            if (bytes != null)
            {
                StringBuilder strB = new StringBuilder();
                for (int i = 0; i < bytes.Length; i++)
                {
                    strB.Append(bytes[i].ToString("X2"));
                }
                hexString = strB.ToString();
            }
            return hexString;
        }
        /// <summary> 
        /// 字符串进行Hex解码(Hex.decodeHex())
        /// </summary> 
        /// <param name="hexString">需要进行解码的字符串</param> 
        /// <returns></returns> 
        public static byte[] StrToHexByte(string hexString)
        {
            hexString = hexString.Replace(" ", "");
            if ((hexString.Length % 2) != 0)
                hexString += " ";
            byte[] returnBytes = new byte[hexString.Length / 2];
            for (int i = 0; i < returnBytes.Length; i++)
                returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
            return returnBytes;
        }

AES加密、解密封装

        /// <summary>
        /// 对象转成Json并加密
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public string DataAes(object str)
        {
            string json_str = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(str);
            string miwen = AESCryptos.AesEncryptor_Base64(json_str, key);
            return miwen;
        }

        /// <summary>
        /// 解密并Json解析成对象
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public T JsonToModel<T>(string str)
        {
            string mingwen = AESCryptos.AesDecryptor_Base64(str, key);
            System.Web.Script.Serialization.JavaScriptSerializer jss = new System.Web.Script.Serialization.JavaScriptSerializer();
            T Model = jss.Deserialize<T>(mingwen);
            return Model;
        }

        /// <summary>
        /// 解密并Json解析成list
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public IList<T> JsonToList<T>(string str)
        {
            string mingwen = AESCryptos.AesDecryptor_Base64(str, key);
            System.Web.Script.Serialization.JavaScriptSerializer jss = new System.Web.Script.Serialization.JavaScriptSerializer();
            IList<T> list = jss.Deserialize<List<T>>(mingwen);
            return list;
        }

 

Base64加密

string str= "汉字";
str= Convert.ToBase64String(UnicodeEncoding.UTF8.GetBytes(str));

Base64解密

string str= HttpUtility.UrlDecode(Request["str"], System.Text.Encoding.UTF8).Replace(" ", "+");
str= UnicodeEncoding.UTF8.GetString(Convert.FromBase64String(str));

 

posted @ 2017-04-10 14:30    阅读(20451)  评论(0编辑  收藏  举报