ZeroPadding Class
Definition
- Namespace
- Bodu.Security.Cryptography
- Assembly
- Bodu.Security.Cryptography.dll
- Package
- Bodu.Security.Cryptography 1.2.0
- Source
- ZeroPadding.cs
Implements zero-byte padding, appending 0x00 bytes until the input aligns with the cipher block size.
public sealed class ZeroPadding : IPaddingStrategy
- Inheritance
-
ZeroPadding
- Implements
- Inherited Members
- Extension Methods
Examples
using Bodu.Security.Cryptography;
// Caller is responsible for tracking the original plaintext length -
// Unpad here returns the padded buffer unchanged.
IPaddingStrategy padding = new ZeroPadding();
byte[] padded = padding.Pad(plaintext, blockSize: 128); // 128 bits = 16 bytes
Remarks
Zero padding is not self-describing and cannot be unambiguously removed when the plaintext itself may end in zero bytes. Use this strategy only when the original plaintext length is recorded out-of-band or the data is known never to contain trailing zeros.
When to choose zero padding. Pick zero padding only when the surrounding format already records the
plaintext length explicitly (e.g. a length-prefixed protocol frame or fixed-size record), or when the plaintext is
text that cannot legitimately contain trailing 0x00 bytes. For ordinary length-recoverable padding pick
Pkcs7Padding; for AEAD modes that handle alignment internally pick NoPadding.
Constructors
ZeroPadding()
public ZeroPadding()
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
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)
Pads the input with zero bytes to align its length to a multiple of the block size.
public byte[] Pad(ReadOnlySpan<byte> input, int blockSize)
Parameters
inputReadOnlySpan<byte>The input data to pad.
blockSizeintThe block size in bits. Must be a positive multiple of 8.
Returns
- byte[]
The padded input.
Exceptions
- ArgumentOutOfRangeException
Thrown if
blockSizeis not a positive multiple of 8.
Unpad(ReadOnlySpan<byte>, int)
Returns the input as-is. Zero padding cannot be safely removed unless the original length is known.
public byte[] Unpad(ReadOnlySpan<byte> input, int blockSize)
Parameters
inputReadOnlySpan<byte>The padded input.
blockSizeintThe block size in bits. Must be a positive multiple of 8.
Returns
- byte[]
The original input with zero padding preserved.
Remarks
The method does not remove trailing zeros because it cannot distinguish between padding and legitimate data.
Applies to
| Product | Versions |
|---|---|
| .NET | 8, 10 |