Difficulty: Intermediate
How do you protect sensitive data at rest in a web application, and what are the different layers of encryption you can use?
Protecting data at rest means making stored data (databases, files, object storage, backups, logs) useless to anyone who gets the storage without proper authorization. You combine encryption at several layers (disk, database, application field), keep the keys in a separate key management system with tight access, minimise what you store, and control and audit who can read the decrypted data. Encryption only helps if the keys are better protected than the data.
| Layer | Example | Protects against | Does not protect against |
|---|---|---|---|
| Full disk / volume | AWS EBS encryption, LUKS, BitLocker | Stolen disks, decommissioned hardware | Anyone who can query the running DB |
| Database (TDE) | SQL Server or Oracle TDE, RDS storage encryption | Copies of data files and backups | SQL injection, over-privileged DB users |
| Application / field-level | Encrypt PAN, Aadhaar or PAN card number before INSERT | DB admins, DB dumps, leaked backups | A compromised application server holding the key |
| Tokenization | Replace a card number with a token, real value in a vault | Most systems ever seeing the real value | Compromise of the vault itself |
Disk and TDE encryption are "free" checkboxes in the cloud and you should always enable them, but they are transparent to anyone with database access. For truly sensitive fields, application-level encryption is what actually stops a SQL injection or a leaked DB dump from exposing plaintext. This is why card networks and RBI tokenization rules push merchants to never store raw card numbers.
Rather than one master key encrypting everything:
This gives you central access control, audit logs for every decryption and cheap key rotation, since rotating the KEK does not require re-encrypting all the data.
India's DPDP Act 2023 requires "reasonable security safeguards" for personal data, and encryption at rest with proper key management is the baseline auditors expect.
import crypto from "node:crypto";
// In production the key comes from KMS (data key), never from source code
const key = crypto.randomBytes(32);
export function encryptField(plaintext) {
const iv = crypto.randomBytes(12); // unique per encryption, never reused
const cipher = crypto.createCipheriv("aes-256-gcm", key, iv);
const ct = Buffer.concat([cipher.update(plaintext, "utf8"), cipher.final()]);
const tag = cipher.getAuthTag(); // detects tampering
return [iv, tag, ct].map((b) => b.toString("base64")).join(".");
}
export function decryptField(stored) {
const [iv, tag, ct] = stored.split(".").map((s) => Buffer.from(s, "base64"));
const decipher = crypto.createDecipheriv("aes-256-gcm", key, iv);
decipher.setAuthTag(tag);
return Buffer.concat([decipher.update(ct), decipher.final()]).toString("utf8");
}
const enc = encryptField("ABCDE1234F"); // e.g. a PAN number
console.log(enc, decryptField(enc));
GCM provides confidentiality and integrity. Each value gets a fresh 12-byte IV stored alongside the ciphertext and auth tag.
Encryption at Rest, Envelope Encryption, KMS, AES-256-GCM, Field-Level Encryption, Key Rotation