{"record":{"id":"a53ba494f18e288b","repo":"TheAlgorithms/C-Sharp","slug":"invalid-padding","errorCode":null,"errorMessage":"Invalid padding","messagePattern":"Invalid padding","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Algorithms/Crypto/Paddings/Iso7816D4Padding.cs","lineNumber":90,"sourceCode":"    /// <returns>The input data without the padding as a new byte array.</returns>\n    /// <exception cref=\"ArgumentException\">\n    /// Thrown when the input data has invalid padding.\n    /// </exception>\n    public byte[] RemovePadding(byte[] inputData)\n    {\n        // Find the index of the first padding byte by scanning from the end of the input.\n        var paddingIndex = inputData.Length - 1;\n\n        // Skip all the padding bytes that are 0.\n        while (paddingIndex >= 0 && inputData[paddingIndex] == 0)\n        {\n            paddingIndex--;\n        }\n\n        // Check if the first padding byte is 0x80.\n        if (paddingIndex < 0 || inputData[paddingIndex] != 0x80)\n        {\n            throw new ArgumentException(\"Invalid padding\");\n        }\n\n        // Create a new array to store the unpadded data.\n        var unpaddedData = new byte[paddingIndex];\n\n        // Copy the unpadded data from the input data to the new array.\n        Array.Copy(inputData, 0, unpaddedData, 0, paddingIndex);\n\n        // Return the unpadded data array.\n        return unpaddedData;\n    }\n\n    /// <summary>\n    /// Gets the number of padding bytes in the input data according to the ISO 7816-4 standard.\n    /// </summary>\n    /// <param name=\"input\">The input data array that has padding.</param>\n    /// <returns>The number of padding bytes in the input data.</returns>\n    /// <exception cref=\"ArgumentException\"> Thrown when the input data has invalid padding.</exception>","sourceCodeStart":72,"sourceCodeEnd":108,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/Algorithms/Crypto/Paddings/Iso7816D4Padding.cs#L72-L108","documentation":"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.","triggerScenarios":"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).","commonSituations":"Mixing padding schemes between encrypt and decrypt sides (PKCS7 vs ISO7816-4); wrong decryption key yielding random trailing bytes with no 0x80.","solutions":["Use the same IPadding implementation for both AddPadding and RemovePadding","Verify decryption key/IV correctness — garbage plaintext will not contain the 0x80 marker","Validate the data format before unpadding when input provenance is uncertain"],"exampleFix":"// before\nvar out1 = iso7816Padding.RemovePadding(pkcs7Decrypted); // wrong scheme\n// after\nvar out1 = pkcs7Padding.RemovePadding(pkcs7Decrypted);\nvar out2 = iso7816Padding.RemovePadding(iso7816Decrypted);","handlingStrategy":"try-catch","validationCode":"bool hasMarker = data != null && data.AsSpan().LastIndexOf((byte)0x80) >= 0;\nif (!hasMarker) throw new CryptographicException(\"No ISO 7816-4 padding marker\");","typeGuard":null,"tryCatchPattern":"try { plain = padding.RemovePadding(data); }\ncatch (ArgumentException) { throw new CryptographicException(\"Invalid padding\"); }","preventionTips":["Use the identical IPadding implementation for pad and unpad","Verify keys before blaming the padding scheme","Document which padding each stored ciphertext uses"],"tags":["csharp","crypto","padding","decryption"],"backgroundTag":"invalid-padding-length","analyzedSha":"96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c","analyzedAt":"2026-09-13T17:04:01.438Z","contentChangedAt":"2026-09-13T17:04:01.438Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}