TheAlgorithms/C-Sharp · error · ArgumentException

Invalid padding

Error message

Invalid padding

What it means

RemovePadding scans backwards for the 0x80 ISO 7816-4 marker; if no marker exists (paddingIndex < 0) or the byte at the marker position is not 0x80, it throws ArgumentException('Invalid padding'). This indicates the unpadded data is not valid ISO 7816-4 padded data.

Solutions

  1. Use the same IPadding implementation for both AddPadding and RemovePadding
  2. Verify decryption key/IV correctness — garbage plaintext will not contain the 0x80 marker
  3. Validate the data format before unpadding when input provenance is uncertain

Example fix

// before
var out1 = iso7816Padding.RemovePadding(pkcs7Decrypted); // wrong scheme
// after
var out1 = pkcs7Padding.RemovePadding(pkcs7Decrypted);
var out2 = iso7816Padding.RemovePadding(iso7816Decrypted);
Defensive patterns

Strategy: try-catch

Validate before calling

bool hasMarker = data != null && data.AsSpan().LastIndexOf((byte)0x80) >= 0;
if (!hasMarker) throw new CryptographicException("No ISO 7816-4 padding marker");

Try / catch

try { plain = padding.RemovePadding(data); }
catch (ArgumentException) { throw new CryptographicException("Invalid padding"); }

Prevention

When it happens

Trigger: Calling RemovePadding on data that never had ISO 7816-4 padding applied; data whose trailing bytes contain no 0x80 marker (e.g. wrong key produced garbage, or a different padding scheme like PKCS#7 was used).

Common situations: Mixing padding schemes between encrypt and decrypt sides (PKCS7 vs ISO7816-4); wrong decryption key yielding random trailing bytes with no 0x80.

Related errors


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

Appendix: source

Thrown at Algorithms/Crypto/Paddings/Iso7816D4Padding.cs:90

    /// <returns>The input data without the padding as a new byte array.</returns>
    /// <exception cref="ArgumentException">
    /// Thrown when the input data has invalid padding.
    /// </exception>
    public byte[] RemovePadding(byte[] inputData)
    {
        // Find the index of the first padding byte by scanning from the end of the input.
        var paddingIndex = inputData.Length - 1;

        // Skip all the padding bytes that are 0.
        while (paddingIndex >= 0 && inputData[paddingIndex] == 0)
        {
            paddingIndex--;
        }

        // Check if the first padding byte is 0x80.
        if (paddingIndex < 0 || inputData[paddingIndex] != 0x80)
        {
            throw new ArgumentException("Invalid padding");
        }

        // Create a new array to store the unpadded data.
        var unpaddedData = new byte[paddingIndex];

        // Copy the unpadded data from the input data to the new array.
        Array.Copy(inputData, 0, unpaddedData, 0, paddingIndex);

        // Return the unpadded data array.
        return unpaddedData;
    }

    /// <summary>
    /// Gets the number of padding bytes in the input data according to the ISO 7816-4 standard.
    /// </summary>
    /// <param name="input">The input data array that has padding.</param>
    /// <returns>The number of padding bytes in the input data.</returns>
    /// <exception cref="ArgumentException"> Thrown when the input data has invalid padding.</exception>

View on GitHub (pinned to 96e2905cab)