Format-Preserving Encryption Explained: Scrambled Data That Still Looks Right

Format preserving encryption explained in plain terms means understanding encryption that scrambles the value without changing its shape: a sixteen-digit card number encrypts to a different sixteen-digit number, and a nine-digit account code encrypts to another nine-digit code. The central question is why anyone would want an encrypted value to look exactly like the original, and the answer is that the rest of the system, the databases, the forms, and the validation checks, was never built to handle anything else.

Ordinary encryption is careless about shape. Feed it a card number and it returns a block of binary data, often encoded as a long string of characters that will not fit the field it came from, will not pass the checksum, and will break every report that expects digits. Format-preserving encryption solves the plumbing problem by guaranteeing that the ciphertext has exactly the same format as the plaintext, so nothing downstream needs to change.

The idea is older than most people realise, and it became a standard only after a public break forced a rethink. This article explains what format-preserving encryption is, how it works, and the one weakness, determinism, that every deployment must plan around.

Key takeaways

  • Format-preserving encryption keeps the ciphertext in the same format as the plaintext.
  • A card number encrypts to another valid-looking card number.
  • It exists so legacy databases and forms need no changes.
  • The standards are FF1 and FF3-1, both built on Feistel networks.
  • It is deterministic, so equal plaintexts produce equal ciphertexts.

What format-preserving encryption is

The problem it solves is mundane and expensive. A payment system stores card numbers in a database field that holds exactly sixteen digits, validates them with a checksum, and prints them on receipts in groups of four. Encrypting those numbers with ordinary AES would produce binary data that fits none of that, so the company would have to change the database, the validation, and every report, which is a project nobody wants to fund.

Format-preserving encryption avoids the project. It is a cipher whose output is constrained to the same set as its input: digits in, digits out; letters in, letters out; a date in, a plausible date out. The ciphertext is still unreadable without the key, but it slides through the existing system untouched, because as far as the database is concerned, it is just another card number.

The standard constructions are FF1 and FF3, published by the United States National Institute of Standards and Technology in 2016. Both are built on Feistel networks, the same structure behind the older DES cipher, with the AES block cipher used as the round function. FF1 works for almost any format and is the recommended choice, while FF3 was designed for small domains and was revised to FF3-1 in 2017 after researchers showed a practical attack against the original.

A tweak is the extra ingredient that makes the schemes flexible. A tweak is a public value, such as a column name or a record identifier, that is mixed into the encryption so that the same plaintext encrypts differently under different tweaks. It gives each field its own encryption without needing a separate key, and it is the reason two identical card numbers in different columns do not produce identical ciphertexts.

Our guide to AES encryption explains the block cipher that powers the standard constructions, and our guide to hashing explains the one-way functions that format-preserving encryption is often confused with.

How format-preserving encryption works

The core trick is to treat the format as a number system. A sixteen-digit card number is simply a number between zero and ten to the sixteenth power, and a Feistel network can be built to permute exactly that range. The network splits the number into two halves, then runs them through several rounds in which one half is mixed with a keyed function of the other, and after enough rounds the result is a thoroughly scrambled number in the same range.

Each round uses AES as its engine. The round function takes the key, the tweak, the round number, and one half of the value, and produces a pseudo-random output that is added to the other half. Because AES is a strong cipher, the output looks random, and because the Feistel structure is reversible, decryption runs the same rounds backwards and recovers the original number.

Cycle walking handles the awkward cases. Some formats, such as valid calendar dates or card numbers with a correct checksum, are not simple ranges, so the cipher encrypts the underlying number and checks whether the result is valid; if it is not, it encrypts again, repeating until a valid value appears. The process terminates quickly in practice, and it guarantees that the output always looks like a real value.

FF1 and FF3 differ in their details. FF1 uses a ten-round Feistel network with a flexible structure that handles almost any radix, which is why it is the general recommendation. FF3 used an eight-round network with a smaller domain, and the 2017 attack showed that for small domains, such as a six-digit code, an attacker with enough examples could recover the key, which is why FF3 was withdrawn and replaced by the tweaked FF3-1.

The determinism is the price. The same plaintext under the same key and tweak always produces the same ciphertext, which means an attacker who can see the database can tell which records share a value, even without decrypting anything. A column of encrypted salaries would still reveal who earns the same as whom, which is why format-preserving encryption is a scoping tool, not a cure-all.

A dark office desk with a payment card glowing green under a lamp, its digits rearranged into a new pattern
A dark office desk with a payment card glowing green under a lamp, its digits rearranged into a new pattern, shows how format-preserving encryption works: the number changes completely, but it still looks like a card number.

Comparing format-preserving encryption with other data protections

Format-preserving encryption is one of several ways to protect data that must keep its shape, and each was built with a different emphasis. Tokenisation replaces a value with a random token stored in a vault, ordinary AES encryption protects the data but changes its format, and hashing destroys the original value entirely, and each approach trades reversibility for security in a different way.

The table below sets out the main contrasts between the leading approaches.

Aspect Format-preserving Tokenisation Ordinary AES
Output format Same as the input Any token format Binary, encoded
Reversible Yes, with the key Yes, via the vault Yes, with the key
Needs a vault No Yes No
Equal values visible Yes, deterministic Yes, same token No, with random IV
Best for Legacy databases Payment systems General encryption

The practical result is that format-preserving encryption is the right choice where the system cannot change, tokenisation suits payment data with a vault, and ordinary AES fits everything else.

Where format-preserving encryption is used

Payment systems are the biggest users. A retailer that must keep card numbers for refunds and chargebacks can encrypt them in place, and the encrypted values still pass the database checks, still fit the fields, and still look like card numbers to every report. The real numbers leave the scope of the audit, while the system keeps working exactly as before.

Database encryption in legacy systems uses it for the same reason. A mainframe field that holds a nine-digit account code cannot suddenly hold a long encrypted string, and rewriting the application is often more expensive than the encryption project itself. Format-preserving encryption lets the team encrypt the column and change nothing else.

Test data generation is a quieter use. Developers need realistic data that is not real data, and encrypting production values with a format-preserving cipher produces test records that look genuine, pass validation, and reveal nothing about the original customers. It is a neat way to keep production data out of the test environment.

Regulated industries use it to reduce audit scope. Under payment-card rules, data that is properly encrypted may fall outside the strictest controls, and format-preserving encryption lets an organisation claim that benefit without rebuilding its systems, which is the business case that sells the technology.

Cloud migrations use it as a bridge. A company moving a database to the cloud can encrypt sensitive fields in their original format, so the migration proceeds without changing the application, and the cloud provider stores only ciphertext that still looks like ordinary data.

Strengths and limits

The greatest strength of format-preserving encryption is that it changes nothing downstream. The database schema stays the same, the validation stays the same, and the application stays the same, which turns a multi-year migration into a single project. For a legacy system, that is often the difference between encrypting and not encrypting at all.

It is also a strength that the output is self-contained. There is no vault to run, no token database to protect, and no lookup on the decryption path, which makes the system simpler and cheaper to operate than tokenisation.

The limits are real, though, and the biggest is determinism. Equal plaintexts produce equal ciphertexts, which leaks equality, and the small domains it is used on, such as six-digit codes, are inherently easier to attack than large ones, as the FF3 break showed. It is a pragmatic tool for a specific problem, not a general-purpose cipher.

Format-preserving encryption checklist

  • Use FF1 for new deployments; it is the recommended standard.
  • Do not use the original FF3; it was broken and replaced by FF3-1.
  • Always use a tweak so identical values differ across fields.
  • Remember determinism leaks equality; plan for what that reveals.
  • Keep the key in a hardware module, not beside the data.
  • Validate the output format after encryption, including checksums.
  • Avoid tiny domains; small value spaces are easier to attack.
  • Rotate keys with a re-encryption plan, not a hope.
  • Document where encrypted values can still be correlated.

Frequently asked questions

What is format-preserving encryption in simple terms?

It is encryption that keeps the ciphertext in the same format as the plaintext, so a card number encrypts to another card number and a code encrypts to another code.

Why is it used for payment cards?

Because the encrypted number still fits the database field, still passes validation, and still looks like a card number, so the payment system needs no changes.

What are FF1 and FF3?

They are the standard constructions, published by the National Institute of Standards and Technology in 2016, both built on Feistel networks with AES as the round function.

Why was FF3 replaced by FF3-1?

Researchers showed a practical attack against FF3 on small domains in 2017, recovering keys from enough examples, so the standard was revised to the strengthened FF3-1.

Does it leak anything?

Yes, equality. The same plaintext always produces the same ciphertext, so an attacker can tell which records share a value, even without decrypting anything.

When should I use format-preserving encryption?

When sensitive data must be encrypted but the database, the forms, and the validation cannot change, which is the classic legacy-system problem.

The bottom line

Format preserving encryption explained in one line is this: scramble the value, keep the shape, and let the rest of the system carry on as if nothing changed.

It is the pragmatic answer to a boring problem, and the reason so many payment systems could encrypt their data without rebuilding everything around it.

This article is general information about how format-preserving encryption works, not advice about choosing encryption for a particular system.

Latest articles

Scroll to Top