Archive for September 2012

Single-Block AES Encryption/Decryption in .NET

While trying to solve the 2nd programming assignment from the excellent crypto class on coursera, it was required to implement the CBC and CTR modes of operation starting from a simple AES block encryption/decryption.

To get this done using .NET's Cryptography.AesManaged or Cryptography.RijndaelManaged, the example on the MSDN documentation was not enough to have a one block encryption. Indeed, the example codes show how to encrypt/decrypt complete clear/cipher texts; this implies applying the chaining in the CBC mode and the underlying padding.

After some tinkering, I used the TransformBlock method of the ICryptoTransform interface. This method does exactly one block transformation of the underlying block cipher (AES in our case), there is no explicit padding involved nor chaining in the case of CBC.

To get a consistent AES block encryption/decryption, it was mandatory to eliminate the XOR with the Initial Vector (IV). This can be achieved whether by using the ECB mode or using the CBC mode with an initial IV = 0^{128}.

The following two methods show the encryption/decryption of a single block using AES. Notice that it is considered cheating if you use the built-in encryption to find the solution to the coursera problem.



Swedish Greys - a WordPress theme from Nordic Themepark. Converted by LiteThemes.com.