BlockCipherModeFactory Class
Definition
- Namespace
- Bodu.Security.Cryptography
- Assembly
- Bodu.Security.Cryptography.dll
- Package
- Bodu.Security.Cryptography 1.2.0
Creates IBlockCipherModeTransform instances that wrap an IBlockCipher with a standard chaining mode.
public static class BlockCipherModeFactory
- Inheritance
-
BlockCipherModeFactory
- Inherited Members
Examples
using IBlockCipher cipher = /* construct an IBlockCipher, e.g. an AES wrapper */;
IBlockCipherModeTransform mode = BlockCipherModeFactory.Create(CipherBlockMode.CBC, cipher, iv);
IPaddingStrategy padding = PaddingFactory.Create(PaddingMode.PKCS7);
byte[] padded = padding.Pad(plaintext, cipher.BlockSize);
byte[] ciphertext = new byte[padded.Length];
mode.Transform(padded, ciphertext, encrypt: true);
Remarks
The factory dispatches a CipherModeKind enumeration value to the matching mode-transform implementation. Used internally by every SymmetricAlgorithm in this library when its Mode is set, so that the same enum value selects the appropriate transform regardless of which cipher is in play.
What this factory covers and does not cover. Only the classic confidentiality-only modes (ECB, CBC, CFB, OFB, CTR) are dispatched here. CtsModeTransform and XtsModeTransform have members in the CipherModeKind enumeration (CTS and XTS) but are not dispatched by this factory - their construction shapes differ (XTS requires a second cipher), so they must be constructed directly. Authenticated modes have their own contract and lifecycle - construct an IAeadBlockCipherModeTransform implementation directly, or use the helpers on AeadBlockCipherModeTransformExtensions.
The following example composes a block cipher, a CBC mode transform, and PKCS#7 padding to encrypt a message.
Methods
Create(CipherModeKind, IBlockCipher, byte[]?)
Creates a new IBlockCipherModeTransform instance for the specified block cipher mode.
public static IBlockCipherModeTransform Create(CipherModeKind mode, IBlockCipher cipher, byte[]? iv = null)
Parameters
modeCipherModeKindThe cipher mode to apply (for example CBC, CFB, OFB, ECB, or CTR).
cipherIBlockCipherThe underlying block cipher to wrap.
ivbyte[]The initialization vector or initial counter. Required by all modes except ECB and must have the same length as BlockSize.
Returns
- IBlockCipherModeTransform
An IBlockCipherModeTransform that applies
modeovercipher.
Exceptions
- ArgumentNullException
Thrown if
cipheris null.- ArgumentException
Thrown if
ivis required but null or of the wrong length.- NotSupportedException
Thrown if
modeis not a supported CipherModeKind value.
Applies to
| Product | Versions |
|---|---|
| .NET | 8, 10 |