Cryptography Key Usage - Quick Review
v# 🔐 Cryptography Key Usage — Quick Review
1. Symmetric Key Cryptography
Alice ──[Key K]──► Encrypt(M) ──► Ciphertext ──► Decrypt(C) ──[Key K]──► Bob
- Alice and Bob share the exact same secret key
- Must exchange the key securely beforehand (the key distribution problem!)
- Examples: AES, ChaCha20, DES
| ✅ Pros | ❌ Cons |
|---|---|
| Very fast | Key must be shared securely first |
| Low overhead | Doesn't scale — users need keys |
| Great for bulk data encryption | No built-in identity proof |
🔑 Analogy: Both Alice and Bob have a copy of the same house key. Anyone with the key can lock or unlock the door.
Real-World Use
- TLS session — after handshake, AES symmetric key encrypts actual data
- Full-disk encryption — BitLocker / FileVault uses AES
- File encryption — encrypting a ZIP archive with a password
2. Asymmetric (Public Key) Cryptography
| Key | Who has it? | Can be shared? |
|---|---|---|
| Public Key | Everyone | ✅ Yes — share freely |
| Private Key | Owner only | ❌ Never share |
🏦 Analogy: A public key is like a mailbox slot — anyone can drop a letter in (encrypt). Only the mailbox owner has the key to open it (decrypt).
2.1 Encryption — Sending Secret Info to Someone
Goal: Only Bob can read the message.
Alice wants to send secret M to Bob:
① Alice gets Bob's PUBLIC KEY (from certificate, key server, etc.)
② Alice encrypts: C = Encrypt(M, Bob's Public Key)
③ Alice sends C to Bob
④ Bob decrypts: M = Decrypt(C, Bob's Private Key)
Key rule: Encrypted with public key → only private key owner can decrypt
✉️ Only Bob has the private key → only Bob can read the message → confidentiality guaranteed
2.2 Digital Signature — Proving Identity / Non-Repudiation
Goal: Prove the message came from Alice and wasn't tampered with.
Alice wants to prove she sent message M:
① Alice hashes the message: h = Hash(M) e.g., SHA-256
② Alice signs the hash: σ = Sign(h, Alice's Private Key)
③ Alice sends M + σ to Bob
④ Bob verifies:
- Bob re-hashes the message: h' = Hash(M)
- Bob decrypts signature: h = Verify(σ, Alice's Public Key)
- If h' == h → ✅ Signature valid (came from Alice, message intact)
- If h' ≠ h → ❌ Message tampered or wrong sender
Key rule: Signed with private key → anyone with public key can verify
🖊️ Analogy: A handwritten signature — only you can write it (private key), but anyone can look at it and confirm it's yours (public key).
2.3 Sign vs. Encrypt — Side by Side
| Goal | Action | Key Used | Who Can |
|---|---|---|---|
| Confidentiality (only Bob reads) | Encrypt | Bob's Public key | Anyone can encrypt; only Bob decrypts |
| Authentication (prove it's Alice) | Sign | Alice's Private key | Only Alice signs; anyone can verify |
2.4 Both: Encrypt AND Sign (Full Security)
Goal: Only Bob can read it, AND Bob knows it came from Alice.
① Alice signs message M: σ = Sign(Hash(M), Alice's Private Key)
② Alice encrypts (M + σ): C = Encrypt(M + σ, Bob's Public Key)
③ Alice sends C to Bob
④ Bob decrypts with: Bob's Private Key → gets M + σ
⑤ Bob verifies signature: Verify(σ, Alice's Public Key) → confirms Alice sent it
3. Key Usage Rules — The Golden Table ⭐
| Operation | Use Whose Key? | Type | Purpose |
|---|---|---|---|
| Encrypt a message to Bob | Bob's Public key | Asymmetric | Only Bob can decrypt → Confidentiality |
| Decrypt a message from Alice | Your own Private key | Asymmetric | You are the intended recipient |
| Sign a message as Alice | Alice's Private key | Asymmetric | Proves Alice sent it → Authentication |
| Verify Alice's signature | Alice's Public key | Asymmetric | Anyone can confirm it came from Alice |
| Encrypt/Decrypt data (fast) | Shared Secret key | Symmetric | Bulk data encryption |
🧠 Memory trick:
- Private key = your secret actions (decrypt your mail, sign your name)
- Public key = public-facing actions (anyone locks mail for you, anyone checks your signature)
4. Hybrid Encryption (How TLS/S-MIME Actually Work)
Related
Asymmetric is slow for large data → use it only to exchange the symmetric key, then switch.
① Alice generates a random AES session key K
② Alice encrypts K with Bob's PUBLIC key: Enc(K, Bob_PK)
③ Bob decrypts to get K: Dec(Enc(K, Bob_PK), Bob_SK) = K
④ Both now share K
⑤ All data encrypted with fast symmetric AES using K
🚗 Analogy: Asymmetric = expensive armored van that delivers the house key. Symmetric = the normal key you use every day to unlock your door.
Examples:
- TLS — ECDHE/RSA handshake to agree on session key → AES-GCM encrypts traffic
- S/MIME — RSA/ECC encrypts the session key → Triple DES/AES encrypts the email
- PGP — RSA/ECC wraps the session key → AES encrypts the file
5. Hash Functions (One-Way, Not Reversible)
| Property | Meaning |
|---|---|
| Deterministic | Same input → always same output |
| One-way | Cannot reverse hash to get original message |
| Avalanche effect | Tiny change in input → completely different hash |
| Collision resistant | Hard to find two different inputs with same hash |
Used in:
- Digital signatures (sign the hash, not the whole message — faster)
- Password storage (store hash, not plaintext)
- File integrity checks (compare hash before/after)
- TLS (HMAC for record integrity)
Examples: SHA-256, SHA-3, HMAC-SHA256
6. Complete Scenario Walkthroughs
Scenario A — Alice sends a SECRET email to Bob (S/MIME style)
Goal: Confidentiality only (Bob can read it, content hidden from others)
① Generate random session key K (e.g., AES-256)
② Encrypt message M: Enc_AES(M, K) = Ciphertext
③ Encrypt key K: Enc(K, Bob's Public Key) = Wrapped Key
④ Send both: [Ciphertext + Wrapped Key] → Bob
Bob:
⑤ Decrypt wrapped key: Dec(Wrapped Key, Bob's Private Key) = K
⑥ Decrypt message: Dec_AES(Ciphertext, K) = M ✅
Scenario B — Alice SIGNS a message (non-repudiation)
Goal: Bob can verify the message came from Alice and wasn't modified
① Hash message: h = SHA256(M)
② Sign hash: σ = RSA_Sign(h, Alice's Private Key)
③ Send: [M + σ] → Bob
Bob:
④ Re-hash message: h' = SHA256(M)
⑤ Verify signature: h = RSA_Verify(σ, Alice's Public Key)
⑥ If h == h' → ✅ Valid. Came from Alice, not modified.
Scenario C — Alice sends SIGNED and ENCRYPTED email (Full Security)
Goal: Only Bob reads it + Bob knows it came from Alice
① Hash + Sign: σ = Sign(Hash(M), Alice's Private Key)
② Encrypt [M + σ]: C = Enc(M + σ, Bob's Public Key)
③ Send C → Bob
Bob:
④ Decrypt: M + σ = Dec(C, Bob's Private Key)
⑤ Verify signature: Verify(σ, Alice's Public Key) → ✅ Alice confirmed
Scenario D — TLS Handshake Key Exchange (Simplified)
① Server sends Certificate (contains Server's Public Key)
② Client verifies Certificate (via CA signature)
③ Client generates Premaster Secret
④ Client encrypts it: Enc(Premaster Secret, Server's Public Key)
⑤ Server decrypts: Dec(..., Server's Private Key) = Premaster Secret
⑥ Both compute:
Session Key = f(Client Random, Server Random, Premaster Secret)
⑦ All data encrypted with Session Key (AES) from now on ✅
7. Quick Reference Summary
| "I want to…" | Use |
|---|---|
| Send a secret to Bob | Encrypt with Bob's public key |
| Read a secret sent to me | Decrypt with my private key |
| Prove this message is from me | Sign with my private key |
| Check if message is really from Alice | Verify with Alice's public key |
| Encrypt lots of data fast | Symmetric AES with shared key |
| Securely agree on a symmetric key | DH / ECDHE / RSA key exchange |
| Check data hasn't been modified | Hash (SHA-256) comparison |