{"record":{"id":"00d9b98b18fdbba3","repo":"TheAlgorithms/C-Sharp","slug":"pad-block-corrupted-x932padding","errorCode":null,"errorMessage":"Pad block corrupted","messagePattern":"Pad block corrupted","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Algorithms/Crypto/Paddings/X932Padding.cs","lineNumber":128,"sourceCode":"    /// <exception cref=\"ArgumentException\">\n    /// Thrown when the input data has a corrupted padding block.\n    /// </exception>\n    public int GetPaddingCount(byte[] input)\n    {\n        // Get the last byte of the input data, which is the padding length.\n        var count = input[^1] & 0xFF;\n\n        // Calculate the position of the first padding byte.\n        var position = input.Length - count;\n\n        // Check if the position and count are valid using bitwise operations.\n        // If either of them is negative or zero, the result will be negative.\n        var failed = (position | (count - 1)) >> 31;\n\n        // Throw an exception if the result is negative.\n        if (failed != 0)\n        {\n            throw new ArgumentException(\"Pad block corrupted\");\n        }\n\n        // Return the padding length.\n        return count;\n    }\n}\n","sourceCodeStart":110,"sourceCodeEnd":135,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/Algorithms/Crypto/Paddings/X932Padding.cs#L110-L135","documentation":"X932Padding.GetPaddingCount derives the padding count from the last byte of the input and validates it with a constant-time bitwise check: position = input.Length - count must be positive and count - 1 must be non-negative. If either fails (count is 0, or the padding is longer than the block), it throws this ArgumentException indicating a corrupted or non-X9.32 pad block.","triggerScenarios":"Calling GetPaddingCount on a block whose last byte is 0x00 (count = 0), or on a block shorter than the encoded padding count (e.g. last byte 0xFF on a 16-byte block). Any tampered or wrongly decrypted final block.","commonSituations":"Side-channel-safe padding checks during decryption of data encrypted with the wrong key; ciphertext bit-flipping/tampering; feeding ciphertext bytes instead of decrypted plaintext; blocks assembled in the wrong order.","solutions":["Confirm you are passing decrypted plaintext (not ciphertext) to GetPaddingCount.","Check the decryption key/IV; random-looking last bytes indicate a key mismatch.","Verify the block passed in is complete and exactly one cipher block long.","Catch ArgumentException and treat it as a padding-oracle condition: return a generic decryption-failure to callers."],"exampleFix":"// before\nint padLen = padding.GetPaddingCount(decryptedBlock); // throws 'Pad block corrupted'\n\n// after\ntry\n{\n    int padLen = padding.GetPaddingCount(decryptedBlock);\n}\ncatch (ArgumentException)\n{\n    throw new CryptographicException(\"Decryption failed: invalid padding\");\n}","handlingStrategy":"try-catch","validationCode":"int count = input[^1] & 0xFF;\nbool valid = count >= 1 && count <= input.Length;","typeGuard":null,"tryCatchPattern":"try { int pad = padding.GetPaddingCount(block); }\ncatch (ArgumentException) { throw new CryptographicException(\"Decryption failed: corrupted pad block\"); }","preventionTips":["Only call GetPaddingCount on decrypted plaintext, never on ciphertext","Uniformly convert padding failures into a generic decryption error to avoid padding-oracle leaks","Confirm key and IV correctness before blaming the padding"],"tags":["csharp","cryptography","padding","decryption","tampering"],"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"}