TheAlgorithms/C-Sharp · error · ArgumentException

No padding found

Error message

No padding found

What it means

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.

Solutions

  1. Ensure the same TBC padding was applied at encryption time.
  2. Verify decryption key, IV, and mode match the encryptor.
  3. Decrypt fully before unpadding; never inspect ciphertext.
  4. Detect the actual padding scheme used by the peer and use the matching padder.

Example fix

// before
var tbc = new TbcPadding();
var n = tbc.GetPaddingCount(pkcs7Data); // wrong scheme
// after
var pkcs7 = new Pkcs7Padding(blockSize);
var n = pkcs7.RemovePadding(decrypted);
Defensive patterns

Strategy: validation

Validate before calling

var last = input[^1] & 0xFF;
if (last != 0x00 && last != 0xFF)
    throw new InvalidOperationException("Data does not use TBC padding — check the encryption-side scheme");

Try / catch

try { count = tbc.GetPaddingCount(decrypted); }
catch (ArgumentException) { throw new CryptographicException("No TBC padding found — scheme or key mismatch"); }

Prevention

When it happens

Trigger: 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.

Common situations: Mismatched padding schemes between encryptor and decryptor; wrong key or cipher mode; processing data from a peer that uses a different padding standard.

Related errors


AI-assisted analysis of TheAlgorithms/C-Sharp@96e2905cab (2026-09-13). Data as JSON: /api/errors/2a637d55c8c0c09a. Report an issue: GitHub.

Appendix: source

Thrown at Algorithms/Crypto/Paddings/TbcPadding.cs:139

    /// </remarks>
    public int GetPaddingCount(byte[] input)
    {
        var length = input.Length;

        if (length == 0)
        {
            throw new ArgumentException("No padding found.");
        }

        // Get the value of the last byte as the padding value
        var paddingValue = input[--length] & 0xFF;
        var paddingCount = 1; // Start count at 1 for the last byte
        var countingMask = -1; // Initialize counting mask

        // Check if there is no padding
        if (paddingValue != 0 && paddingValue != 0xFF)
        {
            throw new ArgumentException("No padding found");
        }

        // Loop backwards through the array
        for (var i = length - 1; i >= 0; i--)
        {
            var currentByte = input[i] & 0xFF;

            // Calculate matchMask. If currentByte equals paddingValue, matchMask will be 0, otherwise -1
            var matchMask = ((currentByte ^ paddingValue) - 1) >> 31;

            // Update countingMask. Once a non-matching byte is found, countingMask will remain -1
            countingMask &= matchMask;

            // Increment count only if countingMask is 0 (i.e., currentByte matches paddingValue)
            paddingCount -= countingMask;
        }

        return paddingCount;

View on GitHub (pinned to 96e2905cab)