Quick revision
The short answer
The application encrypts each ID number with AES-GCM using a data key protected by a KMS (envelope encryption), so the database and its administrators only ever see ciphertext. Next to it we store an HMAC of the normalised ID made with a separate key, and index that column, so an exact-match lookup computes the same HMAC and finds the row without decrypting anything. Each value carries its key version for rotation, range and LIKE searches are given up, and calls to decrypt are restricted and audited in the KMS.
What to remember
In this answer, we designed encryption for national ID numbers that hides them from DBAs and still supports exact-match lookup. The application normalises the value, encrypts it with AES-GCM under a KMS-protected data key with a fresh nonce and the customer id as associated data, and stores a separately keyed HMAC in a unique index for lookups. Key versions on each row make rotation a background job, range and LIKE searches are knowingly given up, and decryption is limited and audited in the KMS.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
The application encrypts each ID number with AES-GCM using a data key protected by a KMS (envelope encryption), so the database and its administrators only ever see ciphertext. Next to it we store an HMAC of the normalised ID made with a separate key, and index that column, so an exact-match lookup computes the same HMAC and finds the row without decrypting anything. Each value carries its key version for rotation, range and LIKE searches are given up, and calls to decrypt are restricted and audited in the KMS.
Why this question is asked
National ID numbers are among the most sensitive fields a company stores, and regulators increasingly expect that even insiders with database access cannot read them. The difficulty is that the business still needs to find a customer by ID number, and normal encryption makes every ciphertext look random, so the database cannot match it. Interviewers ask this design question because it combines cryptography basics, key management and query design in one realistic problem. In this answer, we will recap the building blocks, design the write and lookup paths with code, plan key rotation, be honest about what the scheme cannot do, and close with access control on the keys.
1A Quick Recap of the Building Blocks
AES-GCM is a symmetric cipher mode that encrypts and authenticates at the same time: the ciphertext comes with a 16-byte tag, and if anyone changes even one bit, decryption fails instead of returning garbage. It needs a fresh random 12-byte nonce for every encryption, and because of that the same ID encrypted twice gives two different ciphertexts. That is good for secrecy but means the database cannot compare ciphertexts.
A KMS, a key management service, holds master keys that never leave it. In envelope encryption, the KMS generates data keys, gives us the data key in plaintext for use in memory and also encrypted under the master key for storage. To use the stored data key later, we ask the KMS to decrypt it, and that request is checked against policy and logged. An HMAC is a keyed hash: the same input and key always give the same output, but without the key nobody can compute it, not even by trying every possible ID number.
2The Table Layout
We keep the encrypted ID in its own table, so normal customer queries never touch it and access can be granted separately.
Notice that there is no plaintext column anywhere, and the unique index is on the HMAC, so the database still enforces that one ID belongs to one customer. Storing the key versions with each row is what makes rotation possible later without guessing which key was used.
3Writing a Value
The application normalises the ID first, because the lookup only works if the same person's ID always produces the same bytes. Then it computes the HMAC with the lookup key and encrypts with the current data key.
5 messages in order. Play them, or step through them one at a time.
The database never receives the plaintext or any key.
Each line has a reason. The HMAC uses a different key from the encryption, so a leak of one key does not give away the other function. The associated data, the customer id passed to updateAAD, is authenticated but not encrypted: if someone copies the ciphertext of one customer into another customer's row, decryption fails because the customer id no longer matches. And the data key in memory is the plaintext data key from the KMS, cached for a short time so we do not call the KMS on every write.
4Looking Up by ID Number
A lookup repeats the first half of the write: normalise the input, compute the HMAC with the same lookup key, and query the indexed column.
So exact-match search costs one HMAC in the application and one index lookup in the database, and the DBA still sees only random-looking bytes. We need a keyed hash rather than a plain SHA-256 here because ID numbers have a small, structured space: someone with the table could hash every possible ID in hours and reverse a plain hash. Without the HMAC key, that attack is impossible.
5Planning Key Rotation
Data keys are rotated regularly, for example yearly, or immediately after a suspected leak. Because every row records its data_key_version, rotation is a background job: new writes use the new data key, and the job reads old rows in batches, decrypts with the old key, re-encrypts with the new one and updates the version. Reads work throughout, because each row says which key to use. Rotating the master key inside the KMS is even simpler, because we only re-wrap the few data keys, not the rows.
The lookup key is harder, since changing it changes every HMAC. During that rotation, the job recomputes id_lookup under the new key version, and lookups try the new version first and the old one second until the job finishes, which is why the version is part of the index.
6What the Scheme Cannot Do
We should be clear with the business about the costs. Range queries, sorting and LIKE searches on the ID are impossible, because neither the ciphertext nor the HMAC preserves order or substrings, so search by the last four digits needs its own design, such as a separate HMAC of the last four digits, which leaks more. The HMAC also reveals equality, so anyone with the table can see that two rows share an ID, which the unique index prevents anyway. If many teams need these values, a tokenisation service that holds the IDs and hands out tokens may be simpler than putting crypto into every service.
Finally, the protection is only as strong as the control over decryption. The KMS policy allows decrypt of the data keys only to this service's identity and a small break-glass role, every decrypt call is logged centrally, and an alert fires on unusual volumes, so a compromised account trying to decrypt the whole table is noticed quickly.
7Follow-up questions
Why not use pgcrypto inside PostgreSQL?
Because then the key travels to the database with every query, appearing in statements, possibly in logs and in memory of the server the DBA controls. The goal is that administrators cannot read the IDs, so encryption must happen in the application with keys the database never sees.
What happens if the same nonce is reused with AES-GCM?
It is a serious break: two messages under the same key and nonce reveal the XOR of their plaintexts and allow forging tags. That is why we generate 12 random bytes per encryption with SecureRandom and rotate data keys long before the number of encryptions makes random collisions plausible.
How do support agents see the ID when they need to?
Through an explicit reveal action in the support tool that calls the service, which checks the agent's role and reason, decrypts, shows a masked form by default, and records the reveal in the audit trail. The agent never gets database access to the ciphertext or keys.
8Conclusion
In this answer, we designed encryption for national ID numbers that hides them from DBAs and still supports exact-match lookup. The application normalises the value, encrypts it with AES-GCM under a KMS-protected data key with a fresh nonce and the customer id as associated data, and stores a separately keyed HMAC in a unique index for lookups. Key versions on each row make rotation a background job, range and LIKE searches are knowingly given up, and decryption is limited and audited in the KMS.
Open with
I would encrypt in the application so the database only stores ciphertext. Each ID is normalised, for example spaces and dashes removed and upper-cased, then encrypted with AES-GCM using a data key from a KMS with envelope encryption: the master key stays in the KMS, we store the wrapped data key and cache the plaintext one briefly in memory.
Then, if they want more
Each encryption gets a fresh random 12-byte nonce, and I pass the customer id as associated data so a ciphertext copied to another row fails to decrypt. For lookup, I store an HMAC-SHA256 of the normalised value with a different key, in a unique index.
Searching by ID means normalising the input, computing the same HMAC and doing an index lookup, with no decryption. It must be a keyed hash, because ID numbers are a small space and a plain hash could be brute-forced.
Every row stores the data key version and the lookup key version, so rotation is a background re-encryption job and lookups can check both versions during an HMAC key change. The trade-offs are that range, sorting and LIKE on the ID are impossible and equality is visible, and if many services need IDs a tokenisation service may be better.
Finally, the KMS policy restricts decrypt to the service and break-glass, and every decrypt call is audited.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- PostgreSQL documentation, MySQL reference manual
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "You must encrypt national ID numbers in a customer table so that DBAs cannot read them...": https://scrivio-interview-library.netlify.app/databases/database-security/a68293d8ef/