{"record":{"id":"7c4e987aea074d6f","repo":"TheAlgorithms/C-Sharp","slug":"padding-block-is-corrupted","errorCode":null,"errorMessage":"Padding block is corrupted","messagePattern":"Padding block is corrupted","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Algorithms/Crypto/Paddings/Iso10126D2Padding.cs","lineNumber":119,"sourceCode":"        var lastByte = input[^1];\n        var paddingCount = lastByte & 0xFF;\n\n        // Calculate the index where the padding starts.\n        var paddingStartIndex = input.Length - paddingCount;\n        var paddingCheckFailed = 0;\n\n        // The paddingCheckFailed will be non-zero under the following circumstances:\n        // 1. When paddingStartIndex is negative: This happens when paddingCount (the last byte of the input array) is\n        // greater than the length of the input array. In other words, the padding count is claiming that there are more\n        // padding bytes than there are bytes in the array, which is not a valid scenario.\n        // 2. When paddingCount - 1 is negative: This happens when paddingCount is zero or less. Since paddingCount\n        // represents the number of padding bytes and is derived from the last byte of the input array, it should always\n        // be a positive number. If it's zero or less, it means that either there's no padding, or an invalid negative\n        // padding count has shomehow encoded into the last byte of the input array.\n        paddingCheckFailed = (paddingStartIndex | (paddingCount - 1)) >> 31;\n        if (paddingCheckFailed != 0)\n        {\n            throw new ArgumentException(\"Padding block is corrupted\");\n        }\n\n        return paddingCount;\n    }\n}\n","sourceCodeStart":101,"sourceCodeEnd":125,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/Algorithms/Crypto/Paddings/Iso10126D2Padding.cs#L101-L125","documentation":"GetPaddingCount performs a constant-time validity check on the padding: the arithmetic mask paddingCheckFailed is non-zero when the computed padding start index is negative (padding count zero or inconsistent with the data), and throws ArgumentException. This signals a corrupted or forged padding block — usually wrong key, corrupted ciphertext, or malicious input.","triggerScenarios":"Input whose last byte is 0x00, or whose derived paddingStartIndex is negative; occurs during unpadding of wrongly decrypted or tampered data.","commonSituations":"Wrong decryption key/IV, bit-flipped ciphertext, or padding-oracle probing attempts against a non-authenticated cipher setup.","solutions":["Verify key/IV/mode parity between encrypt and decrypt sides","Switch to authenticated encryption so corruption is detected before unpadding","Catch ArgumentException around unpadding and return a generic 'decryption failed' to avoid padding-oracle leaks"],"exampleFix":"// before\nvar count = padding.GetPaddingCount(data);\n// after\ntry { var count = padding.GetPaddingCount(data); }\ncatch (ArgumentException) { throw new CryptographicException(\"Decryption failed\"); }","handlingStrategy":"try-catch","validationCode":"if (input == null || input.Length == 0 || input[^1] == 0)\n    throw new CryptographicException(\"Input cannot have valid padding\");","typeGuard":null,"tryCatchPattern":"try { count = padding.GetPaddingCount(data); }\ncatch (ArgumentException) { throw new CryptographicException(\"Decryption failed\"); }","preventionTips":["Use AEAD modes so corruption is detected before unpadding","Return a single generic error on padding failure to avoid padding oracles","Verify key/IV consistency in integration tests"],"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"}