标签:static ecb lex created coding ice bsp 加密 rem
#region aes实现加密解密
/// <summary>
/// 获取Aes32位密钥
/// </summary>
/// <param name="key">Aes密钥字符串</param>
/// <returns>Aes32位密钥</returns>
static byte[] GetAesKey(string key)
{
if (string.IsNullOrEmpty(key))
{
throw new ArgumentNullException("key", "Aes密钥不能为空");
}
if (key.Length < 32)
{
// 不足32补全
key = key.PadRight(32, ‘0‘);
}
if (key.Length > 32)
{
key = key.Substring(0, 32);
}
return Encoding.UTF8.GetBytes(key);
}
/// <summary>
/// Aes加密
/// </summary>
/// <param name="source">源字符串</param>
/// <param name="key">aes密钥,长度必须32位</param>
/// <returns>加密后的字符串</returns>
public string EncryptAes(string source, string key)
{
using (AesCryptoServiceProvider aesProvider = new AesCryptoServiceProvider())
{
//获取32位密钥
aesProvider.Key = GetAesKey(key);
//密码模式
aesProvider.Mode = CipherMode.ECB;
aesProvider.Padding = PaddingMode.PKCS7;
using (ICryptoTransform cryptoTransform = aesProvider.CreateEncryptor())
{
byte[] inputBuffers = Encoding.UTF8.GetBytes(source);
byte[] results = cryptoTransform.TransformFinalBlock(inputBuffers, 0, inputBuffers.Length);
aesProvider.Clear();
aesProvider.Dispose();
return Convert.ToBase64String(results, 0, results.Length);
}
}
}
/// <summary>
/// Aes解密
/// </summary>
/// <param name="source">源字符串</param>
/// <param name="key">aes密钥,长度必须32位</param>
/// <returns>解密后的字符串</returns>
public string DecryptAes(string source, string key)
{
using (AesCryptoServiceProvider aesProvider = new AesCryptoServiceProvider())
{
aesProvider.Key = GetAesKey(key);
aesProvider.Mode = CipherMode.ECB;
aesProvider.Padding = PaddingMode.PKCS7;
using (ICryptoTransform cryptoTransform = aesProvider.CreateDecryptor())
{
byte[] inputBuffers = Convert.FromBase64String(source);
byte[] results = cryptoTransform.TransformFinalBlock(inputBuffers, 0, inputBuffers.Length);
aesProvider.Clear();
return Encoding.UTF8.GetString(results);
}
}
}
#endregion
标签:static ecb lex created coding ice bsp 加密 rem
原文地址:http://www.cnblogs.com/mxx328/p/7714872.html