{"record":{"id":"29cd5e8060594640","repo":"TheAlgorithms/C-Sharp","slug":"pad-block-corrupted","errorCode":null,"errorMessage":"Pad block corrupted","messagePattern":"Pad block corrupted","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Algorithms/Crypto/Paddings/Iso7816D4Padding.cs","lineNumber":146,"sourceCode":"\n            // Compute a mask to indicate if the current byte is 0x80.\n            var isPaddingStartMask = ((currentByte ^ 0x80) - 1) >> 31;\n\n            // Update the index of the first padding byte using bitwise operations.\n            // If the current byte is 0x80 and still part of the padding, set the index to the current index.\n            // Otherwise, keep the previous index.\n            paddingStartIndex ^= (currentIndex ^ paddingStartIndex) & (stillPaddingMask & isPaddingStartMask);\n\n            // Update the mask to indicate if the current byte is still part of the padding using bitwise operations.\n            // If the current byte is 0x00, keep the previous mask.\n            // Otherwise, set the mask to 0.\n            stillPaddingMask &= isZeroMask;\n        }\n\n        // Check if the index of the first padding byte is valid.\n        if (paddingStartIndex < 0)\n        {\n            throw new ArgumentException(\"Pad block corrupted\");\n        }\n\n        // Return the number of padding bytes.\n        return input.Length - paddingStartIndex;\n    }\n}\n","sourceCodeStart":128,"sourceCodeEnd":153,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/Algorithms/Crypto/Paddings/Iso7816D4Padding.cs#L128-L153","documentation":"GetPaddingCount runs a constant-time scan for the first padding byte and throws ArgumentException('Pad block corrupted') when the computed paddingStartIndex is negative — meaning no valid 0x80 marker was found or the padding structure is inconsistent. Like the RemovePadding check, it detects corrupted or forged data without leaking timing information.","triggerScenarios":"Input containing no 0x80 padding marker anywhere valid; decrypted garbage from a wrong key; tampered ciphertext during constant-time unpadding.","commonSituations":"Padding-oracle probing, ciphertext corruption in transit, key/mode mismatch producing plaintext without the marker byte.","solutions":["Confirm matching padding schemes and keys on both sides","Adopt authenticated encryption to reject corrupted ciphertext before unpadding","Catch ArgumentException and surface a uniform cryptographic-failure error to callers"],"exampleFix":"// before\nvar count = padding.GetPaddingCount(decrypted);\n// after\ntry { var count = padding.GetPaddingCount(decrypted); }\ncatch (ArgumentException) { throw new CryptographicException(\"Bad padding\"); }","handlingStrategy":"try-catch","validationCode":"if (input == null || input.Length == 0)\n    throw new CryptographicException(\"Nothing to unpad\");","typeGuard":null,"tryCatchPattern":"try { count = padding.GetPaddingCount(input); }\ncatch (ArgumentException) { throw new CryptographicException(\"Pad block corrupted\"); }","preventionTips":["Prefer authenticated encryption to catch corruption earlier","Treat padding failures uniformly to resist padding-oracle attacks","Round-trip test every padding scheme you support"],"tags":["csharp","crypto","padding","tampering"],"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"}