using System;
using System.Text;
using System.Security.Cryptography;
namespace Alipay.EasySDK.Kernel.Util
{
public class AES
{
///
/// 128位全0初始向量
///
private static readonly byte[] AES_IV = InitIV(16);
///
/// AES加密
///
/// 明文
/// 对称密钥
/// 密文
public static string Encrypt(string plainText, string key)
{
try
{
byte[] keyBytes = Convert.FromBase64String(key);
byte[] plainBytes = AlipayConstants.DEFAULT_CHARSET.GetBytes(plainText); ;
RijndaelManaged rijndatel = new RijndaelManaged
{
Key = keyBytes,
Mode = CipherMode.CBC,
Padding = PaddingMode.PKCS7,
IV = AES_IV
};
ICryptoTransform transform = rijndatel.CreateEncryptor(rijndatel.Key, rijndatel.IV);
byte[] cipherBytes = transform.TransformFinalBlock(plainBytes, 0, plainBytes.Length);
return Convert.ToBase64String(cipherBytes);
}
catch (Exception e)
{
throw new Exception("AES加密失败,plainText=" + plainText +
",keySize=" + key.Length + "。" + e.Message, e);
}
}
///
/// AES解密
///
/// 密文
/// 对称密钥
/// 明文
public static string Decrypt(string cipherText, string key)
{
try
{
byte[] keyBytes = Convert.FromBase64String(key);
byte[] cipherBytes = Convert.FromBase64String(cipherText);
RijndaelManaged rijndatel = new RijndaelManaged
{
Key = keyBytes,
Mode = CipherMode.CBC,
Padding = PaddingMode.PKCS7,
IV = AES_IV
};
ICryptoTransform transform = rijndatel.CreateDecryptor(rijndatel.Key, rijndatel.IV);
byte[] plainBytes = transform.TransformFinalBlock(cipherBytes, 0, cipherBytes.Length);
return AlipayConstants.DEFAULT_CHARSET.GetString(plainBytes);
}
catch (Exception e)
{
throw new Exception("AES解密失败,ciphertext=" + cipherText +
",keySize=" + key.Length + "。" + e.Message, e);
}
}
private static byte[] InitIV(int blockSize)
{
byte[] iv = new byte[blockSize];
for (int i = 0; i < blockSize; ++i)
{
iv[i] = 0x0;
}
return iv;
}
}
}