/// <summary>
/// AES加密
/// </summary>
public class AES
{
/// <summary>
/// 加密
/// </summary>
/// <param name="toEncrypt"></param>
/// <param name="key"></param>
/// <returns></returns>
public static string Encrypt(string toEncrypt, string key)
{
byte[] keyArray = Encoding.UTF8.GetBytes(key);
byte[] ivArray = keyArray;
byte[] toEncryptArray = Encoding.UTF8.GetBytes(toEncrypt); try
{
using (var rDel = new RijndaelManaged
{
Key = keyArray,
IV = ivArray,
Mode = CipherMode.CBC,
Padding = PaddingMode.Zeros
})
{
using (var cTransform = rDel.CreateEncryptor())
{
byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, , toEncryptArray.Length);
return Convert.ToBase64String(resultArray, , resultArray.Length);
}
}
}
catch (Exception ex)
{
//log4net.LogManager.GetLogger(typeof(AES).FullName).Error(ex);
} return null;
} /// <summary>
/// 解密
/// </summary>
/// <param name="toDecrypt"></param>
/// <param name="key"></param>
/// <returns></returns>
public static string Decrypt(string toDecrypt, string key)
{
byte[] keyArray = Encoding.UTF8.GetBytes(key);
byte[] ivArray = keyArray;
byte[] toEncryptArray = Convert.FromBase64String(toDecrypt); try
{
using (var rDel = new RijndaelManaged())
{
rDel.Key = keyArray;
rDel.IV = ivArray;
rDel.Mode = CipherMode.CBC;
rDel.Padding = PaddingMode.Zeros;
using (var cTransform = rDel.CreateDecryptor())
{
byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, , toEncryptArray.Length);
return Encoding.UTF8.GetString(resultArray);
}
}
}
catch (Exception ex)
{
//log4net.LogManager.GetLogger(typeof(AES).FullName).Error(ex);
} return null; } /* <script src="aes.js"></script>
<script src="md5.js"></script>
<script src="pad-zeropadding-min.js"></script> var data = "eVIcVEAnPZtVchn1Md85QQ==";
var key = CryptoJS.enc.Latin1.parse('1111111111111111');
var iv = key;
* 解密
var encrypted = CryptoJS.AES.decrypt(data, key, { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.ZeroPadding });
document.write(encrypted.toString(CryptoJS.enc.Utf8));
* 加密
var encrypted = CryptoJS.AES.encrypt(data, key, { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.ZeroPadding });
document.write(encrypted);
*/
}