AESEncryption Aes 加密

/*******************************************************
 * 
 * 作者:朱皖苏
 * 创建日期:20180521
 * 说明:此文件只包含一个类,具体内容见类型注释。
 * 运行环境:.NET 4.0
 * 版本号:1.0.0
 * 
 * 历史记录:
 * 创建文件 朱皖苏 20180521 20:08
 * 
*******************************************************/

using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace Dben.CommonLib.Cryptography
{
    /// <summary>
    /// Aes 加密
    /// </summary>
    public class AESEncryption
    {
        /// <summary>
        /// AES加密
        /// </summary>
        /// <param name="encryptString">待加密字符串</param>
        /// <param name="key">16位密钥</param>
        /// <returns></returns>
        public static string EncryptAes(string encryptString, string key)
        {
            try
            {
                byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
                SymmetricAlgorithm des = Aes.Create();
                des.Key = Encoding.ASCII.GetBytes(key.Substring(0, 16));

                des.IV = Encoding.ASCII.GetBytes(key.Substring(16));
                des.Mode = CipherMode.CBC;
                des.Padding = PaddingMode.Zeros;

                using (MemoryStream mStream = new MemoryStream())
                {
                    using (CryptoStream cStream = new CryptoStream(mStream, des.CreateEncryptor(), CryptoStreamMode.Write))
                    {
                        cStream.Write(inputByteArray, 0, inputByteArray.Length);
                        cStream.FlushFinalBlock();

                        byte[] desBytes = mStream.ToArray();
                        StringBuilder sb = new StringBuilder();
                        for (int i = 0; i < desBytes.Length; i++)
                        {
                            sb.Append(desBytes[i].ToString("x2"));
                        }
                        return sb.ToString();
                    }
                }
            }
            catch (Exception)
            {
                return encryptString;
            }

        }

        /// <summary>
        /// AES解密 
        /// </summary>
        /// <param name="decryptString">解密字符串</param>
        /// <param name="key">密钥</param>
        /// <returns></returns>
        public static string DecryptAes(string decryptString, string key)
        {
            try
            {
                byte[] inputByteArray = StrToToHexByte(decryptString);
                SymmetricAlgorithm des = Aes.Create();
                des.Key = Encoding.ASCII.GetBytes(key.Substring(0, 16));

                des.IV = Encoding.ASCII.GetBytes(key.Substring(16));
                des.Padding = PaddingMode.Zeros;
                des.Mode = CipherMode.CBC;
                MemoryStream mStream = new MemoryStream();
                CryptoStream cStream = new CryptoStream(mStream, des.CreateDecryptor(), CryptoStreamMode.Write);
                cStream.Write(inputByteArray, 0, inputByteArray.Length);
                cStream.FlushFinalBlock();
                byte[] desDecryBytes = mStream.ToArray();
                return Encoding.UTF8.GetString(desDecryBytes);
            }
            catch (Exception)
            {
                return decryptString;
            }
        }

        /// <summary>
        /// 转16进制字符串
        /// </summary>
        /// <param name="hexString">待转换字符串</param>
        /// <returns></returns>
        private static byte[] StrToToHexByte(string hexString)
        {
            try
            {
                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;
            }
            catch (Exception)
            {
                return null;
            }
        }
    }
}

 

posted @ 2019-05-09 10:21  zwsu  阅读(728)  评论(0编辑  收藏  举报