C#版本工具包【AES加密与解密】
public class AESUtil
{
public static string DEFAULT_CHARSET = "UTF-8";
public static string encrypt(string content, string password)
{
if (string.IsNullOrEmpty(content)) return null;
Byte[] toEncryptArray = Encoding.UTF8.GetBytes(content);
string secretMd5 = MD5Util.strToMd5(password, DEFAULT_CHARSET);
password = MD5Util.strToMd5(password + secretMd5, DEFAULT_CHARSET);
System.Security.Cryptography.RijndaelManaged rm = new System.Security.Cryptography.RijndaelManaged
{
Key = Encoding.UTF8.GetBytes(password),
Mode = System.Security.Cryptography.CipherMode.ECB,
Padding = System.Security.Cryptography.PaddingMode.PKCS7
};
System.Security.Cryptography.ICryptoTransform cTransform = rm.CreateEncryptor();
Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
return Convert.ToBase64String(resultArray, 0, resultArray.Length);
}
public static string decrypt(string content, string password)
{
if (string.IsNullOrEmpty(content)) return null;
Byte[] toEncryptArray = Convert.FromBase64String(content);
string secretMd5 = MD5Util.strToMd5(password, DEFAULT_CHARSET);
password = MD5Util.strToMd5(password + secretMd5, DEFAULT_CHARSET);
System.Security.Cryptography.RijndaelManaged rm = new System.Security.Cryptography.RijndaelManaged
{
Key = Encoding.UTF8.GetBytes(password),
Mode = System.Security.Cryptography.CipherMode.ECB,
Padding = System.Security.Cryptography.PaddingMode.Zeros,
BlockSize = 128
};
System.Security.Cryptography.ICryptoTransform cTransform = rm.CreateDecryptor();
Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
return Encoding.UTF8.GetString(resultArray);
}
}