Sub topic 12
Tokenization, encryption, and PCI DSS scope
This chapter assumes you know basic HTTP APIs, relational databases, and how to read short Python snippets. It explains how to keep card data out of most of your system.
Why scope matters
PCI DSS is the Payment Card Industry Data Security Standard. It applies to any system that stores, processes, or transmits card data. Every system in scope must meet its controls, and each control adds audit and operating cost.
The primary account number (PAN) is the 13 to 19 digit card number. The card verification code (CVC) must never be stored after authorization, even if encrypted. So the main design goal is to make as few components as possible ever see a PAN.
Tokenization
A token is a random value that stands in for a PAN. It has no mathematical relationship to the card number, so stealing a token reveals nothing. A token vault is a small, tightly controlled service that maps tokens to PANs.
The usual flow works like this:
- The customer types the card into a field hosted by the PSP or by your vault, not into your own page.
- The hosted field sends the PAN straight to the vault.
- The vault returns a token to the browser.
- Your backend receives only the token and uses it to create payments.
Your API, database, logs, and ledger then hold tokens only. Only the vault and the hosted field are in the strictest scope.
Encryption
Encryption protects data in two places.
- In transit: use TLS on every connection that carries card data or tokens.
- At rest: encrypt the vault's PAN storage with a data key, and protect that data key with a master key held in a key management service (KMS) or hardware security module (HSM).
This is called envelope encryption. Rotating the master key only requires re-wrapping the data keys, not re-encrypting every card.
A short sketch shows the idea, using the cryptography package.
import secrets
from cryptography.fernet import Fernet
def tokenize(pan: str, vault: dict, data_key: bytes) -> str:
token = "tok_" + secrets.token_urlsafe(16)
vault[token] = Fernet(data_key).encrypt(pan.encode())
return token
def detokenize(token: str, vault: dict, data_key: bytes) -> str:
return Fernet(data_key).decrypt(vault[token]).decode()
data_key = Fernet.generate_key() # in production, wrapped by a KMS master key
vault = {}
t = tokenize("4242424242424242", vault, data_key)
print(t, detokenize(t, vault, data_key)[-4:])The token comes from a secure random generator, not from the PAN. A real vault also needs access control, audit logs, and a database instead of a dictionary.
Common mistakes
- Logging request bodies. A PAN in a log file brings the log system into scope.
- Treating a hash of the PAN as safe. The PAN space is small enough to brute-force, so use random tokens.
- Storing the CVC after authorization.
- Showing full PANs in support tools. Display only the last four digits.
Scope in practice
| Approach | Card data touches your servers | Typical scope |
|---|---|---|
| Hosted payment page | No | Smallest |
| Hosted fields or client-side SDK | No | Small |
| Direct API post to your backend | Yes | Largest |
Network segmentation helps too. Put the vault in its own network segment with strict firewall rules, so the rest of your platform stays out of scope.
Exercise
A teammate proposes storing SHA-256 hashes of card numbers as "safe tokens" so the team can match repeat customers. Why is this a poor choice, and what would you use instead?
Show answer
Card numbers have limited structure, including a fixed length and a Luhn check digit, so an attacker can compute hashes of all likely PANs and reverse the stored values. Use random tokens generated by a vault. If you need to match repeat cards, ask the vault or PSP for a card fingerprint produced with a secret key.