Iso7816_4Padding Class
Definition
- Namespace
- Bodu.Security.Cryptography
- Assembly
- Bodu.Security.Cryptography.dll
- Package
- Bodu.Security.Cryptography 1.2.0
- Source
- Iso7816_4Padding.cs
Implements the ISO/IEC 7816-4 padding scheme (also known as "one-and-zeros" or bit padding). The first pad byte is
0x80 and remaining pad bytes are 0x00.
public sealed class Iso7816_4Padding : IPaddingStrategy
- Inheritance
-
Iso7816_4Padding
- Implements
- Inherited Members
- Extension Methods
Examples
using Bodu.Security.Cryptography;
IPaddingStrategy padding = new Iso7816_4Padding();
byte[] padded = padding.Pad(plaintext, blockSize: 128); // 128 bits = 16 bytes
// padded ends with 0x80 followed by zero or more 0x00 bytes.
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 by locating the terminator. The scheme is widely used in smart-card protocols, SHA-3/Keccak and CMAC. Unpad(ReadOnlySpan<byte>, int) validates in constant time over the final block to resist padding-oracle side channels.
When to choose ISO/IEC 7816-4. Pick this when interoperating with smart-card, EMV, or ISO crypto
tooling - the 0x80/0x00 sentinel scheme is the standard there. It is also the padding used by the
SHA-3 / Keccak family and by CMAC. For general-purpose .NET / Java / OpenSSL interop prefer
Pkcs7Padding; for AEAD modes that handle their own alignment use NoPadding.
Constructors
Iso7816_4Padding()
public Iso7816_4Padding()
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)
Applies ISO/IEC 7816-4 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
inputReadOnlySpan<byte>The data to pad.
blockSizeintThe block size in bits. Must be a positive multiple of 8.
Returns
- byte[]
The padded data as a byte array.
Exceptions
- ArgumentOutOfRangeException
Thrown if
blockSizeis not a positive multiple of 8.
Unpad(ReadOnlySpan<byte>, int)
Validates and removes ISO/IEC 7816-4 padding from the specified input data.
public byte[] Unpad(ReadOnlySpan<byte> input, int blockSize)
Parameters
inputReadOnlySpan<byte>The padded data.
blockSizeintThe block size in bits. Must be a positive multiple of 8.
Returns
- byte[]
The unpadded data as a byte array.
Exceptions
- ArgumentOutOfRangeException
Thrown if
blockSizeis not a positive multiple of 8.- ArgumentException
Thrown if
inputis empty or not aligned to the block size.- CryptographicException
Thrown if the padding terminator is missing or malformed.
Applies to
| Product | Versions |
|---|---|
| .NET | 8, 10 |