Class AesKeyOptions
- Namespace
- Cuemon.Security.Cryptography
- Assembly
- Cuemon.Security.Cryptography.dll
Configuration options for GenerateKey(Action<AesKeyOptions>).
public class AesKeyOptions : IParameterObject
- Inheritance
-
AesKeyOptions
- Implements
Examples
The following example demonstrates how to configure AesKeyOptions to specify the key size and custom random string provider when generating AES keys.
using System;
using Cuemon.Security.Cryptography;
namespace MyApp.Security.Cryptography
{
public class AesKeyOptionsExamples
{
public static void ConfigureKeySize()
{
// Default is AesSize.Aes256 (32 bytes).
var defaultOptions = new AesKeyOptions();
Console.WriteLine("Default key size: {0}", defaultOptions.Size); // Aes256
// Generate a 128-bit key with AesKeyOptions.
byte[] key128 = AesCryptor.GenerateKey(o =>
{
o.Size = AesSize.Aes128;
});
Console.WriteLine("128-bit key length: {0} bytes", key128.Length); // 16
// Generate a 192-bit key.
byte[] key192 = AesCryptor.GenerateKey(o =>
{
o.Size = AesSize.Aes192;
});
Console.WriteLine("192-bit key length: {0} bytes", key192.Length); // 24
// Generate a 256-bit key explicitly.
byte[] key256 = AesCryptor.GenerateKey(o =>
{
o.Size = AesSize.Aes256;
});
Console.WriteLine("256-bit key length: {0} bytes", key256.Length); // 32
}
public static void CustomRandomStringProvider()
{
// Replace the default random string provider with a custom one.
// The provider is used internally when generating passphrase-based keys.
byte[] key = AesCryptor.GenerateKey(o =>
{
o.Size = AesSize.Aes128;
o.RandomStringProvider = size => new string('X', size == AesSize.Aes128 ? 16 : size == AesSize.Aes192 ? 24 : 32);
});
Console.WriteLine("Custom provider key length: {0} bytes", key.Length); // 16
}
public static void InspectDefaultProperties()
{
var options = new AesKeyOptions();
Console.WriteLine("Default size: {0}", options.Size); // Aes256
Console.WriteLine("RandomStringProvider != null: {0}", options.RandomStringProvider != null); // true
}
}
}
Constructors
AesKeyOptions()
Initializes a new instance of the AesKeyOptions class.
public AesKeyOptions()
Remarks
The following table shows the initial property values for an instance of AesKeyOptions.
| Property | Initial Value |
|---|---|
| RandomStringProvider |
|
| Size | Aes256 |
Properties
RandomStringProvider
Gets or sets the function delegate that provides a random generated string.
public Func<AesSize, string> RandomStringProvider { get; set; }
Property Value
Exceptions
- ArgumentNullException
valuecannot be null.
Size
Gets or sets the size of the Advanced Encryption Standard (AES) symmetric algorithm.
public AesSize Size { get; set; }
Property Value
- AesSize
The size of the Advanced Encryption Standard (AES) symmetric algorithm.
Exceptions
- InvalidEnumArgumentException
valueis not a valid value of AesSize.