Table of Contents

Ansix923Padding Class

Definition

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

Implements the ANSI X.923 padding scheme, which appends N - 1 bytes of value 0x00 followed by a trailing byte holding the padding length N.

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

Examples

using Bodu.Security.Cryptography;

IPaddingStrategy padding = new Ansix923Padding();
byte[] padded = padding.Pad(plaintext, blockSize: 128); // 128 bits = 16 bytes

// padded ends with N-1 zero bytes followed by a single byte holding N.
byte[] recovered = padding.Unpad(padded, blockSize: 128);

Remarks

A full block of padding is always added when the input is already block-aligned so that Unpad(ReadOnlySpan<byte>, int) can unambiguously recover the original length. Valid values of N lie in the range 1..blockSize. Unpad(ReadOnlySpan<byte>, int) validates in constant time to resist padding-oracle side channels.

When to choose ANSI X.923. Pick this when interoperating with legacy financial / banking systems or formats that explicitly require the X.923 layout. For all other cases use Pkcs7Padding - it is the modern standard and is what every mainstream library expects by default.

Constructors

Ansix923Padding()

public Ansix923Padding()

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)

Applies ANSI X.923 padding to the input data, ensuring the total output is a multiple of the block size.

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

Parameters

input ReadOnlySpan<byte>

The data to pad.

blockSize int

The block size in bits.

Returns

byte[]

The padded data as a byte array.

Exceptions

ArgumentOutOfRangeException

Thrown if blockSize is not a positive multiple of 8.

Unpad(ReadOnlySpan<byte>, int)

Validates and removes ANSI X.923 padding from the specified input data.

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

Parameters

input ReadOnlySpan<byte>

The padded data.

blockSize int

The block size in bits.

Returns

byte[]

The unpadded data as a byte array.

Exceptions

ArgumentOutOfRangeException

Thrown if blockSize is not a positive multiple of 8.

ArgumentException

Thrown if input is empty or not aligned to the block size.

CryptographicException

Thrown if the padding is invalid or malformed.

Applies to

ProductVersions
.NET8, 10