TheAlgorithms/C-Sharp · error · ArgumentException
Pad block corrupted
Error message
Pad block corrupted
What it means
X932Padding.GetPaddingCount derives the padding count from the last byte of the input and validates it with a constant-time bitwise check: position = input.Length - count must be positive and count - 1 must be non-negative. If either fails (count is 0, or the padding is longer than the block), it throws this ArgumentException indicating a corrupted or non-X9.32 pad block.
Solutions
- Confirm you are passing decrypted plaintext (not ciphertext) to GetPaddingCount.
- Check the decryption key/IV; random-looking last bytes indicate a key mismatch.
- Verify the block passed in is complete and exactly one cipher block long.
- Catch ArgumentException and treat it as a padding-oracle condition: return a generic decryption-failure to callers.
Example fix
// before
int padLen = padding.GetPaddingCount(decryptedBlock); // throws 'Pad block corrupted'
// after
try
{
int padLen = padding.GetPaddingCount(decryptedBlock);
}
catch (ArgumentException)
{
throw new CryptographicException("Decryption failed: invalid padding");
} Defensive patterns
Strategy: try-catch
Validate before calling
int count = input[^1] & 0xFF; bool valid = count >= 1 && count <= input.Length;
Try / catch
try { int pad = padding.GetPaddingCount(block); }
catch (ArgumentException) { throw new CryptographicException("Decryption failed: corrupted pad block"); } Prevention
- Only call GetPaddingCount on decrypted plaintext, never on ciphertext
- Uniformly convert padding failures into a generic decryption error to avoid padding-oracle leaks
- Confirm key and IV correctness before blaming the padding
When it happens
Trigger: Calling GetPaddingCount on a block whose last byte is 0x00 (count = 0), or on a block shorter than the encoded padding count (e.g. last byte 0xFF on a 16-byte block). Any tampered or wrongly decrypted final block.
Common situations: Side-channel-safe padding checks during decryption of data encrypted with the wrong key; ciphertext bit-flipping/tampering; feeding ciphertext bytes instead of decrypted plaintext; blocks assembled in the wrong order.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- Invalid padding length
- Invalid padding length
- Padding block is corrupted
- Invalid padding
- Pad block corrupted
AI-assisted analysis of TheAlgorithms/C-Sharp@96e2905cab (2026-09-13).
Data as JSON: /api/errors/00d9b98b18fdbba3.
Report an issue: GitHub.
Appendix: source
Thrown at Algorithms/Crypto/Paddings/X932Padding.cs:128
/// <exception cref="ArgumentException">
/// Thrown when the input data has a corrupted padding block.
/// </exception>
public int GetPaddingCount(byte[] input)
{
// Get the last byte of the input data, which is the padding length.
var count = input[^1] & 0xFF;
// Calculate the position of the first padding byte.
var position = input.Length - count;
// Check if the position and count are valid using bitwise operations.
// If either of them is negative or zero, the result will be negative.
var failed = (position | (count - 1)) >> 31;
// Throw an exception if the result is negative.
if (failed != 0)
{
throw new ArgumentException("Pad block corrupted");
}
// Return the padding length.
return count;
}
}
View on GitHub (pinned to 96e2905cab)