TheAlgorithms/C-Sharp · error · ArgumentException

Invalid padding length

Error message

Invalid padding length

What it means

X932Padding.RemovePadding reads the last byte of the decrypted block as the padding length and strips that many bytes. It throws this ArgumentException when the last byte is 0 or greater than the input array length, because such a value cannot describe a valid X9.32 padding, meaning the data is not correctly padded (or was corrupted / decrypted with the wrong key).

Solutions

  1. Ensure the same IBlockCipherPadding instance/mode is used for both encryption and decryption.
  2. Verify the decryption key and IV match those used for encryption; wrong keys are the most common cause.
  3. Check that the full ciphertext was transmitted and the decrypted block passed to RemovePadding is complete and unmodified.
  4. Catch ArgumentException around RemovePadding and treat it as an authentication/integrity failure of the ciphertext.

Example fix

// before
byte[] plain = cipher.Decrypt(data);
byte[] msg = x932.RemovePadding(plain); // throws if last byte is 0 or > plain.Length

// after
byte[] plain = cipher.Decrypt(data);
if (plain.Length > 0 && plain[^1] is > 0 and <= plain.Length)
{
    byte[] msg = x932.RemovePadding(plain);
}
else
{
    throw new CryptographicException("Data is not X9.32 padded (wrong key or corrupted data)");
}
Defensive patterns

Strategy: try-catch

Validate before calling

bool plausiblyPadded = data.Length > 0 && data[^1] >= 1 && data[^1] <= data.Length;

Try / catch

try { return padding.RemovePadding(data); }
catch (ArgumentException) { throw new CryptographicException("Invalid padding: wrong key, IV, or corrupted ciphertext"); }

Prevention

When it happens

Trigger: Calling RemovePadding on data that was never padded, on data decrypted with a wrong key/IV (last byte is random), on truncated ciphertext blocks, or on data padded with a different scheme (e.g. PKCS#7 data whose last byte is 0 is fine, but empty result cases). Specifically: last byte == 0 or last byte > inputData.Length.

Common situations: Wrong decryption key or IV producing garbage plaintext; concatenating blocks incorrectly so the padding byte is lost; mixing X9.32 padding with another padding scheme between encrypt/decrypt; ciphertext truncated by a transport layer.

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/e1131f56d0941816. Report an issue: GitHub.

Appendix: source

Thrown at Algorithms/Crypto/Paddings/X932Padding.cs:92

    /// <returns>The unpadded data array.</returns>
    /// <exception cref="ArgumentException">
    /// Thrown when the input data is empty or has an invalid padding length.
    /// </exception>
    public byte[] RemovePadding(byte[] inputData)
    {
        // Check if the array is empty.
        if (inputData.Length == 0)
        {
            return Array.Empty<byte>();
        }

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

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

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

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

        // Return the output array.
        return output;
    }

    /// <summary>
    /// Gets the number of padding bytes in the input data according to the X9.23 padding scheme.
    /// </summary>
    /// <param name="input">The input data array to be checked.</param>
    /// <returns>The number of padding bytes in the input data.</returns>
    /// <exception cref="ArgumentException">

View on GitHub (pinned to 96e2905cab)