using System.Security.Cryptography;
private static int _iterations = 2;
private static int _keySize = 256;
private static string _hash = "SHA1";
private static string _salt = "aselrias38490a32";
private static string _vector = "8947az34awl34kjq";
static void Main(string[] args)
string ciphertext = Encrypt("The quick brown fox jumps over the lazy dog", "0123456789012345");
string decrypted = Decrypt(ciphertext, "0123456789012345");
string decryptedFixed = DecryptFixed<AesManaged>(ciphertext, "0123456789012345");
Console.WriteLine("Ciphertext: " + ciphertext);
Console.WriteLine("Decrypted (original): " + decrypted);
Console.WriteLine("Decyrpted (fixed): " + decryptedFixed);
public static string Encrypt(string value, string AesKey)
return Encrypt<AesManaged>(value, AesKey);
private static string Encrypt<RijndaelManaged>(string value, string password)
where RijndaelManaged : SymmetricAlgorithm, new()
byte[] vectorBytes = Encoding.UTF8.GetBytes(_vector);
byte[] saltBytes = Encoding.UTF8.GetBytes(_salt);
byte[] valueBytes = Encoding.UTF8.GetBytes(value);
using (RijndaelManaged cipher = new RijndaelManaged())
PasswordDeriveBytes _passwordBytes =
new PasswordDeriveBytes(password, saltBytes, _hash, _iterations);
byte[] keyBytes = _passwordBytes.GetBytes(_keySize / 8);
cipher.Mode = CipherMode.CBC;
using (ICryptoTransform encryptor = cipher.CreateEncryptor(keyBytes, vectorBytes))
using (MemoryStream to = new MemoryStream())
using (CryptoStream writer = new CryptoStream(to, encryptor, CryptoStreamMode.Write))
writer.Write(valueBytes, 0, valueBytes.Length);
writer.FlushFinalBlock();
encrypted = to.ToArray();
return Convert.ToBase64String(encrypted);
public static string Decrypt(string value, string AesKey)
return Decrypt<AesManaged>(value, AesKey);
private static string Decrypt<T>(string value, string password) where T : SymmetricAlgorithm, new()
byte[] vectorBytes = Encoding.UTF8.GetBytes(_vector);
byte[] saltBytes = Encoding.UTF8.GetBytes(_salt);
byte[] valueBytes = Convert.FromBase64String(value);
using (T cipher = new T())
PasswordDeriveBytes _passwordBytes = new PasswordDeriveBytes(password, saltBytes, _hash, _iterations);
byte[] keyBytes = _passwordBytes.GetBytes(_keySize / 8);
cipher.Mode = CipherMode.CBC;
using (ICryptoTransform decryptor = cipher.CreateDecryptor(keyBytes, vectorBytes))
using (MemoryStream from = new MemoryStream(valueBytes))
using (CryptoStream reader = new CryptoStream(from, decryptor, CryptoStreamMode.Read))
buffer = new byte[valueBytes.Length];
totalRead = reader.Read(buffer, 0, buffer.Length);
return Encoding.UTF8.GetString(buffer, 0, totalRead);
private static string DecryptFixed<T>(string value, string password) where T : SymmetricAlgorithm, new()
byte[] vectorBytes = Encoding.UTF8.GetBytes(_vector);
byte[] saltBytes = Encoding.UTF8.GetBytes(_salt);
byte[] valueBytes = Convert.FromBase64String(value);
string decrypted = string.Empty;
using (T cipher = new T())
PasswordDeriveBytes _passwordBytes = new PasswordDeriveBytes(password, saltBytes, _hash, _iterations);
byte[] keyBytes = _passwordBytes.GetBytes(_keySize / 8);
cipher.Mode = CipherMode.CBC;
using (ICryptoTransform decryptor = cipher.CreateDecryptor(keyBytes, vectorBytes))
using (MemoryStream from = new MemoryStream(valueBytes))
using (CryptoStream reader = new CryptoStream(from, decryptor, CryptoStreamMode.Read))
using (var plainTextReader = new StreamReader(reader))
decrypted = plainTextReader.ReadToEnd();