Introduction:
Encryption involves transforming plain text into ciphertext, a jumbled version of the text that is indecipherable to individuals without authorization. This method serves to safeguard confidential information from unauthorized individuals. The Advanced Encryption Standard (AES) stands as a prevalent encryption technique known for its robust security measures. In this guide, we will delve into the process of executing AES Encryption using C#.
What is AES Encryption?
The AES Encryption technique functions as a Symmetric-key Block Cipher, employing a consistent block size of 128 bits and key lengths of 128, 192, or 256 bits. This encryption method was created by Joan Daemen and Vincent Rijmen, Belgian cryptographers, and was integrated as a U.S. Federal Information Processing Standard (FIPS) in 2001. AES Encryption is extensively applied in numerous scenarios such as safeguarding communication protocols, encrypting files, and securing passwords.
The AES Encryption algorithm employs a sequence of mathematical procedures to encode and decode information. Encryption comprises multiple iterations of substitution, permutation, and linear transformation procedures. The encryption and decryption key remains constant, making AES a Symmetric-Key Algorithm. Consequently, the identical key is applied for both encryption and decryption purposes.
Implementing AES Encryption in C#:
C# offers native functionality for AES Encryption via the System.Security.Cryptography namespace. Within this namespace, there are diverse classes and functions available for integrating a range of encryption algorithms, like AES.
Below are the steps involved in implementing AES Encryption in C#:
- Generate a new instance of the AES algorithm.
- Set the key and initialization vector (IV) for the encryption process.
- Create a new instance of the CryptoStream class for encryption.
- Create a memory stream to store the encrypted data.
- Write the data to be encrypted into the CryptoStream.
- Close the CryptoStream to complete the encryption process.
- Retrieve the encrypted data from the memory stream.
- To decrypt, repeat the above steps using the CryptoStream for decryption instead.
Step 1: Create an Instance of the AES Class
To incorporate AES Encryption in C#, it is necessary to instantiate an object of the Aes class, which is available within the System.Security.Cryptography namespace. This class serves as the implementation of the AES Encryption algorithm and offers functionalities for both encrypting and decrypting information.
C# Code:
using System.Security.Cryptography;
Aes aes = Aes.Create();
Step 2: Set the Key Size and Mode
The Aes class offers attributes for specifying the size of the key and the encryption mode. The key size dictates the magnitude of the encryption key utilized for both encryption and decryption processes, with options of 128, 192, or 256 bits. The mode selection influences the encryption methodology applied to the data, with popular choices being ECB (Electronic Codebook) and CBC (Cipher Block Chaining).
C# Code:
aes.KeySize = 256;
aes.Mode = CipherMode.CBC;
Step 3: Create a Random Key and Initialization Vector Using a Cryptographically Secure Pseudorandom Number Generator (CSPRNG).
The AES Encryption algorithm necessitates both a key and an Initialization Vector (IV) for the encryption and decryption processes. The key serves as a confidential value applied in the encryption and decryption of data, while the IV functions as a random value utilized to kickstart the encryption procedure. Within the Aes class, there are functions available for generating both a random key and IV.
C# Code:
aes.GenerateKey();
aes.GenerateIV();
Step 4: Encrypt the Data
To secure the information, it is essential to instantiate the ICryptoTransform interface, which is accessible through the Aes class. This interface offers functionalities for modifying data through the AES Encryption algorithm.
C# Code:
ICryptoTransform encryptor = aes.CreateEncryptor();
We can utilize the encryptor instance to encode the information by employing the TransformBlock method, which accepts the original data and provides the resulting encrypted text.
C# Code:
byte[] plaintext = Encoding.UTF8.GetBytes("Hello, world!");
byte[] ciphertext = encryptor.TransformFinalBlock(plaintext, 0, plaintext.Length);
Step 5: Decrypt the Data
To decipher the information, it is necessary to instantiate a fresh object of the ICryptoTransform interface with the identical key and initialization vector (IV) that were employed during the encryption process.
C# Code:
ICryptoTransform decryptor = aes.CreateDecryptor(aes.Key, aes.IV);
byte[] plaintextBytes = decryptor.TransformFinalBlock(ciphertext, 0, ciphertext.Length);
string plaintext = Encoding.UTF8.GetString(plaintextBytes);
Here is the full code for implementing AES Encryption in C#:
C# Code:
using System;
using System.Security.Cryptography;
using System.Text;
namespace AESExample
{
class Program
{
static void Main(string[] args)
{
Aes aes = Aes.Create();
aes.KeySize = 256;
aes.Mode = CipherMode.CBC;
aes.GenerateKey();
aes.GenerateIV();
string plaintext = "Hello, world!";
byte[] ciphertext;
using (ICryptoTransform encryptor = aes.CreateEncryptor())
{
byte[] plaintextBytes = Encoding.UTF8.GetBytes(plaintext);
ciphertext = encryptor.TransformFinalBlock(plaintextBytes, 0, plaintextBytes.Length);
}
string decryptedText;
using (ICryptoTransform decryptor = aes.CreateDecryptor(aes.Key, aes.IV))
{
byte[] decryptedBytes = decryptor.TransformFinalBlock(ciphertext, 0, ciphertext.Length);
decryptedText = Encoding.UTF8.GetString(decryptedBytes);
}
Console.WriteLine($"Plaintext: {plaintext}");
Console.WriteLine($"Ciphertext: {Convert.ToBase64String(ciphertext)}");
Console.WriteLine($"Decrypted text: {decryptedText}");
}
}
}
In this script, we instantiate the Aes class and configure the key size and encryption mode. Following that, we randomly generate a key and initialization vector (IV) by employing the GenerateKey and GenerateIV techniques. An ICryptoTransform object is then constructed through the CreateEncryptor function for encrypting the original text. Subsequently, another ICryptoTransform object is created with the identical key and IV for decrypting the encrypted data. To conclude, the original text, encrypted data, and decrypted content are displayed on the console.