AESEncryption Aes 加密
阅读原文时间:2023年07月15日阅读:1

/*******************************************************
*
* 作者:朱皖苏
* 创建日期: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
{
///

/// Aes 加密 ///
public class AESEncryption
{
/// /// AES加密 ///
/// 待加密字符串
/// 16位密钥
///
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(, ));

            des.IV = Encoding.ASCII.GetBytes(key.Substring());  
            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, , inputByteArray.Length);  
                    cStream.FlushFinalBlock();

                    byte\[\] desBytes = mStream.ToArray();  
                    StringBuilder sb = new StringBuilder();  
                    for (int i = ; 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(, ));

            des.IV = Encoding.ASCII.GetBytes(key.Substring());  
            des.Padding = PaddingMode.Zeros;  
            des.Mode = CipherMode.CBC;  
            MemoryStream mStream = new MemoryStream();  
            CryptoStream cStream = new CryptoStream(mStream, des.CreateDecryptor(), CryptoStreamMode.Write);  
            cStream.Write(inputByteArray, , 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 % ) != )  
                hexString += " ";  
            byte\[\] returnBytes = new byte\[hexString.Length / \];  
            for (int i = ; i < returnBytes.Length; i++)  
                returnBytes\[i\] = Convert.ToByte(hexString.Substring(i \* , ), );  
            return returnBytes;  
        }  
        catch (Exception)  
        {  
            return null;  
        }  
    }  
}  

}

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器