Tuesday, February 24, 2015

AES (Rijndael) Encryption

In this post I want to explain how one can use the Rijndael encryption in C#. This is a symmetric block cipher, which was chosen after DES as encryption standard AES (Advanced Encryption Standard). A symmetric block cipher uses the same key for en- and decryption.
Different encryption algorithms are available in the class System.Security.Cryptography in .Net. The en- and decryption is then done via a CryptoStream, which en- or decrypts and which writes the result to a MemoryStream.
For the encryption we of course need a key. AES supports the key sizes 128, 192 and 256 Bit. The key is passed as a byte array, I will just take a string though and convert this via System.Text.Encoding.UTF8.GetBytes() to a byte array. This is somehow problematic, as UTF8 uses 1 - 4 bytes and no fixed size for coding a single character, depending on the character. Therefor, depending on the string the key might have an invalid size. There are better methods to create keys, but this should suffice here, since standard characters are coded with 1 byte.
Block ciphers further need an initialization vector (IV). This is due to the fact that the text is split in blocks of same size which are then encrypted separately. To make it harder to obtain information from this structure an IV is used to mask the first block and then subsequently randomize the encryption of the following ones based on this (see http://en.wikipedia.org/wiki/Block_cipher_mode_of_operation). AES supports also the block sizes 128, 192 and 256 Bit. The length of the IV has to be a block size divided by 8.
Let us take the smallest key size, 128 Bit. Suppose every character is coded by 1 Byte, then we need a string of length 16 as key. For the IV we also take a string of length 16.
Now the code, which should be relatively easy to understand in my opinion after the previous explanations:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using System.Security.Cryptography;
using System.IO;

namespace AES
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // legal key sizes are 128, 192 or 256 bit
            // legal block sizes are 128, 192 or 256 bit
            // IV has to be of length blocksize / 8

            string Key = "1234567812345678";
            string IV = "0000000000000000";

            byte[] Ciphertext = Encode("This is a secret.", Key, IV);
            string Plaintext = Decode(Ciphertext, Key, IV);

            textBox1.Text = System.Text.Encoding.UTF8.GetString(Ciphertext);
            textBox2.Text = Plaintext;
        }

        public string Decode(byte[] encryptedBytes, string key, string IV)
        {
            Rijndael AESCrypto = Rijndael.Create();
            AESCrypto.Key = System.Text.Encoding.UTF8.GetBytes(key);
            AESCrypto.IV = System.Text.Encoding.UTF8.GetBytes(IV);

            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms, AESCrypto.CreateDecryptor(), CryptoStreamMode.Write);

            cs.Write(encryptedBytes, 0, encryptedBytes.Length);
            cs.Close();

            byte[] DecryptedBytes = ms.ToArray();
            return System.Text.Encoding.UTF8.GetString(DecryptedBytes);
        }

        public byte[] Encode(string plaintext, string key, string IV)
        {
            Rijndael AESCrypto = Rijndael.Create();
            AESCrypto.Key = System.Text.Encoding.UTF8.GetBytes(key);
            AESCrypto.IV = System.Text.Encoding.UTF8.GetBytes(IV);

            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms, AESCrypto.CreateEncryptor(), CryptoStreamMode.Write);

            byte[] PlainBytes = System.Text.Encoding.UTF8.GetBytes(plaintext);
            cs.Write(PlainBytes, 0, PlainBytes.Length);
            cs.Close();

            byte[] EncryptedBytes = ms.ToArray();
            return EncryptedBytes;
        }

    }
}

One annotation: The function Encode() returns a byte array and no string. This is due to the fact, that the resulting bytes are displayed as special characters and thus result in different sizes when converting from and to string, creating a not readable string for the algorithm.

No comments:

Post a Comment