{"record":{"id":"2a637d55c8c0c09a","repo":"TheAlgorithms/C-Sharp","slug":"no-padding-found-tbcpadding","errorCode":null,"errorMessage":"No padding found","messagePattern":"No padding found","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Algorithms/Crypto/Paddings/TbcPadding.cs","lineNumber":139,"sourceCode":"    /// </remarks>\n    public int GetPaddingCount(byte[] input)\n    {\n        var length = input.Length;\n\n        if (length == 0)\n        {\n            throw new ArgumentException(\"No padding found.\");\n        }\n\n        // Get the value of the last byte as the padding value\n        var paddingValue = input[--length] & 0xFF;\n        var paddingCount = 1; // Start count at 1 for the last byte\n        var countingMask = -1; // Initialize counting mask\n\n        // Check if there is no padding\n        if (paddingValue != 0 && paddingValue != 0xFF)\n        {\n            throw new ArgumentException(\"No padding found\");\n        }\n\n        // Loop backwards through the array\n        for (var i = length - 1; i >= 0; i--)\n        {\n            var currentByte = input[i] & 0xFF;\n\n            // Calculate matchMask. If currentByte equals paddingValue, matchMask will be 0, otherwise -1\n            var matchMask = ((currentByte ^ paddingValue) - 1) >> 31;\n\n            // Update countingMask. Once a non-matching byte is found, countingMask will remain -1\n            countingMask &= matchMask;\n\n            // Increment count only if countingMask is 0 (i.e., currentByte matches paddingValue)\n            paddingCount -= countingMask;\n        }\n\n        return paddingCount;","sourceCodeStart":121,"sourceCodeEnd":157,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/Algorithms/Crypto/Paddings/TbcPadding.cs#L121-L157","documentation":"In TBC padding the trailing bytes must start with either 0x00 or 0xFF (the complement pattern). If the last byte is neither, the data has no recognizable TBC padding, so GetPaddingCount throws ArgumentException. This typically means the data wasn't TBC-padded or was corrupted/decrypted incorrectly.","triggerScenarios":"Calling GetPaddingCount on data whose last byte is neither 0x00 nor 0xFF — unpadded plaintext, data padded with PKCS7/ANSI X9.23 instead, wrong-key decryption garbage.","commonSituations":"Mismatched padding schemes between encryptor and decryptor; wrong key or cipher mode; processing data from a peer that uses a different padding standard.","solutions":["Ensure the same TBC padding was applied at encryption time.","Verify decryption key, IV, and mode match the encryptor.","Decrypt fully before unpadding; never inspect ciphertext.","Detect the actual padding scheme used by the peer and use the matching padder."],"exampleFix":"// before\nvar tbc = new TbcPadding();\nvar n = tbc.GetPaddingCount(pkcs7Data); // wrong scheme\n// after\nvar pkcs7 = new Pkcs7Padding(blockSize);\nvar n = pkcs7.RemovePadding(decrypted);","handlingStrategy":"validation","validationCode":"var last = input[^1] & 0xFF;\nif (last != 0x00 && last != 0xFF)\n    throw new InvalidOperationException(\"Data does not use TBC padding — check the encryption-side scheme\");","typeGuard":null,"tryCatchPattern":"try { count = tbc.GetPaddingCount(decrypted); }\ncatch (ArgumentException) { throw new CryptographicException(\"No TBC padding found — scheme or key mismatch\"); }","preventionTips":["Use one padding scheme end-to-end and record it in protocol metadata.","Verify key/IV/mode consistency across implementations.","Decrypt before inspecting padding bytes."],"tags":["csharp","cryptography","padding","scheme-mismatch"],"backgroundTag":"invalid-padding","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"}