using System.Security.Cryptography;
public static class Program
static RijndaelManaged aes = new RijndaelManaged(){
Padding = PaddingMode.None
public static void Main(){
byte[] key = Encoding.UTF8.GetBytes("0123456789abcdef");
byte[] iv = Encoding.UTF8.GetBytes("1234567890abcdef");
byte[] encryptedBytes = new byte[]{0x76, 0x2b, 0x6d, 0xce, 0xa9, 0xc2, 0xa7, 0x46, 0x0d, 0xb7};
encryptedBytes = PadBytes(encryptedBytes, aes.BlockSize, out padded);
byte[] decryptedBytes = DecryptBytesAES(encryptedBytes, key, iv, encryptedBytes.Length);
if(decryptedBytes != null){
decryptedBytes = UnpadBytes(decryptedBytes, padded);
Console.Write("Decrypted: " + Encoding.UTF8.GetString(decryptedBytes));
public static byte[] Initialize(this byte[] array, byte value, int length)
for (int i = 0; i < array.Length; i++)
public static byte[] PadBytes(byte[] encryptedBytes, int blockSize, out int numPadded)
int mod = encryptedBytes.Length % blockSize;
numPadded = blockSize - mod;
return encryptedBytes.Concat(new byte[numPadded].Initialize(0, numPadded)).ToArray();
public static byte[] UnpadBytes(byte[] decryptedBytes, int numPadded)
byte[] unpaddedBytes = new byte[decryptedBytes.Length - numPadded];
Array.Copy(decryptedBytes, unpaddedBytes, unpaddedBytes.Length);
public static byte[] DecryptBytesAES(byte[] cipherText, byte[] Key, byte[] IV, int size)
byte[] array = new byte[size];
ICryptoTransform transform = aes.CreateDecryptor(aes.Key, aes.IV);
using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(cipherText))
using (CryptoStream cryptoStream = new CryptoStream(memoryStream, transform, CryptoStreamMode.Read))
cryptoStream.Read(array, 0, size);