ISO/IEC 7816-4 and Other Padding Methods
What you'll learn: How ISO/IEC 7816-4, ANSI X.923, and zero padding work as alternatives to PKCS#7, and when each method makes sense.
Understanding Alternative Padding Schemes
You've already learned PKCS#7, the most common padding scheme. But it's not the only way to fill those last block-sized gaps before encryption. Different industries and systems developed their own padding methods for specific needs.
ISO/IEC 7816-4 Padding
Also called "ISO 9797-1 Method 2," this scheme always adds at least one byte. It starts with a mandatory 0x80 byte (binary 10000000), followed by zero or more 0x00 bytes to reach the block boundary.
Example: If you need 5 padding bytes:
0x80 0x00 0x00 0x00 0x00
Think of it like placing a marker flag (0x80) at the data's end, then filling with zeros. The flag always tells you exactly where real data stops.
ANSI X.923 Padding
This scheme fills with zeros, but stores the padding length in the final byte.
Example: For 5 padding bytes:
0x00 0x00 0x00 0x00 0x05
It's like PKCS#7's cousin—the last byte still tells you the count, but the rest is zeros instead of repeating values.
Zero Padding
The simplest (and riskiest): just add 0x00 bytes until you hit the block size. Problem: if your real data ends in zeros, you can't distinguish padding from data during removal. Only use this when data length is transmitted separately or data never ends in zero.
When to Use Each
- ISO/IEC 7816-4: Smart cards and cryptographic tokens (where it originated)
- ANSI X.923: Some Microsoft systems and legacy protocols
- Zero padding: Fixed-length messages or when combined with explicit length fields
Key Takeaway: While PKCS#7 dominates modern systems, ISO/IEC 7816-4, ANSI X.923, and zero padding serve specialized use cases—choose based on your protocol requirements and compatibility needs, never zero padding alone for variable-length data.