using System.Security.Cryptography;
public const int DerivationIterations = 600000;
public const int KeySize = 256;
public const int BlockSize = 128;
public static void Main()
string password = "This is my very secret password";
string plaintext = "Hello World";
Console.WriteLine(plaintext);
string encrypted = Encrypt(plaintext, password);
Console.WriteLine(encrypted);
string decrypted = Decrypt(encrypted, password);
Console.WriteLine(decrypted);
public static string Encrypt(string plainText, string passPhrase)
var saltStringBytes = GenerateBitsOfRandomEntropy(BlockSize);
var ivStringBytes = GenerateBitsOfRandomEntropy(BlockSize);
var plainTextBytes = Encoding.UTF8.GetBytes(plainText);
using (var password = new Rfc2898DeriveBytes(passPhrase, saltStringBytes, DerivationIterations, HashAlgorithmName.SHA256))
var keyBytes = password.GetBytes(KeySize / 8);
using (var symmetricKey = Aes.Create())
symmetricKey.BlockSize = BlockSize;
symmetricKey.Mode = CipherMode.CBC;
symmetricKey.Padding = PaddingMode.PKCS7;
using (var encryptor = symmetricKey.CreateEncryptor(keyBytes, ivStringBytes))
using (var memoryStream = new MemoryStream())
using (var cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
cryptoStream.FlushFinalBlock();
var cipherTextBytes = saltStringBytes;
cipherTextBytes = cipherTextBytes.Concat(ivStringBytes).ToArray();
cipherTextBytes = cipherTextBytes.Concat(memoryStream.ToArray()).ToArray();
return Convert.ToBase64String(cipherTextBytes);
public static string Decrypt(string cipherText, string passPhrase)
var cipherTextBytesWithSaltAndIv = Convert.FromBase64String(cipherText);
var saltStringBytes = cipherTextBytesWithSaltAndIv.Take(BlockSize / 8).ToArray();
var ivStringBytes = cipherTextBytesWithSaltAndIv.Skip(BlockSize / 8).Take(BlockSize / 8).ToArray();
var cipherTextBytes = cipherTextBytesWithSaltAndIv.Skip((BlockSize / 8) * 2).ToArray();
using (var password = new Rfc2898DeriveBytes(passPhrase, saltStringBytes, DerivationIterations, HashAlgorithmName.SHA256))
var keyBytes = password.GetBytes(KeySize / 8);
using (var symmetricKey = Aes.Create())
symmetricKey.BlockSize = BlockSize;
symmetricKey.Mode = CipherMode.CBC;
symmetricKey.Padding = PaddingMode.PKCS7;
using (var decryptor = symmetricKey.CreateDecryptor(keyBytes, ivStringBytes))
using (var memoryStream = new MemoryStream(cipherTextBytes))
using (var cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read))
var plainTextBytes = new byte[cipherTextBytes.Length];
var decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);
return Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount);
private static byte[] GenerateBitsOfRandomEntropy(int size)
var randomBytes = new byte[size / 8];
using (var rngCsp = RandomNumberGenerator.Create())
rngCsp.GetBytes(randomBytes);