{"record":{"id":"21944006c5991f88","repo":"TheAlgorithms/C-Sharp","slug":"invalid-padding-length","errorCode":null,"errorMessage":"Invalid padding length","messagePattern":"Invalid padding length","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Algorithms/Crypto/Paddings/Iso10126D2Padding.cs","lineNumber":74,"sourceCode":"    /// </summary>\n    /// <param name=\"inputData\">\n    /// The input data with ISO10126d2 padding. Must not be null and must have a valid length and padding.\n    /// </param>\n    /// <returns>\n    /// The input data without the padding as a new byte array.\n    /// </returns>\n    /// <exception cref=\"ArgumentException\">\n    /// Thrown when the padding length is invalid.\n    /// </exception>\n    public byte[] RemovePadding(byte[] inputData)\n    {\n        // Get the size of the padding from the last byte of the input data.\n        var paddingLength = inputData[^1];\n\n        // Check if the padding size is valid.\n        if (paddingLength < 1 || paddingLength > inputData.Length)\n        {\n            throw new ArgumentException(\"Invalid padding length\");\n        }\n\n        // Create a new array to hold the original data.\n        var output = new byte[inputData.Length - paddingLength];\n\n        // Copy the original data into the new array.\n        Array.Copy(inputData, 0, output, 0, output.Length);\n\n        return output;\n    }\n\n    /// <summary>\n    /// Gets the number of padding bytes from the input data array.\n    /// </summary>\n    /// <param name=\"input\">The input data array that has been padded.</param>\n    /// <returns>The number of padding bytes.</returns>\n    /// <exception cref=\"ArgumentNullException\">Thrown when the input is null.</exception>\n    /// <exception cref=\"ArgumentException\">Thrown when the padding block is corrupted.</exception>","sourceCodeStart":56,"sourceCodeEnd":92,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/Algorithms/Crypto/Paddings/Iso10126D2Padding.cs#L56-L92","documentation":"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.","triggerScenarios":"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.","commonSituations":"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).","solutions":["Verify the decryption key, IV and cipher mode match those used for encryption","Confirm the data actually went through ISO 10126 padding before unpadding","Treat as possible ciphertext tampering — use authenticated encryption (e.g. GCM) to detect corruption instead of relying on padding errors"],"exampleFix":"// before\nvar plain = cipher.DoFinal(ciphertext);\nvar unpadded = padding.RemovePadding(plain); // throws on corruption\n// after\ntry { var unpadded = padding.RemovePadding(plain); }\ncatch (ArgumentException) { /* wrong key or corrupted ciphertext */ }","handlingStrategy":"try-catch","validationCode":"if (data == null || data.Length == 0 || data[^1] == 0)\n    throw new CryptographicException(\"Data cannot have valid ISO 10126 padding\");","typeGuard":null,"tryCatchPattern":"try { plain = padding.RemovePadding(data); }\ncatch (ArgumentException) { throw new CryptographicException(\"Decryption failed: bad padding\"); }","preventionTips":["Match keys, IVs and modes on both sides","Prefer authenticated encryption over padding-based integrity","Never apply unpadding to data that skipped AddPadding"],"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"}