This documentation is slowly being reviewed, so you may see some inconsistencies between sections.
For the complete documentation index, see llms.txt. This page is also available as Markdown.

ISO/IEC 7816-4

Purpose

The length of a ciphertext from a stream cipher is equal to the length of the plaintext. Similarly, block cipher padding only adds a small overhead. This metadata can leak information about what was encrypted and thus is something protocols may want to hide.

ISO/IEC 7816-4 padding adds a mandatory hex 80 byte, followed by 00 bytes (if needed) up to a multiple of the block size. This makes it reversible compared to all-zero padding. However, this algorithm does not help with figuring out how much padding to add/what block size to use.

Usage

Fill

Fills a span with padding. This can then be manually concatenated with some data.

Iso78164Padding.Fill(Span<byte> buffer);

Exceptions

ArgumentOutOfRangeException

buffer.Length is equal to 0.

GetPaddedBufferSize

Returns the required buffer size for Pad() based on the unpadded data length and a block size (e.g., 16 bytes).

Exceptions

ArgumentOutOfRangeException

blockSize is less than or equal to 0.

OverflowException

data.Length + paddingSize has resulted in an overflow.

Pad

Fills a span with the data padded up to the specified block size (e.g., a multiple of 16 bytes).

Exceptions

ArgumentOutOfRangeException

buffer.Length is not equal to GetPaddedBufferSize(data, blockSize).

ArgumentOutOfRangeException

blockSize is less than or equal to 0.

OverflowException

GetPaddedBufferSize() validation has resulted in an overflow.

CryptographicException

Error padding data.

GetUnpaddedBufferSize

Returns the number of bytes to slice from the end of the padded data.

Exceptions

ArgumentOutOfRangeException

paddedData.Length is equal to 0.

ArgumentOutOfRangeException

blockSize is less than or equal to 0.

FormatException

Invalid padding.

Notes

Last updated