private const string PASSWORD = "P1e@s3Chang3thi2";
private const string INITVECTOR = "!n1t1@liz3Vector";
public static string AesEncrypt(this string value)
{
return AesEncrypt(value, PASSWORD);
}
public static string AesEncrypt(this string value, string password)
{
using (Aes aes = Aes.Create())
{
var encryptor = aes.CreateEncryptor(Encoding.ASCII.GetBytes(password), Encoding.ASCII.GetBytes(INITVECTOR));
using (MemoryStream msEncrypt = new MemoryStream())
{
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
{
using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
{
swEncrypt.Write(value);
}
return Convert.ToBase64String(msEncrypt.ToArray());
}
}
}
}
public static string AesDecrypt(this string value)
{
return AesDecrypt(value, PASSWORD);
}
public static string AesDecrypt(this string value, string password)
{
using (Aes aes = Aes.Create())
{
var decryptor = aes.CreateDecryptor(Encoding.ASCII.GetBytes(password), System.Text.Encoding.ASCII.GetBytes(INITVECTOR));
using (MemoryStream msDecrypt = new MemoryStream(Convert.FromBase64String(value)))
{
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{
using (StreamReader srDecrypt = new StreamReader(csDecrypt))
{
return srDecrypt.ReadToEnd();
}
}
}
}
}