TheAlgorithms/C-Sharp · error · ArgumentException

Invalid padding length

Error message

Invalid padding length

What it means

RemovePadding reads the last byte as the ISO 10126 padding length and throws ArgumentException if it is less than 1 or greater than the array length, because such a value cannot describe valid padding. This almost always means the data was corrupted, tampered with, or decrypted with the wrong key/parameters.

Solutions

  1. Verify the decryption key, IV and cipher mode match those used for encryption
  2. Confirm the data actually went through ISO 10126 padding before unpadding
  3. Treat as possible ciphertext tampering — use authenticated encryption (e.g. GCM) to detect corruption instead of relying on padding errors

Example fix

// before
var plain = cipher.DoFinal(ciphertext);
var unpadded = padding.RemovePadding(plain); // throws on corruption
// after
try { var unpadded = padding.RemovePadding(plain); }
catch (ArgumentException) { /* wrong key or corrupted ciphertext */ }
Defensive patterns

Strategy: try-catch

Validate before calling

if (data == null || data.Length == 0 || data[^1] == 0)
    throw new CryptographicException("Data cannot have valid ISO 10126 padding");

Try / catch

try { plain = padding.RemovePadding(data); }
catch (ArgumentException) { throw new CryptographicException("Decryption failed: bad padding"); }

Prevention

When it happens

Trigger: Calling RemovePadding on decrypted output whose final byte is 0x00 or exceeds the data length; wrong decryption key, wrong cipher mode, or truncated/truncated-then-modified ciphertext.

Common situations: Key mismatches between encrypt/decrypt sides, ciphertext corrupted in transit, or applying unpadding to data that was never padded (e.g. raw plaintext passed by mistake).

Related errors


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

Appendix: source

Thrown at Algorithms/Crypto/Paddings/Iso10126D2Padding.cs:74

    /// </summary>
    /// <param name="inputData">
    /// The input data with ISO10126d2 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 when the padding length is invalid.
    /// </exception>
    public byte[] RemovePadding(byte[] inputData)
    {
        // Get the size of the padding from the last byte of the input data.
        var paddingLength = inputData[^1];

        // Check if the padding size is valid.
        if (paddingLength < 1 || paddingLength > inputData.Length)
        {
            throw new ArgumentException("Invalid padding length");
        }

        // Create a new array to hold the original data.
        var output = new byte[inputData.Length - paddingLength];

        // Copy the original data into the new array.
        Array.Copy(inputData, 0, output, 0, output.Length);

        return output;
    }

    /// <summary>
    /// Gets the number of padding bytes from the input data array.
    /// </summary>
    /// <param name="input">The input data array that has been padded.</param>
    /// <returns>The number of padding bytes.</returns>
    /// <exception cref="ArgumentNullException">Thrown when the input is null.</exception>
    /// <exception cref="ArgumentException">Thrown when the padding block is corrupted.</exception>

View on GitHub (pinned to 96e2905cab)