Why This Matters#

Cryptography is the mathematical foundation of all digital security. Every time you visit a website, send a message, or authenticate to a service, cryptographic algorithms are protecting you. Understanding how they work — and how they fail — is essential.


Core Concepts#

ConceptPurposeProperty Provided
EncryptionHide data from unauthorized readersConfidentiality
HashingCreate fixed-size fingerprint of dataIntegrity
Digital SignaturesProve who created/approved dataAuthentication + Non-repudiation
Key ExchangeSecurely agree on a shared secretConfidentiality (setup)

Symmetric Encryption#

Same key for encryption and decryption. Fast, efficient for bulk data.

flowchart LR
    P["Plaintext"] -->|"Key K"| E["Encrypt"]
    E --> C["Ciphertext"]
    C -->|"Key K"| D["Decrypt"]
    D --> P2["Plaintext"]

Algorithms#

AlgorithmBlock SizeKey SizeStatusNotes
AES-128128 bits128 bitsSecureMinimum for new systems
AES-256128 bits256 bitsSecureStandard for sensitive data
ChaCha20Stream256 bitsSecureFaster on devices without AES hardware
3DES64 bits168 bitsDeprecatedSweet32 attack, retire by 2023
Blowfish64 bits32–448 bitsLegacySmall block size limits use

Block Cipher Modes#

How to encrypt data larger than one block:

ModePropertiesUse Case
ECBEach block independent (INSECURE — patterns visible)Never use
CBCEach block XORed with previous ciphertextLegacy TLS, disk encryption
CTRTurns block cipher into stream cipherParallel encryption
GCMCTR + authentication tag (AEAD)TLS 1.3, modern standard

Always use authenticated encryption (GCM, ChaCha20-Poly1305) — encryption without authentication allows undetected tampering.

The Key Distribution Problem#

If Alice and Bob need the same key, how do they share it securely? This is why asymmetric cryptography exists.


Asymmetric Encryption#

Two mathematically linked keys: public (shared freely) and private (kept secret).

flowchart LR
    P["Plaintext"] -->|"Bob's Public Key"| E["Encrypt"]
    E --> C["Ciphertext"]
    C -->|"Bob's Private Key"| D["Decrypt"]
    D --> P2["Plaintext"]

Algorithms#

AlgorithmBased OnKey SizeUse Case
RSAInteger factorization2048–4096 bitsKey exchange, signatures (legacy)
ECDSAElliptic curve discrete log256–384 bitsTLS certificates, Bitcoin
Ed25519Twisted Edwards curve256 bitsSSH keys, modern signatures
X25519Curve25519256 bitsKey exchange (TLS 1.3)

Why Not Use Asymmetric for Everything?#

AspectSymmetric (AES-256)Asymmetric (RSA-2048)
Speed~1 GB/s~1 MB/s
Key size for equivalent security256 bits2048+ bits
Use caseBulk dataKey exchange, signatures

Solution: Hybrid encryption — use asymmetric to exchange a symmetric key, then encrypt data with the symmetric key. This is exactly what TLS does.


Hashing#

One-way function: input → fixed-size output. Cannot be reversed.

Properties of a Good Hash#

PropertyMeaning
DeterministicSame input always produces same output
Avalanche effectTiny input change → completely different output
Pre-image resistanceCannot find input from output
Collision resistanceCannot find two inputs with same output

Algorithms#

AlgorithmOutput SizeStatusUse Case
MD5128 bitsBroken (collisions)Legacy checksums only
SHA-1160 bitsBroken (collisions)Do not use
SHA-256256 bitsSecureFile integrity, certificates, blockchain
SHA-3VariableSecureAlternative to SHA-2 family
BLAKE3256 bitsSecureHigh-performance hashing

Password Hashing (Different Category)#

General-purpose hashes (SHA-256) are too fast for passwords — attackers can try billions per second. Password hashes are intentionally slow:

AlgorithmFeaturesRecommendation
bcryptSalt + configurable cost factorGood (widely supported)
scryptSalt + memory-hardGood (resists GPU attacks)
Argon2idSalt + memory-hard + time-hardBest (winner of PHC competition)

Never: store plaintext passwords, use MD5/SHA for passwords, use unsalted hashes.


Digital Signatures#

Prove that a message was created by a specific entity and hasn’t been modified:

sequenceDiagram
    participant Alice
    participant Bob
    
    Note over Alice: Sign with private key
    Alice->>Alice: hash(message) → digest
    Alice->>Alice: encrypt(digest, private_key) → signature
    Alice->>Bob: message + signature
    
    Note over Bob: Verify with public key
    Bob->>Bob: hash(message) → digest
    Bob->>Bob: decrypt(signature, public_key) → original_digest
    Bob->>Bob: Compare digests (match = valid)

Use Cases#

ApplicationWhat’s Signed
TLS certificatesServer’s public key + identity
Code signingSoftware binaries
Git commitsCommit content
JWT tokensClaims (payload)
Email (S/MIME, PGP)Message content

Public Key Infrastructure (PKI)#

The trust system that makes HTTPS work.

Certificate Chain#

flowchart TD
    Root["Root CA<br/>(self-signed, offline, in OS trust store)"]
    Inter["Intermediate CA<br/>(signs end-entity certs)"]
    Leaf["Server Certificate<br/>(your-domain.com)"]
    
    Root -->|signs| Inter
    Inter -->|signs| Leaf

X.509 Certificate Contents#

FieldPurpose
SubjectWho the cert identifies (CN=example.com)
IssuerWho signed it (CA)
Public KeyThe subject’s public key
ValidityNot Before / Not After dates
Serial NumberUnique identifier
ExtensionsSANs (alternative names), key usage, constraints
SignatureCA’s signature over all fields

Certificate Lifecycle#

stateDiagram-v2
    [*] --> Requested: CSR generated
    Requested --> Issued: CA validates & signs
    Issued --> Active: Installed on server
    Active --> Renewed: Before expiry
    Active --> Revoked: Compromised/invalid
    Renewed --> Active
    Revoked --> [*]

Revocation#

MethodHow It WorksProsCons
CRLCA publishes list of revoked certsSimpleLarge files, infrequent updates
OCSPReal-time query to CACurrentPrivacy (CA sees what you visit)
OCSP StaplingServer fetches and caches OCSP responseFast, privateServer must support it
Certificate TransparencyPublic log of all issued certsDetects mis-issuanceDoesn’t revoke, only detects

TLS (Transport Layer Security)#

The protocol that secures HTTPS, email, and most internet communication.

TLS 1.3 Handshake (Simplified)#

sequenceDiagram
    participant C as Client
    participant S as Server
    
    C->>S: ClientHello (supported ciphers, key share)
    S->>C: ServerHello (chosen cipher, key share, certificate, signature)
    Note over C,S: Both compute shared secret from key shares
    C->>S: Finished (encrypted)
    S->>C: Finished (encrypted)
    Note over C,S: Application data (encrypted with shared key)

TLS 1.3 vs 1.2#

AspectTLS 1.2TLS 1.3
Handshake round-trips2 RTT1 RTT (0-RTT resumption)
Cipher suitesMany (some insecure)Only 5 (all secure)
Key exchangeRSA or ECDHEECDHE only (forward secrecy mandatory)
Removed—RSA key exchange, CBC, RC4, SHA-1, compression

Forward Secrecy#

Even if the server’s private key is compromised later, past sessions cannot be decrypted — because each session uses ephemeral keys that are discarded.


Post-Quantum Cryptography#

Quantum computers threaten current asymmetric algorithms (RSA, ECDSA) via Shor’s algorithm. Symmetric and hashing are less affected (just need larger keys).

AlgorithmTypeStatus
ML-KEM (Kyber)Key encapsulationNIST standard (2024)
ML-DSA (Dilithium)Digital signatureNIST standard (2024)
SLH-DSA (SPHINCS+)Digital signature (hash-based)NIST standard (2024)

Action now: Inventory where you use asymmetric crypto, plan migration timeline, use hybrid approaches (classical + post-quantum) during transition.


Key Takeaways#

  1. Symmetric for speed, asymmetric for trust — real systems use both (hybrid)
  2. Always use authenticated encryption (AES-GCM, ChaCha20-Poly1305)
  3. Never roll your own crypto — use well-tested libraries (libsodium, OpenSSL)
  4. Password hashing ≠ general hashing — use Argon2id or bcrypt, never SHA-256
  5. TLS 1.3 is the standard — disable TLS 1.0/1.1, prefer 1.3 over 1.2
  6. Forward secrecy is mandatory — ephemeral key exchange protects past sessions
  7. Post-quantum is coming — start planning migration now