TheAlgorithms/C-Sharp · error · ArgumentException

Input length must be a multiple of block size

Error message

Input length must be a multiple of block size

What it means

RemovePadding requires the input length to be an exact multiple of blockSize, because PKCS#7 padding only exists at block boundaries and the last byte defines the padding length. A length that is not a multiple means the input is not a padded block sequence, so the method throws ArgumentException.

Solutions

  1. Decrypt the data with the matching block cipher before calling RemovePadding.
  2. Ensure the input length is a multiple of blockSize (e.g. 16 bytes for AES).
  3. Verify the padding instance's blockSize equals the cipher's block size.
  4. Check that stream/chunked decryption buffers all bytes before unpadding.

Example fix

// before
padding.RemovePadding(cipherBytes); // wrong input
// after
var plain = aes.Decrypt(cipherBytes);
var unpadded = padding.RemovePadding(plain);
Defensive patterns

Strategy: validation

Validate before calling

if (input is null || input.Length == 0 || input.Length % blockSize != 0)
    throw new ArgumentException("Input must be a non-empty multiple of the block size before unpadding");

Type guard

static bool IsBlockAligned(byte[] input, int blockSize) => input is not null && input.Length > 0 && input.Length % blockSize == 0;

Try / catch

try { plain = padding.RemovePadding(decrypted); }
catch (ArgumentException ex) { throw new CryptographicException("Data is not block-aligned — was it decrypted first?", ex); }

Prevention

When it happens

Trigger: Calling RemovePadding on a byte array whose Length % blockSize != 0 — e.g. decrypting raw (non-block-aligned) data, passing ciphertext instead of decrypted plaintext, or truncating the buffer before removing padding.

Common situations: Stream decryption that didn't accumulate a full final block; passing ciphertext that was never decrypted; a mismatch between the padding's blockSize and the cipher's actual block size; concatenating partially decrypted chunks incorrectly.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of TheAlgorithms/C-Sharp@96e2905cab (2026-09-13). Data as JSON: /api/errors/b124dd9f43b98a78. Report an issue: GitHub.

Appendix: source

Thrown at Algorithms/Crypto/Paddings/Pkcs7Padding.cs:87

        }

        return code;
    }

    /// <summary>
    /// Removes the PKCS7 padding from the given input data.
    /// </summary>
    /// <param name="input">The input data with PKCS7 padding. Must not be null and must have a valid length and padding.</param>
    /// <returns>The input data without the padding as a new byte array.</returns>
    /// <exception cref="ArgumentException">
    /// Thrown if the input data is null, has an invalid length, or has an invalid padding.
    /// </exception>
    public byte[] RemovePadding(byte[] input)
    {
        // Check if input length is a multiple of blockSize
        if (input.Length % blockSize != 0)
        {
            throw new ArgumentException("Input length must be a multiple of block size");
        }

        // Get the padding length from the last byte of input
        var paddingLength = input[^1];

        // Check if padding length is valid
        if (paddingLength < 1 || paddingLength > blockSize)
        {
            throw new ArgumentException("Invalid padding length");
        }

        // Check if all padding bytes have the correct value
        for (var i = 0; i < paddingLength; i++)
        {
            if (input[input.Length - 1 - i] != paddingLength)
            {
                throw new ArgumentException("Invalid padding");
            }

View on GitHub (pinned to 96e2905cab)