(Note · · 6 min read)
Encrypt the personal data, then still be able to find it
Encrypting personal data at rest is the easy half. The hard half arrives the next morning, when a feature that searched by national ID stops working, because the column it searched is now ciphertext. This is the pattern that gives the search back without giving the data away.
By Md. Habibur Rahman Shohel, full-stack & AI product engineer in Dhaka
Why the search breaks
Good encryption is deliberately non-deterministic: encrypt the same national ID twice and you get two different ciphertexts, because a fresh random value goes into each one. That is the property you want, since identical ciphertexts would otherwise tell an attacker which rows share a value without decrypting anything.
It is also the property that destroys lookup. There is no ciphertext you can compute from the user's search term that will equal the one in the row, so a WHERE clause can never match. Decrypting every row to compare them is not a search; it is a table scan that also hands the whole dataset to the application.
Store a fingerprint beside the value
The fix is a second column holding a keyed hash of the same value, computed with a secret key. The same input always produces the same hash, so you can index it and match on it, but the hash cannot be reversed into the original, and without the key an attacker cannot compute hashes to compare against. That column is the blind index.
Looking something up becomes: hash the search term with the same key, query the indexed hash column, then decrypt only the rows that came back. In Paylio, the payroll product I build, national ID, bank account, tax ID and salary details are encrypted at rest and searched exactly this way — a keyed hash stored beside the encrypted value, so an exact match is found without decrypting the rows.
employees
nid_encrypted bytea -- non-deterministic ciphertext
nid_index bytea -- keyed hash of the normalised value, INDEXED
-- write
nid_encrypted = encrypt(nid, data_key)
nid_index = hmac(normalise(nid), index_key)
-- read
where nid_index = hmac(normalise(search_term), index_key)
then decrypt the few rows returnedTwo keys, not one
Use a separate key for the index from the one that encrypts the data, and keep both in a secret manager rather than in the application's configuration. They protect against different things, and if the index key leaks you want the damage to be that someone can test guesses against the index, not that they can read the column.
Index the blind index column. Never put a database index on the ciphertext itself: it buys nothing, because the values differ every time, and it bloats the table.
Normalise before hashing, every time
A keyed hash is exact. Trailing whitespace, a different case, or dashes in a bank account number produce a completely different hash, so the row silently fails to match and the bug looks like missing data rather than a formatting difference.
Decide one canonical form per field — digits only for an account number, trimmed and lowercased for an email — and apply it in one function used by both the write path and the lookup path. If the two ever diverge, old rows become unfindable, and you will not notice until someone searches for one.
Know exactly what it cannot do
A blind index gives you equality and nothing else. No partial matches, no sorting, no ranges, no case-insensitive contains, unless you build a separate scheme for those and accept the extra leakage each one brings. Plan the features around that before encrypting the column, because adding fuzzy search afterwards is a migration, not a patch.
There is also a deliberate tuning decision. A shorter index leaks less about the underlying values but produces more collisions, so unrelated rows can share a hash. That is survivable — you decrypt the handful of candidates and filter them — but the code after the query must expect more than one row and verify the real value rather than trusting the match.
Where equality is genuinely not enough, the usual escape is to index a derived, coarser value instead: a bucket, a prefix, a year rather than a date. Each one trades a little privacy for a little query power, and the point is to make that trade knowingly rather than discovering it in production.
Encryption does not help if the data leaves another way
A column encrypted at rest and then written in plaintext into a log line, an error report or a trace has not been protected; it has been copied somewhere with weaker access control and longer retention. On Paylio, logs are structured JSON and personal data is kept out of them, out of traces and out of commits, which matters as much as the encryption itself.
Treat it as one rule with several enforcement points: the field is encrypted in the database, redacted in logs and traces, excluded from exports and analytics unless someone has asked for it specifically, and never committed as fixture data.
A separate question from who may see it
Encryption protects the data if the database itself gets out. It says nothing about which of your users may read a given row — that is authorisation, enforced on the server per request, and the two are often confused. A system can encrypt everything and still show one company's salaries to another if the query forgets its filter.
Do both, and keep them separate in your head: separation decides who may ask, encryption decides what an attacker gets if they bypass asking.
Common questions
Why can't I search an encrypted column?
Because good encryption is non-deterministic: the same value encrypts to a different ciphertext each time, so no value you compute from the search term will ever equal the stored one. That randomness is what stops an attacker seeing which rows share a value, so the answer is not to remove it but to add a separate searchable column.
What is a blind index?
A second column holding a keyed hash of the same value. The same input always produces the same hash, so the column can be indexed and matched on, but the hash cannot be reversed and cannot be computed without the key. You hash the search term, query that column, and decrypt only the rows returned.
Should the blind index use the same key as the encryption?
No. Use a separate index key and keep both keys in a secret manager rather than application configuration, so a leak of one does not become a leak of the other.
Why do blind index lookups sometimes return the wrong row?
Either a normalisation mismatch or a collision. The hash is exact, so whitespace, case or punctuation differences between the write path and the lookup path produce different hashes; use one canonical form applied by a single shared function. Shorter indexes also collide deliberately, so decrypt the candidates and verify the real value instead of trusting the match.
Can I do partial or case-insensitive search on encrypted data with a blind index?
No. A blind index supports equality only — no partial matches, sorting or ranges. Those need a separate scheme with its own leakage, so decide which queries a field must support before you encrypt it; adding fuzzy search afterwards is a migration rather than a patch.
Is encrypting personal data enough to keep it safe?
No. Encrypted data written into logs, traces, error reports or test fixtures has simply moved somewhere with weaker access control. Encryption also says nothing about which users may read which rows — that is server-side authorisation, and a system can encrypt everything and still leak one company's data to another through a query missing its filter.