{"record":{"id":"e1131f56d0941816","repo":"TheAlgorithms/C-Sharp","slug":"invalid-padding-length-x932padding","errorCode":null,"errorMessage":"Invalid padding length","messagePattern":"Invalid padding length","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Algorithms/Crypto/Paddings/X932Padding.cs","lineNumber":92,"sourceCode":"    /// <returns>The unpadded data array.</returns>\n    /// <exception cref=\"ArgumentException\">\n    /// Thrown when the input data is empty or has an invalid padding length.\n    /// </exception>\n    public byte[] RemovePadding(byte[] inputData)\n    {\n        // Check if the array is empty.\n        if (inputData.Length == 0)\n        {\n            return Array.Empty<byte>();\n        }\n\n        // Get the padding length from the last byte of the input data.\n        var paddingLength = inputData[^1];\n\n        // Check if the padding length is valid.\n        if (paddingLength < 1 || paddingLength > inputData.Length)\n        {\n            throw new ArgumentException(\"Invalid padding length\");\n        }\n\n        // Create a new array for the output data.\n        var output = new byte[inputData.Length - paddingLength];\n\n        // Copy the input data without the padding bytes to the output array.\n        Array.Copy(inputData, output, output.Length);\n\n        // Return the output array.\n        return output;\n    }\n\n    /// <summary>\n    /// Gets the number of padding bytes in the input data according to the X9.23 padding scheme.\n    /// </summary>\n    /// <param name=\"input\">The input data array to be checked.</param>\n    /// <returns>The number of padding bytes in the input data.</returns>\n    /// <exception cref=\"ArgumentException\">","sourceCodeStart":74,"sourceCodeEnd":110,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/Algorithms/Crypto/Paddings/X932Padding.cs#L74-L110","documentation":"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).","triggerScenarios":"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.","commonSituations":"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.","solutions":["Ensure the same IBlockCipherPadding instance/mode is used for both encryption and decryption.","Verify the decryption key and IV match those used for encryption; wrong keys are the most common cause.","Check that the full ciphertext was transmitted and the decrypted block passed to RemovePadding is complete and unmodified.","Catch ArgumentException around RemovePadding and treat it as an authentication/integrity failure of the ciphertext."],"exampleFix":"// before\nbyte[] plain = cipher.Decrypt(data);\nbyte[] msg = x932.RemovePadding(plain); // throws if last byte is 0 or > plain.Length\n\n// after\nbyte[] plain = cipher.Decrypt(data);\nif (plain.Length > 0 && plain[^1] is > 0 and <= plain.Length)\n{\n    byte[] msg = x932.RemovePadding(plain);\n}\nelse\n{\n    throw new CryptographicException(\"Data is not X9.32 padded (wrong key or corrupted data)\");\n}","handlingStrategy":"try-catch","validationCode":"bool plausiblyPadded = data.Length > 0 && data[^1] >= 1 && data[^1] <= data.Length;","typeGuard":null,"tryCatchPattern":"try { return padding.RemovePadding(data); }\ncatch (ArgumentException) { throw new CryptographicException(\"Invalid padding: wrong key, IV, or corrupted ciphertext\"); }","preventionTips":["Use the identical padding mode for encrypt and decrypt","Treat padding failures as integrity failures, not bugs — check keys/IV first","Validate ciphertext completeness before decryption (length is a positive multiple of the block size)"],"tags":["csharp","cryptography","padding","decryption","integrity"],"backgroundTag":"invalid-argument-value","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"}