C# AES Encryption
AES (Advanced Encryption Standard) is a symmetric encryption algorithm that is widely used to secure data. In C#, you can use the Aes
class to implement AES encryption. In this tutorial, we'll discuss how to implement AES encryption in C# using the Aes
class.
Syntax
The basic syntax for implementing AES encryption in C# is as follows:
using System.Security.Cryptography;
Aes aes = Aes.Create();
aes.Key = key;
aes.IV = iv;
byte[] encryptedData;
using (var ms = new MemoryStream())
using (var cs = new CryptoStream(ms, aes.CreateEncryptor(), CryptoStreamMode.Write))
{
cs.Write(plainData, 0, plainData.Length);
cs.FlushFinalBlock();
encryptedData = ms.ToArray();
}
Here, key
and iv
are byte arrays containing the encryption key and initialization vector (IV), and plainData
is the data to be encrypted. The encryptedData
variable contains the encrypted data.
Example
Let's say we want to encrypt a string using AES encryption. Here's how we can implement it:
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
public byte[] Encrypt(string plainText, byte[] key, byte[] iv)
{
byte[] plainData = Encoding.UTF8.GetBytes(plainText);
using (var aes = Aes.Create())
{
aes.Key = key;
aes.IV = iv;
byte[] encryptedData;
using (var ms = new MemoryStream())
using (var cs = new CryptoStream(ms, aes.CreateEncryptor(), CryptoStreamMode.Write))
{
cs.Write(plainData, 0, plainData.Length);
cs.FlushFinalBlock();
encryptedData = ms.ToArray();
}
return encryptedData;
}
}
Now, we can call the Encrypt method and pass the plain text, encryption key, and initialization vector:
var plainText = "This is a test";
var key = new byte[] { 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C };
var iv = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F };
var encryptedData = Encrypt(plainText, key, iv);
Console.WriteLine(Convert.ToBase64String(encryptedData)); // Output: yCjiUdRsNllDD2VxVRVfoA==
Output
When we run the example code above, the output will be:
yCjiUdRsNllDD2VxVRVfoA==
This is because we encrypted the string "This is a test" using AES encryption with the key and IV specified, and then converted the resulting encrypted data to a base64-encoded string.
Explanation
In the example above, we defined a method called Encrypt
that takes a string to be encrypted, an encryption key, and an initialization vector (IV). The method first converts the plain text to a byte array using UTF-8 encoding. Then it creates a new Aes
object, sets the key and IV, and creates a CryptoStream
object with the Aes
object's encryptor. The CryptoStream
writes the encrypted data to a MemoryStream
, which we later convert to a byte array.
Use
AES encryption is useful for securing sensitive data, such as passwords and personal information, during transmission or storage. It can also be used to encrypt data on disk or in databases.
Important Points
- When using AES encryption, make sure to keep the encryption key and IV secure and never store them alongside the encrypted data.
- The
Aes
object andCryptoStream
objects should always be disposed of properly to avoid resource leaks. - The same key and IV must be used for both encryption and decryption.
Summary
In this tutorial, we discussed how to implement AES encryption in C# using the Aes
class. We covered the syntax, example, output, explanation, use, and important points of AES encryption in C#. With this knowledge, you can now use AES encryption in your C# code to securely encrypt data and help protect against unauthorized access.