Introduction
In this article, I have explained how to encrypt and decrypt the text using the AES Encryption standard.
Open Visual Studio, Create a new console application.

Provide a project name and choose the location to store the project information, and click next.

Choose the .Net framework based on your project requirement, then click Create.

Once the project has been created, then Right Click on the project name, choose to add, and click on the new item.
Add the class file to the existing project.

From the C# node, then choose the class definition and provide the class file name like Encrypt and decrypt file.cs.
After we added the class file to our project solution

The class file was created successfully after we wrote the code in the class file.

Use the below code to encrypt the given text input.
public static string EncryptString(string plainText)
{
byte[] array;
using (Aes aes = Aes.Create())
{
aes.Padding = PaddingMode.PKCS7;
aes.KeySize = 256;
aes.Key = new byte[32];
aes.IV = new byte[16];
aes.Padding = PaddingMode.PKCS7;
ICryptoTransform encryptor = aes.CreateEncryptor(aes.Key, aes.IV);
using (MemoryStream memoryStream = new MemoryStream())
{
using (CryptoStream cryptoStream = new CryptoStream((Stream)memoryStream, encryptor, CryptoStreamMode.Write))
{
using (StreamWriter streamWriter = new StreamWriter((Stream)cryptoStream))
{
streamWriter.Write(plainText);
}
array = memoryStream.ToArray();
}
}
}
return Convert.ToBase64String(array);
}
Join the conversation! Your thoughts help the community grow.