Table of Contents

EcbModeTransform Class

Definition

Namespace
Bodu.Security.Cryptography
Assembly
Bodu.Security.Cryptography.dll
Package
Bodu.Security.Cryptography 1.3.0
Source
EcbModeTransform.cs

Applies the Electronic Codebook (ECB) mode transformation to an underlying IBlockCipher, encrypting or decrypting each block independently with no chaining.

public sealed class EcbModeTransform : IBlockCipherModeTransform, IDisposable
Inheritance
EcbModeTransform
Implements
Inherited Members
Extension Methods

Examples

using System.Security.Cryptography;
using Bodu.Security.Cryptography;

// Most callers should reach for SymmetricAlgorithm.Mode = ECB instead of constructing this directly.
// Direct use is appropriate only inside larger primitives (e.g. wide-block constructions, KDFs).
using IBlockCipher cipher = new AesBlockCipher(key);
IBlockCipherModeTransform ecb = new EcbModeTransform(cipher);
byte[] ciphertext = new byte[plaintext.Length];
int written = ecb.Transform(plaintext, ciphertext, encrypt: true);

Remarks

ECB panel - each plaintext block is encrypted independently to its ciphertext block with no feedback.

Encryption computes Cᵢ = E(Pᵢ) and decryption Pᵢ = D(Cᵢ); no initialization vector is used. See panel 1 of the diagram above: each column is entirely self-contained, so the three cells carry no arrows between them.

That independence is exactly what makes ECB insecure for virtually all real-world messages: identical plaintext blocks always yield identical ciphertext blocks, leaking structural information. Prefer CBC, CTR, or an authenticated mode unless ECB is required as a primitive inside a larger construction.

When to use ECB. Only as a building block inside a larger, well-understood construction - for example, encrypting a single fixed-length tweak inside an XTS or wide-block scheme. For protecting arbitrary plaintext, use CbcModeTransform as a baseline, CtrModeTransform when random access or stream-cipher behavior is wanted, or one of the AEAD modes (GcmModeTransform, EaxModeTransform, …) when authentication matters.

Because ECB has no chaining state, the transform is stateless - repeated Transform(ReadOnlySpan<byte>, Span<byte>, bool) calls produce the same result for the same input - and is trivially parallelizable.

Constructors

EcbModeTransform(IBlockCipher)

Initializes a new instance of the EcbModeTransform class that wraps the specified block cipher.

public EcbModeTransform(IBlockCipher cipher)

Parameters

cipher IBlockCipher

The block cipher over which ECB is applied.

Exceptions

ArgumentNullException

Thrown if cipher is null.

Methods

Dispose()

Releases the resources used by this instance. ECB holds no per-message chaining state, so this is a no-op beyond satisfying the IBlockCipherModeTransform contract. The underlying IBlockCipher is not disposed by this type - ownership remains with the caller.

public void Dispose()

Transform(ReadOnlySpan<byte>, Span<byte>, bool)

Transforms input under the mode's chaining strategy and writes the result to output.

public int Transform(ReadOnlySpan<byte> input, Span<byte> output, bool encrypt)

Parameters

input ReadOnlySpan<byte>

The input data to transform.

output Span<byte>

The destination span. Its length must be greater than or equal to the length of input.

encrypt bool

true to encrypt the input; false to decrypt.

Returns

int

The number of bytes written to output.

Remarks

The length rule depends on the mode: ECB, CBC, CFB, OFB, and XTS accept a whole number of blocks, zero included; CtrModeTransform accepts any length; CtsModeTransform accepts any length of at least one block.

Exceptions

ArgumentException

Thrown when output is shorter than input, or, for CtsModeTransform, when input is shorter than one block.

CryptographicException

Thrown when the mode accepts only whole blocks and the length of input is not a multiple of the block size, or when input and output partially overlap.

ObjectDisposedException

Thrown when the transform has been disposed.

Applies to

ProductVersions
.NET8, 10

See Also