Padding

Purpose

The length of a ciphertext from a stream cipher is equal to the length of the plaintext. In most cases, this is not considered an issue. However, hiding the length of a message can be desirable, and ISO/IEC 7816-4arrow-up-right padding can be used to do this.

The amount of padding, determined by the block size, can either be deterministic or randomised. Both have their strengths and weaknessesarrow-up-right.

circle-exclamation
circle-check

Usage

Fill

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

Padding.Fill(Span<byte> buffer)

Exceptions

ArgumentOutOfRangeExceptionarrow-up-right

buffer has a length of 0.

GetPaddedLength

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

Exceptions

ArgumentOutOfRangeExceptionarrow-up-right

unpaddedLength is less than 0.

ArgumentOutOfRangeExceptionarrow-up-right

blockSize is less than or equal to 0.

ArgumentOutOfRangeExceptionarrow-up-right

The amount of padding is too large.

Pad

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

Exceptions

ArgumentOutOfRangeExceptionarrow-up-right

buffer has a length not equal to GetPaddedLength(data.Length, blockSize).

ArgumentOutOfRangeExceptionarrow-up-right

blockSize is less than or equal to 0.

GetUnpaddedLength

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

Exceptions

ArgumentOutOfRangeExceptionarrow-up-right

paddedData has a length of 0.

ArgumentOutOfRangeExceptionarrow-up-right

blockSize is less than or equal to 0.

FormatExceptionarrow-up-right

Incorrect padding.

Notes

circle-exclamation
circle-exclamation

Last updated