using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security.Cryptography;
namespace SDKCSharp.Utility
{
///
/// AES加解密.
/// 字符集:UTF-8
/// 算法模式:ECB
/// 补码方式:PKCS7Padding
/// 加密结果编码方式:Base64
///
public class AESUtil
{
private static Encoding ENCODING_UTF8 = Encoding.UTF8;
///
/// AES 加密
///
/// 明文(待加密)
/// 密文
/// 返回Base64字符串
public static string EncryptToBase64String(string data, string password)
{
if (string.IsNullOrEmpty(data)) return null;
Byte[] toEncryptArray = ENCODING_UTF8.GetBytes(data);
RijndaelManaged rm = new RijndaelManaged
{
Key = ENCODING_UTF8.GetBytes(password),
Mode = CipherMode.ECB,
Padding = PaddingMode.PKCS7
};
ICryptoTransform cTransform = rm.CreateEncryptor();
Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
return Convert.ToBase64String(resultArray, 0, resultArray.Length);
}
///
/// AES 解密
///
/// 密文(待解密)
/// 密码
/// 返回明文
public static string DecryptFromBase64String(string data, string password)
{
if (string.IsNullOrEmpty(data)) return null;
Byte[] toEncryptArray = Convert.FromBase64String(data);
RijndaelManaged rm = new RijndaelManaged
{
Key = ENCODING_UTF8.GetBytes(password),
Mode = CipherMode.ECB,
Padding = PaddingMode.PKCS7
};
ICryptoTransform cTransform = rm.CreateDecryptor();
Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
return ENCODING_UTF8.GetString(resultArray);
}
}
}