Generate Encryption Keys in C# - AES & RSA Key Generation
Complete code tutorial with examples and best practices
[ Code Example - Quick Summary ]
Language: C#
What: Generate AES and RSA encryption keys in C# using System.Security.Cryptography for secure encryption and decryption.
Try it: Use our interactive Encryption-keys generator or integrate this code into your C# application.
Generate AES and RSA encryption keys in C# using System.Security.Cryptography for secure encryption and decryption. Looking for other languages? Check our code examples in PHP , JavaScript , Python , Java , C++ , Ruby and Go or use our interactive web generator.
C# Code Example
using System;
using System.Security.Cryptography;
using System.Text;
class EncryptionKeyGenerator
{
static void Main()
{
// AES-256 Key Generation
using (Aes aes = Aes.Create())
{
aes.KeySize = 256;
aes.GenerateKey();
string aesKeyHex = BitConverter.ToString(aes.Key).Replace("-", "");
string aesKeyBase64 = Convert.ToBase64String(aes.Key);
Console.WriteLine($"AES-256 Key (Hex): {aesKeyHex}");
Console.WriteLine($"AES-256 Key (Base64): {aesKeyBase64}");
}
// AES-128 Key (Manual with RNGCryptoServiceProvider)
byte[] aes128Key = new byte[16]; // 128 bits
using (RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider())
{
rng.GetBytes(aes128Key);
}
Console.WriteLine($"AES-128 Key (Hex): {BitConverter.ToString(aes128Key).Replace("-", "")}");
// RSA Key Pair Generation (2048-bit)
using (RSA rsa = RSA.Create(2048))
{
// Export private key
string privateKeyXml = rsa.ToXmlString(true);
byte[] privateKeyBytes = rsa.ExportRSAPrivateKey();
string privateKeyBase64 = Convert.ToBase64String(privateKeyBytes);
// Export public key
string publicKeyXml = rsa.ToXmlString(false);
byte[] publicKeyBytes = rsa.ExportRSAPublicKey();
string publicKeyBase64 = Convert.ToBase64String(publicKeyBytes);
Console.WriteLine($"Private Key (Base64): {privateKeyBase64}");
Console.WriteLine($"Public Key (Base64): {publicKeyBase64}");
}
}
}
[EXPLANATION]
C# provides Aes.Create() for AES key generation. Set KeySize to 128, 192, or 256 bits, then call GenerateKey() to create a cryptographically secure random key. The Key property contains the byte array. For manual key generation, use RNGCryptoServiceProvider (legacy) or RandomNumberGenerator.GetBytes() (.NET 6+). For RSA keys, RSA.Create(keySize) generates a key pair with the specified bit length (2048 or 4096). ToXmlString() exports keys in XML format (legacy), while ExportRSAPrivateKey() and ExportRSAPublicKey() export in standard formats. Convert to Base64 for storage.
Expected Output
AES-256 Key (Hex): D2F5A8C1E4B7F3A9D6C2E5B8F1A4C7D2E9C3F6A8D1B4E7F2A5C8D1E4B7F3A9D6 AES-256 Key (Base64): 0vWoweS38-nWwuW48aRs0unD9qjRtOfy pcjR5Lfzqdbo= AES-128 Key (Hex): F3A7D2E5C1B8F4A9D6C3E7B2F5A8C1D4 Private Key (Base64): MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQC8... Public Key (Base64): MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvP...
Common Use Cases
- Encrypt configuration files in .NET applications
- Secure data transmission in ASP.NET Core APIs
- Protect sensitive user data at rest
- Digital signature validation
- Azure Key Vault integration
Important Notes
-
Use
RandomNumberGenerator.GetBytes()in .NET 6+ -
RNGCryptoServiceProvideris legacy but still secure -
For PEM export, use
ExportRSAPrivateKeyPem()(.NET 5+) - Store keys in Windows Data Protection API (DPAPI) or Azure Key Vault
-
Aesclass automatically uses platform's best implementation (AES-NI)
Try Our Interactive Generator
Don't want to write code? Use our free web-based Encryption-keys generator with instant results.
TRY ENCRYPTION-KEYS GENERATOR →Other Programming Languages
View Encryption-keys generation code examples in PHP
View Encryption-keys generation code examples in JavaScript
View Encryption-keys generation code examples in Python
View Encryption-keys generation code examples in Java
View Encryption-keys generation code examples in C++
View Encryption-keys generation code examples in Ruby
View Encryption-keys generation code examples in Go