Table of Contents

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

key byte[]

The secret key for the AES-CTR algorithm.

Exceptions

ArgumentNullException

key is null.

CryptographicException

The key length 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

key byte[]

The secret key for the AES-CTR algorithm.

iv byte[]

The initialization vector (initial counter).

Exceptions

ArgumentNullException

key is null.

ArgumentNullException

iv is null.

CryptographicException

The key length is other than 16, 24, or 32 bytes (128, 192, or 256 bits).

ArgumentException

iv is 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

keySize int

The size, in bits, of the randomly generated key.

Exceptions

CryptographicException

keySize is 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

key ReadOnlySpan<byte>

The secret key for the AES-CTR algorithm.

Exceptions

CryptographicException

The key length 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

key ReadOnlySpan<byte>

The secret key for the AES-CTR algorithm.

iv ReadOnlySpan<byte>

The initialization vector (initial counter).

Exceptions

CryptographicException

The key length is other than 16, 24, or 32 bytes (128, 192, or 256 bits).

ArgumentException

iv is 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

iv is null.

ArgumentException

iv is 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

key is null.

CryptographicException

The key length 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

keySize is 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

AesCtr always pretends to use CTS.

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

AesCtr always uses None.

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.")]
public static AesCtr? Create(string algorithmName)

Parameters

algorithmName string

The 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 algorithmName parameter is null.

CreateDecryptor()

Creates a symmetric decryptor object with the current Key property and initialization vector (IV).

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

rgbKey byte[]

The secret key to use for the symmetric algorithm.

rgbIV byte[]

The initialization vector (initial counter).

Returns

ICryptoTransform

A symmetric decryptor object.

Exceptions

ObjectDisposedException

The AesCtr instance has been disposed.

ArgumentNullException

key is null.

CryptographicException

The key length is other than 16, 24, or 32 bytes (128, 192, or 256 bits).

CryptographicException

rgbIV is null.

ArgumentException

iv is the incorrect length. Callers are expected to pass an initialization vector that is exactly BlockSize in length, converted to bytes (BlockSize / 8).

CreateEncryptor()

Creates a symmetric encryptor object with the current Key property and initialization vector (IV).

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

rgbKey byte[]

The secret key to use for the symmetric algorithm.

rgbIV byte[]

The initialization vector (initial counter).

Returns

ICryptoTransform

A symmetric encryptor object.

Exceptions

ObjectDisposedException

The AesCtr instance has been disposed.

ArgumentNullException

key is null.

CryptographicException

The key length is other than 16, 24, or 32 bytes (128, 192, or 256 bits).

CryptographicException

rgbIV is null.

ArgumentException

iv is 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

disposing bool

true 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

input byte[]

The data to transform.

iv byte[]

The initialization vector (initial counter).

Returns

byte[]

The transformed data.

Exceptions

ArgumentNullException

input is null.

ArgumentNullException

iv is null.

ArgumentException

iv is 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

input ReadOnlySpan<byte>

The data to transform.

iv ReadOnlySpan<byte>

The initialization vector (initial counter).

Returns

byte[]

The transformed data.

Exceptions

ArgumentException

iv is 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

input ReadOnlySpan<byte>

The data to transform.

iv ReadOnlySpan<byte>

The initialization vector (initial counter).

destination Span<byte>

The buffer to receive the transformed data.

Returns

int

The total number of bytes written to destination.

Exceptions

ArgumentException

iv is 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 destination is 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

input ReadOnlySpan<byte>

The data to transform.

iv ReadOnlySpan<byte>

The initialization vector (initial counter).

destination Span<byte>

The buffer to receive the transformed data.

bytesWritten int

When this method returns, contains the total number of bytes written to destination.

Returns

bool

true if destination was large enough to receive the transformed data; otherwise, false.

Exceptions

ArgumentException

iv is the incorrect length. Callers are expected to pass an initialization vector that is exactly BlockSize in length, converted to bytes (BlockSize / 8).

See Also