Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Encrypt Sensitive Fields: App vs Database vs Storage

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Choosing where to encrypt sensitive fields—at the application layer, in the database, or at the storage layer—determines how much control you have over data access, performance, and compliance. This guide walks you through the decision process, covering the trade-offs of each approach and giving you a practical framework to pick the right one for your system.

What You'll Need

  • A clear inventory of your sensitive data fields (e.g., PII, passwords, tokens, financial data).
  • Understanding of your threat model: who are you protecting against (DBAs, cloud providers, attackers with DB access)?
  • Knowledge of your application stack (language, framework, ORM) and database type (SQL, NoSQL).
  • Familiarity with your cloud provider's encryption options (if using managed services).
  • Compliance requirements (e.g., GDPR, HIPAA, PCI-DSS) that may mandate encryption at rest.

Step-by-Step

  1. Classify your data – List every field that needs protection. Mark which are highly sensitive (e.g., credit card numbers, health records) vs. moderately sensitive (e.g., email addresses, phone numbers). This helps prioritize.
  2. Define your threat model – Ask: who could access the raw data? If you worry about DBAs or database backups leaking, application-layer encryption is best. If you worry about physical theft of hard drives, storage-layer encryption suffices.
  3. Evaluate application-layer encryption – Encrypt data in your code before writing to the database. Use strong algorithms (AES-256-GCM) and manage keys separately (e.g., via a KMS). This gives you fine-grained control: you can encrypt only specific fields, and data stays encrypted even if the database is compromised.
  4. Consider database-level encryption (TDE) – Transparent Data Encryption (TDE) encrypts the entire database file at rest. It's easy to enable and has minimal application changes. However, it does not protect against someone with database access (e.g., a DBA querying the data), because the database decrypts data on read.
  5. Look at storage-layer encryption – Encrypt the underlying disk or object storage (e.g., AWS EBS encryption, S3 SSE). This protects against physical theft and some cloud provider issues, but again, anyone with access to the application or database can still read plaintext.
  6. Decide on a combination – For most systems, a layered approach is best: storage-layer encryption as a baseline, plus application-layer encryption for the most sensitive fields (like passwords or API keys). Database TDE can be added if you need to protect backups without app changes.
  7. Implement key management – Regardless of layer, keys must be managed securely. Use a dedicated key management service (KMS) or hardware security module (HSM). Rotate keys regularly and separate keys for different data types.
  8. Test thoroughly – After implementation, test that encryption/decryption works correctly, that search and indexing still function (if you need to search encrypted data, consider deterministic encryption or client-side search), and that performance is acceptable.

Common Mistakes

  • Relying solely on storage encryption – It does not protect against application-level SQL injection or malicious insiders with DB access.
  • Using application-layer encryption without key rotation – If a key is compromised, all data encrypted with it is exposed.
  • Encrypting fields that need to be searchable – Standard encryption breaks equality searches and range queries. Plan for searchable encryption (e.g., blind indexes) or avoid encrypting fields that must be searched.
  • Ignoring performance overhead – Encryption/decryption adds CPU load and can slow down queries, especially with large datasets.
  • Not backing up keys – Losing keys means losing data permanently. Ensure keys are backed up and recoverable.

Tips

  • Start with application-layer encryption for the most sensitive fields (passwords, tokens) and use storage encryption for everything else.
  • Use a well-vetted library (e.g., libsodium, AWS Encryption SDK) rather than rolling your own crypto.
  • For cloud databases, use managed encryption features (e.g., RDS encryption) to reduce operational burden.
  • Consider using column-level encryption in databases that support it (e.g., PostgreSQL pgcrypto) if you need DB-level control without full TDE.
  • Document your encryption strategy and key management procedures for audits and team onboarding.

Troubleshooting

  • Data not decrypting after key rotation – Ensure you keep old keys for decryption until all data has been re-encrypted with the new key. Implement a key versioning system.
  • Search queries fail on encrypted fields – If you need to search, use deterministic encryption (with a random IV) or a separate searchable index. Avoid encrypting fields that must be searched frequently.
  • Performance degradation – Test with realistic data volumes. If slow, consider caching decrypted data in memory (with proper security) or moving encryption to hardware acceleration.
  • Key management service outage – Have a fallback plan: cache keys locally (with secure storage) or use a secondary KMS region. Test failover procedures.
  • Compliance auditor asks for proof of encryption – Keep logs of encryption settings, key rotation dates, and access controls. Use cloud provider reports (e.g., AWS Config) to demonstrate compliance.

Frequently Asked Questions

Is application-layer encryption worth the complexity?

Yes, for highly sensitive fields like passwords or financial data. It protects against database breaches and insider threats, but it requires careful key management and can complicate queries.

Does database TDE protect against SQL injection?

No. TDE only encrypts data at rest. If an attacker gains query access, they can read plaintext because the database decrypts data on read.

Can I encrypt only some columns in a database?

Yes, with application-layer encryption or database features like column-level encryption. TDE encrypts the whole database, not individual columns.

What's the difference between encryption and hashing for passwords?

Encryption is reversible (with a key), while hashing is one-way. For passwords, always use a slow hash (bcrypt, Argon2) instead of encryption, because you never need to reverse it.

Do I need to encrypt data in transit as well?

Yes. Encryption at rest protects stored data, but you also need TLS/SSL to protect data as it travels between your app and database, and between users and your app.

How do I choose the right encryption algorithm?

Use AES-256 for symmetric encryption (standard and widely supported). For key exchange, use RSA or ECDH. Avoid deprecated algorithms like DES or MD5.