TL;DR
This guide shows you how to encrypt and decrypt data securely using symmetric encryption in your .NET applications. We’ll use the AES algorithm with a strong key, focusing on practical implementation for protecting sensitive information.
1. Choose an Encryption Algorithm
AES (Advanced Encryption Standard) is a widely used and secure symmetric encryption algorithm. .NET provides excellent support for it through the AesCryptoServiceProvider class.
2. Generate a Strong Key
The key is crucial! A weak key compromises the entire system. Use a cryptographically strong random number generator to create your key. The key size should be at least 128 bits (16 bytes), but 256 bits (32 bytes) is generally recommended for higher security.
using System.Security.Cryptography;
// Generate a 256-bit AES key
byte[] key = new byte[32];
using (var rng = RandomNumberGenerator.Create())
{
rng.GetBytes(key);
}
3. Create an Initialization Vector (IV)
The IV adds randomness to the encryption process, even if you encrypt the same data multiple times with the same key. It must be unique for each encryption operation. Use a random number generator like in step 2 and ensure it’s the correct size for your chosen cipher block size (typically 16 bytes for AES).
// Generate a 16-byte IV
byte[] iv = new byte[16];
using (var rng = RandomNumberGenerator.Create())
{
rng.GetBytes(iv);
}
4. Encryption Process
- Instantiate the AES CryptoServiceProvider: Create an instance of
AesCryptoServiceProvider. - Set Key and IV: Configure the crypto service provider with your generated key and IV.
- Create a Cipher Stream: Use a
MemoryStreamto hold the encrypted data. ACryptoStreamwill handle the encryption process. - Write Data to the CryptoStream: Write the plaintext data into the
CryptoStream, which encrypts it on-the-fly. - Retrieve Encrypted Data: Read the encrypted bytes from the
MemoryStream.
using System.IO;
public static byte[] Encrypt(byte[] plaintext, byte[] key, byte[] iv)
{
using (var aes = new AesCryptoServiceProvider())
{
aes.Key = key;
aes.IV = iv;
using (var memoryStream = new MemoryStream())
using (var cryptoStream = new CryptoStream(memoryStream, aes.CreateEncryptor(), CryptoStreamMode.Write))
{
cryptoStream.Write(plaintext, 0, plaintext.Length);
cryptoStream.FlushFinalBlock();
return memoryStream.ToArray();
}
}
}
5. Decryption Process
- Instantiate the AES CryptoServiceProvider: Create an instance of
AesCryptoServiceProvider. - Set Key and IV: Configure with the *same* key and IV used for encryption.
- Create a Cipher Stream: Use a
MemoryStreamto hold the decrypted data, and aCryptoStreamfor decryption. - Write Encrypted Data to CryptoStream: Write the ciphertext into the
CryptoStream, which decrypts it on-the-fly. - Retrieve Decrypted Data: Read the plaintext bytes from the
MemoryStream.
public static byte[] Decrypt(byte[] ciphertext, byte[] key, byte[] iv)
{
using (var aes = new AesCryptoServiceProvider())
{
aes.Key = key;
aes.IV = iv;
using (var memoryStream = new MemoryStream())
using (var cryptoStream = new CryptoStream(memoryStream, aes.CreateDecryptor(), CryptoStreamMode.Write))
{
cryptoStream.Write(ciphertext, 0, ciphertext.Length);
cryptoStream.FlushFinalBlock();
return memoryStream.ToArray();
}
}
}
6. Important Considerations
- Key Storage: Never hardcode keys directly into your application! Store them securely using a key management system (e.g., Azure Key Vault, HashiCorp Vault) or Windows Data Protection API.
- IV Management: The IV does *not* need to be secret, but it must be unique for each encryption operation. You can store the IV alongside the ciphertext.
- Error Handling: Implement robust error handling to catch exceptions during encryption and decryption.
- Data Integrity: Consider using a Message Authentication Code (MAC) like HMAC-SHA256 in addition to encryption to verify data integrity and prevent tampering.
- cyber security Best Practices: Regularly review your code for vulnerabilities and keep your .NET framework updated with the latest security patches.

