Data at Rest

Difficulty: Intermediate

Question

How do you protect sensitive data at rest in a web application, and what are the different layers of encryption you can use?

Answer

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.

Layers of encryption

LayerExampleProtects againstDoes not protect against
Full disk / volumeAWS EBS encryption, LUKS, BitLockerStolen disks, decommissioned hardwareAnyone who can query the running DB
Database (TDE)SQL Server or Oracle TDE, RDS storage encryptionCopies of data files and backupsSQL injection, over-privileged DB users
Application / field-levelEncrypt PAN, Aadhaar or PAN card number before INSERTDB admins, DB dumps, leaked backupsA compromised application server holding the key
TokenizationReplace a card number with a token, real value in a vaultMost systems ever seeing the real valueCompromise 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.

Envelope encryption and KMS

Rather than one master key encrypting everything:

  1. A key encryption key (KEK) lives inside a KMS or HSM (AWS KMS, GCP Cloud KMS, Azure Key Vault, HashiCorp Vault) and never leaves it.
  2. For each record or file, the app asks KMS for a data encryption key (DEK).
  3. The app encrypts data locally with the DEK (AES-256-GCM), stores the ciphertext along with the encrypted DEK, and discards the plaintext DEK.
  4. To decrypt, it sends the encrypted DEK to KMS, which checks IAM permissions, logs the call and returns the plaintext DEK.

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.

Things that are not encryption

  • Passwords are hashed, not encrypted: use Argon2id or bcrypt so even you cannot recover them.
  • Base64 is encoding, not protection.
  • Data you do not store cannot leak: avoid storing full card numbers, CVVs (never allowed under PCI DSS), or more of an ID number than you need.

Common gaps

  • Backups, snapshots, data warehouse exports and log files containing plaintext copies of the same fields.
  • Encryption keys in the same repo, environment file or database as the data.
  • Using AES in ECB mode or reusing a GCM nonce with the same key.
  • No key rotation plan and no audit of who called decrypt.
  • Forgetting search: encrypted fields cannot be queried directly; use a keyed hash (blind index) for equality lookups.

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.

Envelope encryption with a KMS. Data is encrypted locally with a data key; only the KMS can unwrap that key.
  1. Request a data key from KMS: KMS returns plaintext and encrypted DEK.
  2. Encrypt the field with AES-256-GCM: Done locally using the plaintext DEK.
  3. Store ciphertext and encrypted DEK: Plaintext DEK is discarded from memory.
  4. Decrypt: send encrypted DEK to KMS: KMS checks IAM policy and logs the call.
  5. Decrypt the field locally: Only authorised services ever see plaintext.

Code examples

Field-level encryption with AES-256-GCM in Node.js

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.

Key points

  • Use multiple layers: disk/volume, database, and application-level encryption for the most sensitive fields.
  • Disk and TDE encryption do not help against SQL injection or over-privileged DB users.
  • Envelope encryption: KEK in KMS/HSM, per-record DEKs, encrypted DEK stored with data.
  • Keys must be stored and access-controlled separately from the data, with audit logs.
  • Hash passwords, tokenize card data, and do not store what you do not need.

Common follow-up questions

  • What is the difference between TDE and application-level encryption?
  • How does envelope encryption make key rotation cheaper?
  • How would you search on an encrypted email or phone column?
  • How would you handle encryption for backups and analytics exports?

Interview tips

  • Separate the layers and say what each one actually protects against; that is the key insight.
  • Bring up key management early; interviewers want to hear that keys live in KMS, not in .env files.
  • Mention data minimisation and tokenization as alternatives to storing sensitive data at all.

Concepts covered

Encryption at Rest, Envelope Encryption, KMS, AES-256-GCM, Field-Level Encryption, Key Rotation

More Web Security (OWASP, JWT, XSS, CSRF) questions