Introduction
AES is a symmetric key algorithm, meaning the same key is used for both encryption and decryption. This is in contrast to asymmetric encryption, where a public key is used for encryption and a private key for decryption.
Encryption
Step 1
using (var rijndaelManaged = new RijndaelManaged { Key = cryptkey, IV = initVector, Mode = CipherMode.CBC, Padding = PaddingMode.PKCS7, KeySize = 128 })
- RijndaelManaged: This is a class provided by the .NET framework to perform AES encryption.
- Key: The crypt key is the encryption key used. It must be 128, 192, or 256 bits long (16, 24, or 32 bytes).
- IV: initVector is the initialization vector, which is used to add randomness to the encryption process.
- Mode = CipherMode.CBC: Sets the mode of operation to Cipher Block Chaining (CBC). This means each block of plaintext is XORed with the previous ciphertext block before being encrypted.
- Padding = PaddingMode.PKCS7: Specifies the padding scheme to ensure the data length is compatible with the block size of the cipher.
- KeySize = 128: The size of the encryption key in bits (128 bits in this case).
Step 2
using (var memoryStream = new MemoryStream())
MemoryStream: This is a stream in memory where the encrypted bytes will be written.
Step 3
using (var cryptoStream = new CryptoStream(memoryStream, rijndaelManaged.CreateEncryptor(cryptkey, initVector), CryptoStreamMode.Write))
- CryptoStream: This stream links data streams to cryptographic transformations. Here, it wraps the MemoryStream.
- CreateEncryptor(cryptkey, initVector): Creates a symmetric encryptor object using the provided key and IV.
- CryptoStreamMode.Write: Specifies that the stream will be used for writing.
Step 4
using (var ws = new StreamWriter(cryptoStream))
{
ws.Write(textToCrypt);
}
StreamWriter: This class is used to write characters to a stream. It writes the plaintext textToCrypt to the CryptoStream, which automatically encrypts it and writes the result to the MemoryStream.
Step 5
return Convert.ToBase64String(memoryStream.ToArray());
The encrypted bytes stored in the MemoryStream are converted to a Base64 string, which is returned as the result of the encryption process.
Completed Encryption Method
public string EncryptAES(string textToCrypt)
{
private byte[] cryptkey = Encoding.ASCII.GetBytes("1234567891234567");
private byte[] initVector = Encoding.ASCII.GetBytes("1234567891234567");
try
{
using (var rijndaelManaged =
new RijndaelManaged { Key = cryptkey, IV = initVector, Mode = CipherMode.CBC, Padding = PaddingMode.PKCS7, KeySize = 128 })
using (var memoryStream = new MemoryStream())
using (var cryptoStream =
new CryptoStream(memoryStream,
rijndaelManaged.CreateEncryptor(cryptkey, initVector),
CryptoStreamMode.Write))
{
using (var ws = new StreamWriter(cryptoStream))
{
ws.Write(textToCrypt);
}
return Convert.ToBase64String(memoryStream.ToArray());
}
}
catch (CryptographicException e)
{
Console.WriteLine("A Cryptographic error occurred: {0}", e.Message);
return null;
}
}


Join the conversation! Your thoughts help the community grow.