Table of Contents

NoPadding Class

Definition

Namespace
Bodu.Security.Cryptography
Assembly
Bodu.Security.Cryptography.dll
Package
Bodu.Security.Cryptography 1.2.0
Source
NoPadding.cs

Represents a pass-through padding strategy that adds and removes no bytes, requiring the caller to provide data whose length is already a multiple of the cipher block size.

public sealed class NoPadding : IPaddingStrategy
Inheritance
NoPadding
Implements
Inherited Members
Extension Methods

Examples

using Bodu.Security.Cryptography;

// Caller guarantees that `plaintext.Length` is a multiple of the block size.
IPaddingStrategy padding = new NoPadding();
byte[] aligned = padding.Pad(plaintext, blockSize: 128); // 128 bits = 16 bytes; throws if not aligned

Remarks

Use this strategy only when the plaintext length is guaranteed to be block-aligned, for example when encrypting fixed-size records or when another framing layer already handles length information.

When to choose no padding. The right pick under modes that handle alignment themselves - CTR ( CtrModeTransform), CTS (CtsModeTransform), and every AEAD mode ( GcmModeTransform, CcmModeTransform, EaxModeTransform, GcmSivModeTransform, OcbModeTransform, SivModeTransform) - none of which require the caller to align input to the cipher block size. Also the right pick when encrypting fixed-size on-disk records under XTS. For length-recoverable padding under CBC or ECB use Pkcs7Padding.

Constructors

NoPadding()

public NoPadding()

Properties

StripsPaddingOnUnpad

Gets a value indicating whether Unpad(ReadOnlySpan<byte>, int) inspects the final block and may return fewer bytes than it received. Self-describing schemes such as PKCS#7, ANSI X.923, ISO 10126 and ISO/IEC 7816-4 return true; pass-through schemes such as zero padding and no padding return false.

public bool StripsPaddingOnUnpad { get; }

Property Value

bool

Remarks

Streaming block-cipher transforms use this flag to decide whether the final ciphertext block must be deferred during decryption so that padding validation and removal can happen at the stream boundary.

Methods

Pad(ReadOnlySpan<byte>, int)

Returns a copy of input after verifying that its length is a multiple of blockSize.

public byte[] Pad(ReadOnlySpan<byte> input, int blockSize)

Parameters

input ReadOnlySpan<byte>

The input data to validate and return.

blockSize int

The required block size in bits. Must be a positive multiple of 8.

Returns

byte[]

A new byte array containing the same bytes as input.

Exceptions

ArgumentOutOfRangeException

Thrown if blockSize is not a positive multiple of 8.

ArgumentException

Thrown if the length of input is not a multiple of blockSize.

Unpad(ReadOnlySpan<byte>, int)

Returns a copy of input unchanged, since no padding is ever added by this strategy.

public byte[] Unpad(ReadOnlySpan<byte> input, int blockSize)

Parameters

input ReadOnlySpan<byte>

The input data to return.

blockSize int

The block size in bits. This value is ignored.

Returns

byte[]

A new byte array containing the same bytes as input.

Applies to

ProductVersions
.NET8, 10