TheAlgorithms/C-Sharp · error · ArgumentOutOfRangeException
Invalid block size
Error message
Invalid block size: {blockSize} What it means
Pkcs7Padding's constructor validates that the block size is between 1 and 255, since PKCS#7 encodes the padding length as a single byte. Passing any other value makes valid PKCS#7 padding impossible, so the library throws ArgumentOutOfRangeException immediately at construction rather than failing later during padding operations.
Solutions
- Pass a block size in bytes between 1 and 255 (16 for AES, 8 for DES/3DES).
- If you have a bit-based size, divide by 8 before constructing.
- Validate configuration values before constructing the padding.
- If you need larger blocks than 255 bytes, use a different padding scheme or library.
Example fix
// before var padding = new Pkcs7Padding(128); // bits, throws // after var padding = new Pkcs7Padding(128 / 8); // 16 bytes
Defensive patterns
Strategy: validation
Validate before calling
if (blockSize is < 1 or > 255)
throw new ArgumentOutOfRangeException(nameof(blockSize), blockSize, "Block size must be 1..255 bytes");
var padding = new Pkcs7Padding(blockSize); Type guard
static bool IsValidBlockSize(int size) => size is >= 1 and <= 255;
Try / catch
try { padding = new Pkcs7Padding(cfg.BlockSize); }
catch (ArgumentOutOfRangeException ex) { /* surface config error to operator */ throw new InvalidOperationException("Invalid configured block size", ex); } Prevention
- Express block sizes in bytes, not bits (AES=16, DES=8).
- Validate config values at startup before constructing crypto primitives.
- Use a validated factory method that clamps/rejects bad sizes.
When it happens
Trigger: Calling new Pkcs7Padding(n) with n < 1 (e.g. 0 or negative) or n > 255 (e.g. 512, or a bit size like 128 mistaken for a byte size).
Common situations: Developers confusing block size in bits (AES 128-bit) with bytes and passing 128 in bits-based units while intending 16; copying initialization code and forgetting to set the size; reading a config value of 0 when the block size is unset.
Related errors
- Not enough space in input array for padding
- Not enough space in input array for padding
- Input length must be a multiple of block size
- Invalid padding length
- Invalid padding
AI-assisted analysis of TheAlgorithms/C-Sharp@96e2905cab (2026-09-13).
Data as JSON: /api/errors/8d529b625375e538.
Report an issue: GitHub.
Appendix: source
Thrown at Algorithms/Crypto/Paddings/Pkcs7Padding.cs:29
/// </para>
/// <para>
/// The padding can be easily removed after decryption by looking at the last byte and subtracting that many bytes from the
/// end of the data.
/// </para>
/// <para>
/// This class supports any block size from 1 to 255 bytes, and can be used with any encryption algorithm that requires
/// padding, such as AES.
/// </para>
/// </summary>
public class Pkcs7Padding : IBlockCipherPadding
{
private readonly int blockSize;
public Pkcs7Padding(int blockSize)
{
if (blockSize is < 1 or > 255)
{
throw new ArgumentOutOfRangeException(nameof(blockSize), $"Invalid block size: {blockSize}");
}
this.blockSize = blockSize;
}
/// <summary>
/// Adds padding to the end of a byte array according to the PKCS#7 standard.
/// </summary>
/// <param name="input">The byte array to be padded.</param>
/// <param name="inputOffset">The offset from which to start padding.</param>
/// <returns>The padding value that was added to each byte.</returns>
/// <exception cref="ArgumentException">
/// If the input array does not have enough space to add <c>blockSize</c> bytes as padding.
/// </exception>
/// <remarks>
/// The padding value is equal to the number of of bytes that are added to the array.
/// For example, if the input array has a length of 16 and the input offset is 10,
/// then 6 bytes with the value 6 will be added to the end of the array.View on GitHub (pinned to 96e2905cab)