Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
AES single block decryption/encryption
private static byte[] AES_Decrypt_block(byte[] cipherText, byte[] Key)
{
// Declare the string used to hold the decrypted text.
byte[] output_buffer = new byte[cipherText.Length];
using (AesManaged aesAlg = new AesManaged())
{
//If CBC, must initialize IV = O_{128}
//aesAlg.Mode = CipherMode.CBC;
//aesAlg.IV = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
aesAlg.Mode = CipherMode.ECB;
aesAlg.BlockSize = 128;
aesAlg.KeySize = 128;
aesAlg.Padding = PaddingMode.None;
aesAlg.Key = Key;
// Create a decrytor to perform the stream transform.
ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
decryptor.TransformBlock(cipherText, 0, cipherText.Length, output_buffer, 0);
}
return output_buffer;
}
private static byte[] AES_encrypt_block(byte[] plainText, byte[] Key)
{
byte[] output_buffer = new byte[plainText.Length];
using (AesManaged aesAlg = new AesManaged())
{
//If CBC, must initialize IV = O_{128}
//aesAlg.Mode = CipherMode.CBC;
//aesAlg.IV = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
aesAlg.Mode = CipherMode.ECB;
aesAlg.BlockSize = 128;
aesAlg.KeySize = 128;
aesAlg.Padding = PaddingMode.None;
aesAlg.Key = Key;
// Create a decrytor to perform the stream transform.
ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
encryptor.TransformBlock(plainText, 0, plainText.Length, output_buffer, 0);
}
return output_buffer;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment