AES Encryption In C#

.NET provides high-level level classes for various encryption algorithms, both symmetric and asymmetric. Advanced Encryption Standard (AES) is one of the symmetric encryption algorithms that allows both parties, sender, and receiver, to use the same key to encrypt and decrypt data.

AES was developed by two Belgian cryptographers, Vincent Rijmen and Jan Daemen. In 2001, AES was selected as a standard for encryption by the U. S. National Institute of Standards and Technology (NIST). AES supports 128, 192, and 256 bits key sizes and 128 bits sizes.

AesManaged class is a managed implementation of the AES algorithm. This article demonstrates the use AesManaged class to apply an AES algorithm to encrypt and decrypt data in .NET and C#.

The following steps are required to encrypt data using AesManaged.

Step 1

Create AesManaged,

AesManaged aes = new AesManaged();  

Step 2

Create Encryptor,

ICryptoTransform encryptor = aes.CreateEncryptor(Key, IV);  

Step 3

Create MemoryStream,

MemoryStream ms = new MemoryStream();  

Step 4

Create CryptoStream from MemoryStream and Encrypter and write it.

using(CryptoStream cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write))   
    // Create StreamWriter and write data to a stream    
    using(StreamWriter sw = new StreamWriter(cs))  
    encrypted = ms.ToArray();  

The complete code is listed in Listing 1. To test the code, create a .NET Core project in Visual Studio and copy and paste the code. 

using System;  
using System.IO;  
using System.Security.Cryptography;  
class ManagedAesSample {  
    public static void Main() {  
        Console.WriteLine("Enter text that needs to be encrypted..");  
        string data = Console.ReadLine();  
    static void EncryptAesManaged(string raw) {  
        try {  
            // Create Aes that generates a new key and initialization vector (IV).    
            // Same key must be used in encryption and decryption    
            using(AesManaged aes = new AesManaged()) {  
                // Encrypt string    
                byte[] encrypted = Encrypt(raw, aes.Key, aes.IV);  
                // Print encrypted string    
                Console.WriteLine($ "Encrypted data: {System.Text.Encoding.UTF8.GetString(encrypted)}");  
                //decrypt the bytes to a string.    
                string decrypted = Decrypt(encrypted, aes.Key, aes.IV);  
                // Print decrypted string. It should be same as raw data    
                Console.WriteLine($ "Decrypted data: {decrypted}");  
        } catch (Exception exp) {  
    static byte[] Encrypt(string plainText, byte[] Key, byte[] IV) {  
        byte[] encrypted;  
        // Create a new AesManaged.    
        using(AesManaged aes = new AesManaged()) {  
            // Create encryptor    
            ICryptoTransform encryptor = aes.CreateEncryptor(Key, IV);  
            // Create MemoryStream    
            using(MemoryStream ms = new MemoryStream()) {  
                // Create crypto stream using the CryptoStream class. This class is the key to encryption    
                // and encrypts and decrypts data from any given stream. In this case, we will pass a memory stream    
                // to encrypt    
                using(CryptoStream cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write)) {  
                    // Create StreamWriter and write data to a stream    
                    using(StreamWriter sw = new StreamWriter(cs))  
                    encrypted = ms.ToArray();  
        // Return encrypted data    
        return encrypted;  
    static string Decrypt(byte[] cipherText, byte[] Key, byte[] IV) {  
        string plaintext = null;  
        // Create AesManaged    
        using(AesManaged aes = new AesManaged()) {  
            // Create a decryptor    
            ICryptoTransform decryptor = aes.CreateDecryptor(Key, IV);  
            // Create the streams used for decryption.    
            using(MemoryStream ms = new MemoryStream(cipherText)) {  
                // Create crypto stream    
                using(CryptoStream cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read)) {  
                    // Read crypto stream    
                    using(StreamReader reader = new StreamReader(cs))  
                    plaintext = reader.ReadToEnd();  
        return plaintext;  

Listing 1.

The output looks like the following, where you can type any text that will be encrypted and decrypted.

AES Encryption 



Similar Articles
Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.