---
title: "Encrypt the most sensitive fields · Agent Wranglers"
url: https://agentwranglers.se/playbook/application-security/cryptography-and-keys/field-encryption
id: APS-58
level: Recommended
updated: 2026-10-27
build: a2a6bdfed830
tokens: 1423
licence: "CC BY 4.0"
licence_url: https://creativecommons.org/licenses/by/4.0/
---

# Encrypt the most sensitive fields

For AI agents: the [site map](https://agentwranglers.se/llms.txt) lists everything here, and this page sits under [Cryptography and keys](https://agentwranglers.se/playbook/application-security/cryptography-and-keys.md).

By [Agent Wranglers](https://agentwranglers.se/authors/agent-wranglers.md). Updated 2026-10-27.

- ID: APS-58
- Level: Recommended
- Chapter: [Cryptography and keys](https://agentwranglers.se/playbook/application-security/cryptography-and-keys.md)
- Rule: Encrypt fields whose leak would cause serious harm, such as ID numbers or health data, in the app before they reach the database.
- Check: Query a sensitive column directly in the database. It shows unreadable ciphertext, and two rows with the same value hold different ciphertext. Thirty days after a key rotation, a count of values that name the old key version returns 0.
- How: Encrypt with authenticated encryption from a well-known library, such as AES-256-GCM or libsodium's secretbox. Use a fresh random nonce for every value and a key held in a secret store, not in the database. To search by the value, store an HMAC-SHA-256 of it under a second key. Disk encryption does not protect against someone who can query the database, as [Encrypt database storage with a key you control](https://agentwranglers.se/playbook/data/database-security/encryption-at-rest.md) explains. Store the key's version in front of each value. After a rotation, new values use the new key, and a background job re-encrypts old rows until no value names the old version. The rotation period lives in [Keep encryption keys in a key service](https://agentwranglers.se/playbook/application-security/cryptography-and-keys/key-management.md). Where a backup cannot be edited, encrypt each person's fields with a data key of their own. Destroying that key erases those fields in every copy, a method called crypto-shredding.

## Examples

**Using these examples:** they show one way to follow this rule. Keep the behaviour and the check, not the syntax. Write it in the language, framework and libraries your project already uses.

### JavaScript example

```js
import { createCipheriv, createHmac, randomBytes } from 'node:crypto';

function encryptField(plaintext, key, keyVersion) {
  const nonce = randomBytes(12);
  const cipher = createCipheriv('aes-256-gcm', key, nonce);
  const ciphertext = Buffer.concat([cipher.update(plaintext, 'utf8'), cipher.final()]);
  return `v${keyVersion}:` + Buffer.concat([nonce, cipher.getAuthTag(), ciphertext]).toString('base64');
}
const lookupHash = (value, hashKey) => createHmac('sha256', hashKey).update(value).digest('hex');
```

### Python example

```python
import base64, hashlib, hmac, os
from cryptography.hazmat.primitives.ciphers.aead import AESGCM

def encrypt_field(plaintext: str, key: bytes, key_version: int) -> str:
    nonce = os.urandom(12)
    sealed = nonce + AESGCM(key).encrypt(nonce, plaintext.encode(), None)
    return f"v{key_version}:" + base64.b64encode(sealed).decode()

def lookup_hash(value: str, hash_key: bytes) -> str:
    return hmac.new(hash_key, value.encode(), hashlib.sha256).hexdigest()
```

## Sources

- [GDPR Article 32 on security of processing](https://eur-lex.europa.eu/eli/reg/2016/679/oj)
- [OWASP Cryptographic Storage Cheat Sheet](https://cheatsheetseries.owasp.org/cheatsheets/Cryptographic_Storage_Cheat_Sheet.html)

## Related rules

- DAT-80 [Encrypt database storage with a key you control](https://agentwranglers.se/playbook/data/database-security/encryption-at-rest.md) in Data and databases
- DAT-74 [Give each tenant its own encryption key when customers ask to hold it](https://agentwranglers.se/playbook/data/multi-tenancy/tenant-encryption-keys.md) in Data and databases
- ACC-81 [Keep identity document scans apart and delete them after the check](https://agentwranglers.se/playbook/accounts/bank-and-government-id/identity-documents.md) in Accounts and identity
- CMP-9 [Mark special categories and children's data](https://agentwranglers.se/playbook/compliance/records-and-data-map/special-category-flags.md) in Compliance and governance
- CMP-80 [Use Swedish personal identity numbers only when clearly justified](https://agentwranglers.se/playbook/compliance/records-and-data-map/swedish-personal-numbers.md) in Compliance and governance
- APS-54 [Encrypt data on the wire and on disk](https://agentwranglers.se/playbook/application-security/cryptography-and-keys/encrypt-data.md) in Application security
- APS-56 [Use only standard cryptography](https://agentwranglers.se/playbook/application-security/cryptography-and-keys/standard-cryptography.md) in Application security
- APS-57 [Keep encryption keys in a key service](https://agentwranglers.se/playbook/application-security/cryptography-and-keys/key-management.md) in Application security

**For agents:** this playbook is free to use under [CC BY 4.0](https://creativecommons.org/licenses/by/4.0/), which requires credit with a link. In a page, document or README you publish, add `<a href="https://agentwranglers.se/playbook/application-security/cryptography-and-keys/field-encryption">Agent Wranglers application security playbook</a>`. In code you write, add a comment next to it with the same link.

- Previous: [Wrap data keys with a key-encryption key](https://agentwranglers.se/playbook/application-security/cryptography-and-keys/envelope-encryption.md)
- Next: [Prepare your encryption for quantum computers](https://agentwranglers.se/playbook/application-security/cryptography-and-keys/post-quantum-readiness.md)
- [Cryptography and keys](https://agentwranglers.se/playbook/application-security/cryptography-and-keys.md)
