AES ExampleAES (or Rijndael) is the new replacement for DES, and uses 128-bit blocks with 128, 192 and 256 bit encryption keys. It was selected by NIST in 2001 (after a five year standardisation process). The name Rijndael comes from its Belgium creators: Joan Daemen and Vincent Rijmen. The key has an IV and a key element, where the IV gives the overall key some variation. In this case the key is 256 bits, and the IV is 128 bits. [Lecture] [Tutorial] [Paper] Encrypted (Base-64) Encrypted (Hex) Decrypted |
Code Used
public void aes(string message, string inkey) { if (message == null) return; if (inkey == null) return; try { Rijndael myRijndael = new RijndaelManaged(); myRijndael.Key = StringToByte(inkey, 32); // convert to 32 characters - 256 bits myRijndael.IV = StringToByte("0123456789ABCDEF"); // 16 chars for IV byte[] key = myRijndael.Key; byte[] IV = myRijndael.IV; ICryptoTransform encryptor = myRijndael.CreateEncryptor(key, IV); MemoryStream msEncrypt = new MemoryStream(); CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write); // Write all data to the crypto stream and flush it. csEncrypt.Write(StringToByte(message), 0, StringToByte(message).Length); csEncrypt.FlushFinalBlock(); // Get the encrypted array of bytes. byte[] encrypted1 = msEncrypt.ToArray(); encrypted = ByteToString(encrypted1); ICryptoTransform decryptor = myRijndael.CreateDecryptor(key, IV); // Now decrypt the previously encrypted message using the decryptor MemoryStream msDecrypt = new MemoryStream(encrypted1); CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read); decrypted = ByteToString(csDecrypt); } catch (Exception ex) { encrypted = ex.Message.ToString(); } }