Class AesCtr
- Namespace
- Dorssel.Security.Cryptography
- Assembly
- Dorssel.Security.Cryptography.AesExtra.dll
Provides an implementation of the Advanced Encryption Standard (AES) symmetric algorithm in CTR mode.
public sealed class AesCtr : Aes, IDisposable
- Inheritance
-
AesCtr
- Implements
- Inherited Members
Constructors
AesCtr()
Initializes a new instance of the AesCtr class with a randomly generated 256-bit key and an initial counter of zero.
public AesCtr()
AesCtr(byte[])
Initializes a new instance of the AesCtr class with the specified key data and a randomly generated initial counter.
public AesCtr(byte[] key)
Parameters
keybyte[]The secret key for the AES-CTR algorithm.
Exceptions
- ArgumentNullException
keyis null.- CryptographicException
The
keylength is other than 16, 24, or 32 bytes (128, 192, or 256 bits).
AesCtr(byte[], byte[])
Initializes a new instance of the AesCtr class with the specified key data and initial counter.
public AesCtr(byte[] key, byte[] iv)
Parameters
keybyte[]The secret key for the AES-CTR algorithm.
ivbyte[]The initialization vector (initial counter).
Exceptions
- ArgumentNullException
keyis null.- ArgumentNullException
ivis null.- CryptographicException
The
keylength is other than 16, 24, or 32 bytes (128, 192, or 256 bits).- ArgumentException
ivis the incorrect length. Callers are expected to pass an initialization vector that is exactly BlockSize in length, converted to bytes (BlockSize / 8).
AesCtr(int)
Initializes a new instance of the AesCtr class with a randomly generated key and an initial counter of zero.
public AesCtr(int keySize)
Parameters
keySizeintThe size, in bits, of the randomly generated key.
Exceptions
- CryptographicException
keySizeis other than 128, 192, or 256 bits.
AesCtr(ReadOnlySpan<byte>)
Initializes a new instance of the AesCtr class with the specified key data and a randomly generated initial counter.
public AesCtr(ReadOnlySpan<byte> key)
Parameters
keyReadOnlySpan<byte>The secret key for the AES-CTR algorithm.
Exceptions
- CryptographicException
The
keylength is other than 16, 24, or 32 bytes (128, 192, or 256 bits).
AesCtr(ReadOnlySpan<byte>, ReadOnlySpan<byte>)
Initializes a new instance of the AesCtr class with the specified key data and initial counter.
public AesCtr(ReadOnlySpan<byte> key, ReadOnlySpan<byte> iv)
Parameters
keyReadOnlySpan<byte>The secret key for the AES-CTR algorithm.
ivReadOnlySpan<byte>The initialization vector (initial counter).
Exceptions
- CryptographicException
The
keylength is other than 16, 24, or 32 bytes (128, 192, or 256 bits).- ArgumentException
ivis the incorrect length. Callers are expected to pass an initialization vector that is exactly BlockSize in length, converted to bytes (BlockSize / 8).
Properties
FeedbackSize
Gets or sets the feedback size, in bits, of the cryptographic operation for the Cipher Feedback (CFB) and Output Feedback (OFB) cipher modes.
public override int FeedbackSize { get; set; }
Property Value
- int
The feedback size in bits.
Remarks
AesCtr always uses 128 bits.
Exceptions
- CryptographicException
The feedback size is larger than the block size.
IV
Gets or sets the initialization vector (IV) for the symmetric algorithm.
public override byte[] IV { get; set; }
Property Value
- byte[]
The initialization vector.
Remarks
For AES-CTR, the initialization vector (IV) is the initial counter.
Exceptions
- ArgumentNullException
ivis null.- ArgumentException
ivis the incorrect length. Callers are expected to pass an initialization vector that is exactly BlockSize in length, converted to bytes (BlockSize / 8).- ObjectDisposedException
The AesCtr instance has been disposed.
Key
Gets or sets the secret key for the symmetric algorithm.
public override byte[] Key { get; set; }
Property Value
- byte[]
The secret key to use for the symmetric algorithm.
Exceptions
- ArgumentNullException
keyis null.- CryptographicException
The
keylength is other than 16, 24, or 32 bytes (128, 192, or 256 bits).- ObjectDisposedException
The AesCtr instance has been disposed.
KeySize
Gets or sets the size, in bits, of the secret key used by the symmetric algorithm.
public override int KeySize { get; set; }
Property Value
- int
The size, in bits, of the secret key used by the symmetric algorithm.
Remarks
Setting this property always resets the key to a new random value, even if the key size is set to current value.
Exceptions
- CryptographicException
keySizeis other than 128, 192, or 256 bits.- ObjectDisposedException
The AesCtr instance has been disposed.
Mode
Gets or sets the mode for operation of the symmetric algorithm.
public override CipherMode Mode { get; set; }
Property Value
- CipherMode
One of the enumeration values that specifies the block cipher mode to use for encryption. The default is CBC.
Remarks
Exceptions
- CryptographicException
System.Security.Cryptography.AesManaged.Mode is set to CFB or System.Security.Cryptography.CipherMode.OFB.
Padding
Gets or sets the padding mode used in the symmetric algorithm.
public override PaddingMode Padding { get; set; }
Property Value
- PaddingMode
The padding mode used in the symmetric algorithm. The default is PKCS7.
Remarks
Exceptions
- CryptographicException
The padding mode is not one of the PaddingMode values.
Methods
Create()
Creates a cryptographic object that is used to perform the symmetric algorithm.
[Obsolete("Use one of the constructors instead.")]
public static AesCtr Create()
Returns
- AesCtr
A cryptographic object that is used to perform the symmetric algorithm.
Create(string)
Creates a cryptographic object that specifies the implementation of AES to use to perform the symmetric algorithm.
[Obsolete("Cryptographic factory methods accepting an algorithm name are obsolete. Use the parameterless Create factory method on the algorithm type instead.")]
[RequiresUnreferencedCode("The default algorithm implementations might be removed, use strong type references like 'RSA.Create()' instead.")]
public static AesCtr? Create(string algorithmName)
Parameters
algorithmNamestringThe name of the specific implementation of AES to use.
Returns
- AesCtr
A cryptographic object that is used to perform the symmetric algorithm.
Exceptions
- ArgumentNullException
The
algorithmNameparameter is null.
CreateDecryptor()
public override ICryptoTransform CreateDecryptor()
Returns
- ICryptoTransform
A symmetric decryptor object.
Exceptions
- ObjectDisposedException
The AesCtr instance has been disposed.
CreateDecryptor(byte[], byte[]?)
Creates a symmetric decryptor object with the specified key and initial counter (IV).
public override ICryptoTransform CreateDecryptor(byte[] rgbKey, byte[]? rgbIV)
Parameters
rgbKeybyte[]The secret key to use for the symmetric algorithm.
rgbIVbyte[]The initialization vector (initial counter).
Returns
- ICryptoTransform
A symmetric decryptor object.
Exceptions
- ObjectDisposedException
The AesCtr instance has been disposed.
- ArgumentNullException
keyis null.- CryptographicException
The
keylength is other than 16, 24, or 32 bytes (128, 192, or 256 bits).- CryptographicException
rgbIVis null.- ArgumentException
ivis the incorrect length. Callers are expected to pass an initialization vector that is exactly BlockSize in length, converted to bytes (BlockSize / 8).
CreateEncryptor()
public override ICryptoTransform CreateEncryptor()
Returns
- ICryptoTransform
A symmetric encryptor object.
Exceptions
- ObjectDisposedException
The AesCtr instance has been disposed.
CreateEncryptor(byte[], byte[]?)
Creates a symmetric encryptor object with the specified key and initial counter (IV).
public override ICryptoTransform CreateEncryptor(byte[] rgbKey, byte[]? rgbIV)
Parameters
rgbKeybyte[]The secret key to use for the symmetric algorithm.
rgbIVbyte[]The initialization vector (initial counter).
Returns
- ICryptoTransform
A symmetric encryptor object.
Exceptions
- ObjectDisposedException
The AesCtr instance has been disposed.
- ArgumentNullException
keyis null.- CryptographicException
The
keylength is other than 16, 24, or 32 bytes (128, 192, or 256 bits).- CryptographicException
rgbIVis null.- ArgumentException
ivis the incorrect length. Callers are expected to pass an initialization vector that is exactly BlockSize in length, converted to bytes (BlockSize / 8).
Dispose(bool)
Releases the unmanaged resources used by the SymmetricAlgorithm and optionally releases the managed resources.
protected override void Dispose(bool disposing)
Parameters
disposingbooltrue to release both managed and unmanaged resources; false to release only unmanaged resources.
GenerateIV()
Generates a random initialization vector (IV) to use for the symmetric algorithm.
public override void GenerateIV()
Exceptions
- ObjectDisposedException
The AesCtr instance has been disposed.
GenerateKey()
Generates a random key to use for the symmetric algorithm.
public override void GenerateKey()
Exceptions
- ObjectDisposedException
The AesCtr instance has been disposed.
RegisterWithCryptoConfig()
Registers the AesCtr class with CryptoConfig, such that it can be created by name.
public static void RegisterWithCryptoConfig()
Remarks
CryptoConfig is not supported in browsers.
- See Also
TransformCtr(byte[], byte[])
Transforms data using CTR mode.
public byte[] TransformCtr(byte[] input, byte[] iv)
Parameters
Returns
- byte[]
The transformed data.
Exceptions
- ArgumentNullException
inputis null.- ArgumentNullException
ivis null.- ArgumentException
ivis the incorrect length. Callers are expected to pass an initialization vector that is exactly BlockSize in length, converted to bytes (BlockSize / 8).
TransformCtr(ReadOnlySpan<byte>, ReadOnlySpan<byte>)
Transforms data using CTR mode.
public byte[] TransformCtr(ReadOnlySpan<byte> input, ReadOnlySpan<byte> iv)
Parameters
inputReadOnlySpan<byte>The data to transform.
ivReadOnlySpan<byte>The initialization vector (initial counter).
Returns
- byte[]
The transformed data.
Exceptions
- ArgumentException
ivis the incorrect length. Callers are expected to pass an initialization vector that is exactly BlockSize in length, converted to bytes (BlockSize / 8).
TransformCtr(ReadOnlySpan<byte>, ReadOnlySpan<byte>, Span<byte>)
Transforms data into the specified buffer, using CTR mode.
public int TransformCtr(ReadOnlySpan<byte> input, ReadOnlySpan<byte> iv, Span<byte> destination)
Parameters
inputReadOnlySpan<byte>The data to transform.
ivReadOnlySpan<byte>The initialization vector (initial counter).
destinationSpan<byte>The buffer to receive the transformed data.
Returns
- int
The total number of bytes written to
destination.
Exceptions
- ArgumentException
ivis the incorrect length. Callers are expected to pass an initialization vector that is exactly BlockSize in length, converted to bytes (BlockSize / 8).- ArgumentException
The buffer in
destinationis too small to hold the transformed data.
TryTransformCtr(ReadOnlySpan<byte>, ReadOnlySpan<byte>, Span<byte>, out int)
Attempts to transform data into the specified buffer, using CTR mode.
public bool TryTransformCtr(ReadOnlySpan<byte> input, ReadOnlySpan<byte> iv, Span<byte> destination, out int bytesWritten)
Parameters
inputReadOnlySpan<byte>The data to transform.
ivReadOnlySpan<byte>The initialization vector (initial counter).
destinationSpan<byte>The buffer to receive the transformed data.
bytesWrittenintWhen this method returns, contains the total number of bytes written to
destination.
Returns
Exceptions
- ArgumentException
ivis the incorrect length. Callers are expected to pass an initialization vector that is exactly BlockSize in length, converted to bytes (BlockSize / 8).