Skip to main content

🔐 Encryption

A secret code with a key

The Lockbox Analogy

Sending a valuable item:

Without lockbox: Anyone who sees the package can take the contents.

With lockbox: Contents are locked. The key-holder can open it.

Encryption is a digital lockbox. Data is scrambled so authorized people with the key can read it.


What Is Encryption?

Encryption:
  "Hello, World!" → 🔐 → "xK9$#mPq2&..."
  (Plaintext)           (Ciphertext)

Decryption:
  "xK9$#mPq2&..." → 🔓 → "Hello, World!"
  (Ciphertext)           (Plaintext)

Without the key, ciphertext should look like random data.
In practice, the goal is that it’s computationally infeasible to recover the original plaintext.

Symmetric Encryption

Same Key for Both

One key does both:
  Encrypt: plaintext + key → ciphertext
  Decrypt: ciphertext + key → plaintext

Like a door lock:
  Same key locks and unlocks.

Common Algorithms

AlgorithmStatus
AES✅ Standard
ChaCha20✅ Modern
3DES⚠️ Legacy
DES❌ Not recommended

When to Use

✓ Fast, efficient
✓ Good for large data
✓ File encryption
✓ Database encryption
✓ Disk encryption

Challenge: How do you share the key safely?

Asymmetric Encryption

Two Different Keys

Key pair: Public key + Private key

Anyone can encrypt with PUBLIC key.
The private-key holder can decrypt with the PRIVATE key.

Like a mailbox:
  Anyone can drop mail in (public slot).
  The key-holder can take mail out (your key).

How It Works

Alice wants to send secret message to Bob:

1. Bob shares his PUBLIC key (anyone can have it)
2. Alice encrypts with Bob's public key
3. Alice sends encrypted message
4. Bob decrypts with his PRIVATE key (kept private by Bob)

Anyone can encrypt TO Bob.
Bob can decrypt (with his private key).

Common Algorithms

AlgorithmStatusUse Case
RSA✅ StandardKey exchange, signatures
ECC✅ ModernSame security, smaller keys
Diffie-Hellman✅Key exchange

When to Use

✓ Key exchange
✓ Digital signatures
✓ Protected communication setup
✗ Slow for large data

Often used together:
  Asymmetric to exchange symmetric key.
  Symmetric for the actual data.

Symmetric vs Asymmetric

AspectSymmetricAsymmetric
KeysOne shared keyKey pair (public/private)
SpeedVery fastSlow
Data sizeAny practical sizeSmall payloads (often key exchange)
Key sharingDifficultEasy (public key)
Use caseBulk encryptionKey exchange, signatures

Hybrid Approach (Most Common)

1. Use asymmetric to exchange a symmetric key
2. Use symmetric key for actual data

TLS typically works like this:
  Handshake (uses asymmetric crypto) → shared session keys
  Session keys encrypt most application data (symmetric)

Encryption in Practice

Data at Rest

Encrypted when stored:
  - Full disk encryption (BitLocker, FileVault)
  - Database encryption
  - Encrypted files/backups

Even if device stolen, data unreadable.

This is strongest when the device is locked and the encryption keys aren’t available to the attacker (for example, the device is powered off and the passcode isn’t known).

Data in Transit

Encrypted when moving:
  - HTTPS (TLS)
  - VPNs
  - Encrypted messaging

Network attackers can't read your data.

Assuming the connection is set up correctly (and certificates are validated), it’s designed to prevent passive eavesdroppers from reading your data and to make tampering detectable.

End-to-End Encryption (E2EE)

In end-to-end encryption, the sender and recipient hold the keys to read messages:
  - Signal, WhatsApp
  - iMessage

In many end-to-end designs, the service provider can’t decrypt message *contents* because only the endpoints hold the keys.
In practice, metadata and backups can change what’s protected.

Keys and Key Management

Key Length Matters

All else equal, larger keys raise the cost of brute-force guessing.

But real-world security also depends on the algorithm, mode (how it’s used), and implementation.

Protecting Keys

Key leaked = Data can be compromised

Store keys:
  - Hardware Security Modules (HSMs)
  - Key management services (AWS KMS)
  - Encrypted key storage

Avoid hardcoding keys in code.

Key Rotation

Change keys periodically:
  - Limits damage if key compromised
  - Compliance requirement
  - Often done via “envelope encryption” (re-wrapping data keys) instead of decrypting and re-encrypting all data

Common Mistakes

1. Rolling Your Own Crypto

Making up your own encryption algorithm is risky
"I'll just XOR with a secret word"

Use established, audited algorithms (AES, RSA).

2. Reusing IVs/Nonces

Reusing an IV/nonce with the same key can leak patterns
Use a unique IV/nonce for each encryption.
Many schemes use a random IV/nonce; some require uniqueness but not randomness.

3. Encrypting Without Authentication

Encryption alone doesn't necessarily detect tampering.
Use authenticated encryption (for example, AES-GCM or ChaCha20-Poly1305).

4. Storing Keys with Data

Encrypted database + key in the same place often defeats the point

Keep keys separate from encrypted data when possible.

FAQ

Q: Encryption vs Hashing?

Encryption: reversible (with key) Hashing: one-way (can't reverse)

Q: Is AES-128 still considered strong?

AES-128 is widely considered strong for many uses; AES-256 provides a larger security margin.

Q: What is AES-GCM?

AES with authentication built in. Detects tampering. It’s a common, recommended choice when used correctly.

Q: Is encryption slow?

Symmetric (AES) is very fast. Modern CPUs have hardware acceleration.


Summary

Encryption transforms data so authorized parties with the key can read it.

Key Takeaways:

  • Symmetric: same key encrypts/decrypts (fast)
  • Asymmetric: public/private key pair (slower, solves key distribution)
  • Hybrid: asymmetric for key exchange, symmetric for data
  • Encrypt data at rest and in transit
  • Use established algorithms (AES, RSA)
  • Protect your keys!

Encryption is a foundation of private communication and data protection.

In practice, encryption is a core building block — but it only works well when the surrounding details (key management, validation, and correct use of primitives) are done carefully.

Leave a Comment

Comments (0)

Be the first to comment on this concept.

Comments are approved automatically.