using System.Security.Cryptography;
private static byte[] key = new byte[] { 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0 };
private static byte[] iv = new byte[] { 0x02, 0x13, 0x24, 0x35, 0x46, 0x57, 0x68, 0x79, 0x8A, 0x9B, 0xAC, 0xBD, 0xCE, 0xDF, 0xE0, 0xF1 };
public static void Main()
string ciphertextEncB64 = "amDDjAsQJjEzw7nFvSpcIgHFksO/xb3CoF7Cv+KAsMKN4oCZNeKAulxaHwghwo1ExaEQKcOrGMO9Iw4Rw4sncsOLxb3Di8OKwqs0AgdFwo1CB8Oy4oCTPkrDlSbDkTDDtB3Cj2PDocW4AcKxM0bigJnDgsOsw6scw47DlQHCuEEnwqxZwqnDp8KdDBNzw7JKw70aw5/DtcK2FHzigJNJwq1kBsKyw57CpMOiCkPigJQnw5nCgVUcw5bCtl9j4oCcG8OGw5Yiw4zCv1bDrzhBMREIwr1yKMOTT8OMw6/CvVDDkQ8Bwq3CrnHCs8OMxZPDk3ogZMOHw5TigLrCoEXDn8OJZ3bDlVvCtcWSwqXCtsO2w4LCvMKldMKnNW5lD043YQ9PC1nDuMKQ4oC5CuKAuW8KHsO+w487MMO7aTjCp3kVw5rCsFp5PgTDkU3FuMOMw53FoAo9wrkoN1E5RiQdNUIhwo1ENeKAnsK9w5ladMKQcUdyxZPCqGTCvyjCtsKdwqHDn8W94oChT8Okw4zFocK7wq9bPeKAk8KdfMKBFW/DisOkw6dvcsKwwqLCnXgoxbjFoMOMwrFCw4EcQzUAwrfDoifDvMKoQ8Kdw7sEw7/DrmA1HsKsSMKl4oChT2/igKY3w65KxZMuxaDDqyA=";
byte[] ciphertextDecB64 = Convert.FromBase64String(ciphertextEncB64);
Console.WriteLine("Test 1: Plaintext: " + decrypt(ciphertextDecB64));
Console.WriteLine("Test 1: ERROR: " + ex.Message);
string plaintext = "The quick brown fox jumps over the lazy dog";
byte[] ciphertext = encrypt(plaintext);
string ciphertextDecCp437 = Encoding.GetEncoding("cp437").GetString(ciphertext);
byte[] ciphertextEncCp437 = Encoding.GetEncoding("cp437").GetBytes(ciphertextDecCp437);
Console.WriteLine("Test 2: Plaintext: " + decrypt(ciphertextEncCp437));
string ciphertextDecUtf8 = Encoding.GetEncoding("utf-8").GetString(ciphertext);
byte[] ciphertextEncUtf8 = Encoding.GetEncoding("utf-8").GetBytes(ciphertextDecUtf8);
Console.WriteLine("Test 3: Plaintext: " + decrypt(ciphertextEncUtf8));
Console.WriteLine("Test 3: ERROR: " + ex.Message);
private static byte[] encrypt(string body)
using (Aes aesAlg = Aes.Create())
ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
using (MemoryStream msEncrypt = new MemoryStream())
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
encrypted = msEncrypt.ToArray();
private static string decrypt(byte[] encrypted)
using (Aes aesAlg = Aes.Create())
original = DecryptStringFromBytes_Aes(encrypted, aesAlg.Key, aesAlg.IV);
static string DecryptStringFromBytes_Aes(byte[] cipherText, byte[] Key, byte[] IV)
if (cipherText == null || cipherText.Length <= 0)
throw new ArgumentNullException("cipherText");
if (Key == null || Key.Length <= 0)
throw new ArgumentNullException("Key");
if (IV == null || IV.Length <= 0)
throw new ArgumentNullException("IV");
using (Aes aesAlg = Aes.Create())
ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
using (MemoryStream msDecrypt = new MemoryStream(cipherText))
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
using (StreamReader srDecrypt = new StreamReader(csDecrypt))
plaintext = srDecrypt.ReadToEnd();