TheAlgorithms/C-Sharp · error · ArgumentException

Invalid padding length

Error message

Invalid padding length

What it means

PKCS#7 padding length is encoded in the final byte and must be at least 1 and at most blockSize. If the last byte falls outside that range, the data cannot be validly PKCS#7-padded, so RemovePadding throws ArgumentException. This usually means wrong data, wrong key, or a different padding scheme.

Solutions

  1. Verify the encryption and decryption use the same key and IV.
  2. Confirm the data was encrypted with PKCS7 padding, not another scheme.
  3. Decrypt before unpadding; never call RemovePadding on ciphertext.
  4. Validate the data integrity (e.g. HMAC) to catch corrupted ciphertext early.

Example fix

// before
var plain = padding.RemovePadding(raw); // raw was never PKCS7-padded
// after
var decrypted = cipher.Decrypt(raw);
var plain = padding.RemovePadding(decrypted);
Defensive patterns

Strategy: try-catch

Validate before calling

var last = input[^1];
if (last < 1 || last > blockSize)
    throw new CryptographicException("Last byte is not a valid PKCS7 padding length — wrong key or wrong padding scheme?");

Try / catch

try { plain = padding.RemovePadding(decrypted); }
catch (ArgumentException) { throw new CryptographicException("Invalid padding: verify key/IV and padding scheme"); }

Prevention

When it happens

Trigger: Calling RemovePadding on input whose last byte is 0x00 or greater than blockSize — e.g. unpadded data, data padded with another scheme (ISO/IEC 7816-4, ANSI X9.23, zero padding), or output decrypted with a wrong key/IV producing garbage.

Common situations: Decrypting with the wrong key so the last byte is random; switching padding schemes between encrypt and decrypt; stripping padding twice; receiving unpadding raw data from an external source.

Related errors


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

Appendix: source

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

    /// <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");
            }
        }

        // Create a new array with the size of input minus the padding length
        var output = new byte[input.Length - paddingLength];

        // Copy the data without the padding into the output array
        Array.Copy(input, output, output.Length);

        return output;

View on GitHub (pinned to 96e2905cab)