Key Management
This guide explains how to manage encryption keys.
Best Practices
- Never store keys in plain text in code
- Use a secrets manager (Azure Key Vault, AWS Secrets Manager)
- Regenerate keys periodically
- Separate test and production keys
Secure Storage
// Example with Azure Key Vault
public class KeyVaultKeyManager
{
private readonly SecretClient _client;
public KeyVaultKeyManager(string vaultUri)
{
_client = new SecretClient(new Uri(vaultUri), new DefaultAzureCredential());
}
public async Task<Keys> GetEncryptionKeys(string keyName)
{
var keySecret = await _client.GetSecretAsync($"{keyName}-key");
var ivSecret = await _client.GetSecretAsync($"{keyName}-iv");
return new Keys
{
Base64Key = keySecret.Value.Value,
Base64IV = ivSecret.Value.Value
};
}
}