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
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
blockSize is less than or equal to 0.
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
buffer.Length is not equal to GetPaddedBufferSize(data, blockSize).
blockSize is less than or equal to 0.
GetPaddedBufferSize() validation has resulted in an overflow.
Error padding data.
GetUnpaddedBufferSize
Returns the number of bytes to slice from the end of the padded data.
Exceptions
paddedData.Length is equal to 0.
blockSize is less than or equal to 0.
Invalid padding.
Notes
Last updated