Payment Cryptography, Hands-On
Module 1 · Foundations

The cryptographer’s toolbox

Payment cryptography is old-fashioned on purpose. Almost everything is built from four tools: XOR (exclusive OR), a block cipher, a mode of operation, and a MAC (Message Authentication Code). Learn these and every later module is just “these four, arranged differently”.

By the end you can
  • read and XOR hex strings without fear;
  • explain what 2-key 3DES is and why it still exists;
  • tell ECB from CBC and say when each is used;
  • explain what a MAC proves and what it doesn’t.

1. Bytes, hex and why everything looks like 0123456789ABCDEF

Keys are just random bytes. A byte is 8 bits, written as two hex digits (00–FF). A “double-length 3DES (Triple DES) key” is 16 bytes = 32 hex digits; an AES-256 key is 32 bytes = 64 hex digits. When someone reads out a key component in a ceremony, they read 32 hex characters, usually in groups of four.

2. XOR — the most important operation in this course

XOR (⊕) compares two bits: the result is 1 when they differ. Three properties do all the work:

  • A ⊕ B ⊕ B = A — XOR with the same thing twice undoes it, because B ⊕ B = 0 and A ⊕ 0 = A. The first ⊕B hides A (“encrypts”), the second ⊕B with the same B reveals it again (“decrypts”). One bit: 1 ⊕ 1 = 0, then 0 ⊕ 1 = 1.
  • A ⊕ random = random-looking — if B is truly random and secret, A ⊕ B reveals nothing about A (this is the one-time pad).
  • Order doesn’t matter — A ⊕ B ⊕ C = C ⊕ A ⊕ B.

That is why keys are split into components combined by XOR: each component alone is random noise; all of them together give the key. Module 3 builds on this.

Why “most important”? Not because XOR is a cipher on its own — it isn’t. It is the glue that appears at every step around the ciphers:

  • Key components (module 3): ZMK (Zone Master Key) = C1 ⊕ C2 ⊕ C3.
  • CBC (Cipher Block Chaining) mode and MACs (below): each block is XORed with the previous ciphertext block — the basis of TR-31 (ANSI X9 Technical Report 31, the key block format) and ARQC (Authorization Request Cryptogram).
  • PIN (Personal Identification Number) blocks (module 7): ISO (International Organization for Standardization) format 0 = PIN field ⊕ PAN (Primary Account Number, the card number) field.
  • EMV (Europay, Mastercard, Visa — the chip card standard) key derivation (module 6): the right half of a card key comes from encrypting PAN ⊕ FF…FF.
  • Inside DES (Data Encryption Standard) and AES (Advanced Encryption Standard): the only place the secret key touches the data.
In the book §2.3.1 Cifrado de Vernam (p. 61–63): why XOR with a random key is perfectly secret.

3. Block ciphers: DES, 3DES, AES

A block cipher takes a key and a fixed-size block and returns a scrambled block of the same size. Without the key you can’t reverse it; with the key you can. The block is how many bytes it processes in one go — 8 for DES/3DES, 16 for AES — and has nothing to do with the key length. Longer data is cut into blocks (see Modes below); a PIN block is exactly 8 bytes because it is one 3DES block.

CipherBlockKeyStatus in payments
DES8 bytes8 bytes (56 bits used)Broken alone. Survives inside 3DES.
3DES (TDES, TDEA)8 bytes16 bytes (2-key) or 24 (3-key)Still everywhere: CVV, EMV, PIN blocks, KCVs. Being phased out.
AES16 bytes16, 24 or 32 bytesThe future: TR-31 version D, PIN format 4, new key exchanges.

Where XOR fits in

Why not just encrypt with data ⊕ key? Because the moment an attacker sees one pair of data and ciphertext, data ⊕ ciphertext is the key. A block cipher like DES still uses XOR — in every one of its 16 rounds, half the block is XORed with a round key — but between the XORs it pushes the bits through S-boxes (lookup tables that substitute 6 bits with 4 in a non-linear way) and shuffles them. Those tables (permutations and S-boxes) are fixed and public — the same in every textbook, so anyone can apply or undo them. The XOR with the round key is the only place the secret enters. The tables make the scrambling complicated; the XOR makes it secret. After 16 rounds, known data and ciphertext no longer give away the key, and flipping one input bit changes about half of the output bits.

Below is the full block cipher lab. Think of it as a sealed box: key + 8-byte block in, scrambled 8-byte block out; with the same key, Decrypt gives the block back. Try key 0123456789ABCDEFFEDCBA9876543210 with data 0000000000000000: you get 08D7B4FB629D0885 — remember 08D7B4, it comes back in module 3.

3DES = DES three times: encrypt with K1, decrypt with K2, encrypt with K3 (“EDE”). A 16-byte “2-key” 3DES key is K1‖K2 with K3 = K1. If K1 = K2, the first two steps cancel and you get plain single DES — that’s deliberate, so old single-DES devices could talk to new ones.

Modes: ECB and CBC

ECB (Electronic Codebook) encrypts each block independently. Identical plaintext blocks give identical ciphertext blocks — bad for long data, fine for a single random block like a key or a PIN block. Payment systems use ECB a lot, precisely because they mostly encrypt one block at a time.

CBC XORs each block with the previous ciphertext block before encrypting (the first with an IV). Same blocks no longer look the same. Key blocks (TR-31) and MACs are built on CBC.

Try it: put 00000000000000000000000000000000 (two identical blocks) into the lab, encrypt with ECB, then with CBC.

In the book §2.4.1 DES y AES and §2.4.2 Modos de operación (p. 68–80). This is the most useful chapter for this course.

4. Parity bits

A DES key is 8 bytes, but DES reads only 7 bits of each byte. The last bit of every byte is skipped by the cipher — so it is used as a check digit, like the last digit of a card number (Luhn).

13 = 0001 0011     7 bits go into DES, the last bit is skipped
     three 1s → odd → OK

The rule (“odd parity”): every byte must contain an odd number of 1-bits; the last bit is set to make that true. If someone typing a key component writes 17 (0001 0111, four 1s — even) instead of 13, the HSM (Hardware Security Module) rejects it with “wrong parity”: almost certainly a typo.

Two consequences:

  • Fixing parity changes nothing. 12 and 13 differ only in the skipped bit, so to DES they are the same key — same results, same KCV (Key Check Value).
  • “64-bit” DES is really 56-bit: 8 bytes × 7 bits.

5. MACs: proof that data wasn’t changed

Encryption hides data; it doesn’t stop someone from changing it. A MAC (message authentication code) is a short tag computed from the data and a secret key. Change one bit of the data and the tag changes completely; without the key you can’t produce a valid tag.

  • CBC-MAC / CMAC (Cipher-based MAC): CBC-encrypt the message, keep the last block. CMAC fixes CBC-MAC’s weaknesses and is what TR-31 and AES KCVs use.
  • Retail MAC (ISO 9797-1 algorithm 3): single DES CBC with the left key half, then a 3DES finish. EMV chip cryptograms (ARQC) are retail MACs.

Keep this in mind for later: an ARQC is a MAC the card computes over the transaction. A CVV (Card Verification Value) is a MAC over the card data, cut down to 3 digits.

In the book §2.2 Códigos de autenticación de mensajes (p. 56–61).

Exercises

How many hex characters does a double-length (2-key) 3DES key have?

Hint2-key = 16 bytes; each byte is two hex digits.
Solution32.
HintUse the XOR playground, or do the first byte by hand to check you understand it.

You encrypt the 16-byte block 4141414141414141 4141414141414141 (two identical halves) with 3DES in ECB. What is true about the ciphertext?

  • Its two 8-byte halves are identical
  • The halves differ because of the IV (Initialization Vector)
  • It depends on whether the key is weak
HintUse the block cipher lab: TDES (Triple DES), ECB, paste the 32-digit key.

A colleague changed one byte of a key from 4B to 4A “to fix parity” (4B has four 1-bits). Will the 3DES ciphertexts produced with the key change?

  • No
  • Yes, completely
  • Only the first block

An attacker can see an encrypted message and its MAC, but has neither key. Which of these can they still do?

  • Change the amount and fix the MAC
  • Re-send (replay) the exact same message and MAC
  • Check whether the MAC is valid

You need a 2-key 3DES key whose behaviour is identical to single DES with key 0123456789ABCDEF. Write the 32-hex-digit 3DES key.

HintEDE (Encrypt–Decrypt–Encrypt) with K1 = K2: the first E and the D cancel.
Solution0123456789ABCDEF0123456789ABCDEF. HSMs usually refuse to import such keys for anything sensitive, because they are only as strong as single DES.