Encryption and Decryption of AES by C Code


ES (The Advanced Encryption Standard) is the National Institute of Standards and Technology specification for encrypting electronic data. It is expected to become a recognized method of encrypting digital information including finance, telecommunications and government.

Examples of this article for everyone to introduce C # implementation of AES encryption and decryption of the detailed code, to share for your reference, the specific content is as follows

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;


namespace AESDemo
{
 public static class AESHelper
 {
  /// <summary>
  /// AES Encryption
  /// </summary>
  /// <param name="Data"> Encrypted plaintext </param>
  /// <param name="Key"> Key </param>
  /// <param name="Vector"> Vector </param>
  /// <returns> Ciphertext </returns>
  public static Byte[] AESEncrypt(Byte[] Data, String Key, String Vector)
  {
   Byte[] bKey = new Byte[32];
   Array.Copy(Encoding.UTF8.GetBytes(Key.PadRight(bKey.Length)), bKey, bKey.Length);
   Byte[] bVector = new Byte[16];
   Array.Copy(Encoding.UTF8.GetBytes(Vector.PadRight(bVector.Length)), bVector, bVector.Length);


   Byte[] Cryptograph = null; //  Encrypted ciphertext


   Rijndael Aes = Rijndael.Create();
   try
   {
    //  Open up 1 Block memory flow
    using (MemoryStream Memory = new MemoryStream())
    {
     //  Wrapping memory stream objects as encrypted stream objects
     using (CryptoStream Encryptor = new CryptoStream(Memory,
      Aes.CreateEncryptor(bKey, bVector),
      CryptoStreamMode.Write))
     {
      //  Writing plaintext data to encrypted stream
      Encryptor.Write(Data, 0, Data.Length);
      Encryptor.FlushFinalBlock();


      Cryptograph = Memory.ToArray();
     }
    }
   }
   catch
   {
    Cryptograph = null;
   }


   return Cryptograph;
  }


  /// <summary>
  /// AES Decryption
  /// </summary>
  /// <param name="Data"> Decrypted ciphertext </param>
  /// <param name="Key"> Key </param>
  /// <param name="Vector"> Vector </param>
  /// <returns> Cleartext </returns>
  public static Byte[] AESDecrypt(Byte[] Data, String Key, String Vector)
  {
   Byte[] bKey = new Byte[32];
   Array.Copy(Encoding.UTF8.GetBytes(Key.PadRight(bKey.Length)), bKey, bKey.Length);
   Byte[] bVector = new Byte[16];
   Array.Copy(Encoding.UTF8.GetBytes(Vector.PadRight(bVector.Length)), bVector, bVector.Length);


   Byte[] original = null; //  Decrypted plaintext


   Rijndael Aes = Rijndael.Create();
   try
   {
    //  Open up 1 Block memory stream, storing ciphertext
    using (MemoryStream Memory = new MemoryStream(Data))
    {
     //  Wrapping memory stream objects as encrypted stream objects
     using (CryptoStream Decryptor = new CryptoStream(Memory,
     Aes.CreateDecryptor(bKey, bVector),
     CryptoStreamMode.Read))
     {
      //  Cleartext store
      using (MemoryStream originalMemory = new MemoryStream())
      {
       Byte[] Buffer = new Byte[1024];
       Int32 readBytes = 0;
       while ((readBytes = Decryptor.Read(Buffer, 0, Buffer.Length)) > 0)
       {
        originalMemory.Write(Buffer, 0, readBytes);
       }


       original = originalMemory.ToArray();
      }
     }
    }
   }
   catch
   {
    original = null;
   }


   return original;
  }
 }
}


 In a way that does not use vectors:
public static class AESCrypto
 {
/// <summary>
/// IV Vector is a fixed value
/// </summary>
  //private static byte[] _iV = {
  // 85, 60, 12, 116,
  // 99, 189, 173, 19,
  // 138, 183, 232, 248,
  // 82, 232, 200, 242
  //};


  public static byte[] Decrypt(byte[] encryptedBytes, byte[] key)
  {
MemoryStream mStream = new MemoryStream( encryptedBytes );
//mStream.Write( encryptedBytes, 0, encryptedBytes.Length );
//mStream.Seek( 0, SeekOrigin.Begin );
RijndaelManaged aes = new RijndaelManaged( );
   aes.Mode = CipherMode.ECB;
   aes.Padding = PaddingMode.PKCS7;
   aes.KeySize = 128;
aes.Key = key;
//aes.IV = _iV;
CryptoStream cryptoStream = new CryptoStream( mStream, aes.CreateDecryptor( ), CryptoStreamMode.Read );
try {


byte[] tmp = new byte[ encryptedBytes.Length + 32 ];
int len = cryptoStream.Read( tmp, 0, encryptedBytes.Length + 32 );
byte[] ret = new byte[ len ];
Array.Copy( tmp, 0, ret, 0, len );
return ret;
}
finally {
cryptoStream.Close( );
mStream.Close( );
aes.Clear( );
}
}


  public static byte[] Encrypt(byte[] plainBytes, byte[] key)
  {
   MemoryStream mStream = new MemoryStream();
   RijndaelManaged aes = new RijndaelManaged();


   aes.Mode = CipherMode.ECB;
   aes.Padding = PaddingMode.PKCS7;
   aes.KeySize = 128;
   //aes.Key = _key;


   aes.Key = key;
   //aes.IV = _iV;
   CryptoStream cryptoStream = new CryptoStream(mStream, aes.CreateEncryptor(), CryptoStreamMode.Write);
   try
   {
    cryptoStream.Write(plainBytes, 0, plainBytes.Length);
    cryptoStream.FlushFinalBlock();
    return mStream.ToArray();
   }
   finally
   {
    cryptoStream.Close();
    mStream.Close();
    aes.Clear();
   }
  }
 }

I hope that through this article, we have some understanding of AES encryption and decryption, which is helpful to C # programming.