{"record":{"id":"d6bb94b1fce1be35","repo":"TheAlgorithms/C-Sharp","slug":"no-padding-found","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":128,"sourceCode":"    /// <summary>\n    /// Returns the number of padding bytes in a byte array according to the Trailing-Bit-Complement padding algorithm.\n    /// </summary>\n    /// <param name=\"input\">The byte array to check for padding.</param>\n    /// <returns>The number of padding bytes in the input array.</returns>\n    /// <remarks>\n    /// This method assumes that the input array has been padded with either 0x00 or 0xFF bytes, depending on the last\n    /// bit of the original data. The method works by iterating backwards from the end of the array and counting the\n    /// number of bytes that match the padding code. The method uses bitwise operations to optimize the performance and\n    /// avoid branching. If the input array is not padded or has an invalid padding, the method may return incorrect\n    /// results.\n    /// </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","sourceCodeStart":110,"sourceCodeEnd":146,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/Algorithms/Crypto/Paddings/TbcPadding.cs#L110-L146","documentation":"TBC (Trailing-Bit-Complement) padding is defined over at least one byte, so GetPaddingCount rejects empty arrays with ArgumentException: there is no last byte whose value could encode the padding. An empty input cannot have TBC padding by definition.","triggerScenarios":"Calling GetPaddingCount(new byte[0]) or Array.Empty<byte>() — e.g. decryption returned an empty buffer, or an empty chunk was passed through the unpadding pipeline.","commonSituations":"Empty input files or network messages being processed as if they contained a block; a failed decrypt returning an empty array; chunked pipelines forwarding an empty final chunk.","solutions":["Guard against empty arrays before calling GetPaddingCount.","Treat an empty buffer as an upstream error and throw/log where it originated.","Verify the decrypt stage produced at least one block of data.","Skip unpadding for empty payloads if your protocol defines them as valid."],"exampleFix":"// before\nvar count = padding.GetPaddingCount(chunk); // chunk may be empty\n// after\nif (chunk.Length == 0) throw new InvalidOperationException(\"Received empty block\");\nvar count = padding.GetPaddingCount(chunk);","handlingStrategy":"validation","validationCode":"if (input is null || input.Length == 0)\n    throw new InvalidOperationException(\"Cannot unpad an empty buffer — upstream produced no data\");","typeGuard":"static bool IsNonEmptyBlock(byte[]? input) => input is { Length: > 0 };","tryCatchPattern":"try { count = padding.GetPaddingCount(input); }\ncatch (ArgumentException) when (input.Length == 0) { /* skip empty chunk */ }","preventionTips":["Filter empty chunks out of streaming pipelines before unpadding.","Investigate why a decrypt stage emitted an empty buffer.","Define explicit protocol handling for empty payloads."],"tags":["csharp","cryptography","empty-input","padding"],"backgroundTag":"empty-required-field","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"}