Introduction to Cryptography
Cryptography is the practice and study of techniques for secure communication in the presence of adversaries.
It provides the mathematical foundation for confidentiality, integrity, authentication, and non-repudiation.
Symmetric Encryption
Uses the same key for both encryption and decryption. Like a physical key that both locks and unlocks a door.
How It Works
- Sender and receiver share a secret key
- Plaintext + Key = Ciphertext (encryption)
- Ciphertext + Same Key = Plaintext (decryption)
- Very fast and efficient
Common Algorithms
- AES (Advanced Encryption Standard) - most widely used
- ChaCha20 - modern, fast alternative to AES
- 3DES - legacy, being phased out
- Blowfish/Twofish - older alternatives
The Key Distribution Problem:
How do you securely share the secret key with someone you've never met? If you can securely share a key,
why not just use that secure channel to share your message? This is the fundamental challenge of symmetric encryption.
Use Cases for Symmetric Encryption:
- Full disk encryption (BitLocker, FileVault)
- Database encryption
- VPN tunnels (after key exchange)
- File/folder encryption
- Bulk data encryption (when keys are already shared)
Asymmetric Encryption
Uses a pair of mathematically related keys: a public key (shared openly) and a private key (kept secret).
What one key encrypts, only the other can decrypt.
How It Works
- Generate a key pair (public + private)
- Share public key openly
- Keep private key secret
- Encrypt with public, decrypt with private (confidentiality)
- Sign with private, verify with public (authentication)
Common Algorithms
- RSA - most widely used, key sizes 2048-4096 bits
- Elliptic Curve (ECC) - smaller keys, same security
- Diffie-Hellman - key exchange protocol
- ElGamal - alternative to RSA
Solving Key Distribution:
Alice wants to send Bob a secret message. Bob generates a key pair and publishes his public key.
Alice encrypts her message with Bob's public key. Only Bob's private key can decrypt it.
No need to pre-share a secret!
Trade-offs:
| Aspect |
Symmetric |
Asymmetric |
| Speed |
Very fast (1000x+ faster) |
Slow (computationally expensive) |
| Key Distribution |
Difficult - requires secure channel |
Easy - public keys can be shared openly |
| Key Size |
Small (128-256 bits) |
Large (2048-4096 bits for RSA) |
| Use Cases |
Bulk data encryption |
Key exchange, digital signatures, small messages |
Hybrid Approach (Best of Both):
Most real-world systems use both! Use asymmetric encryption to securely exchange a symmetric session key,
then use that symmetric key for fast bulk encryption. This is how TLS/SSL works.
Hashing Algorithms
A hash function takes input data of any size and produces a fixed-size output (the hash or digest).
It's a one-way function - you can't reverse it to get the original data.
Properties of Cryptographic Hashes:
- Deterministic: Same input always produces same output
- Fixed Output Size: SHA-256 always produces 256 bits, regardless of input size
- One-Way: Computationally infeasible to reverse (find input from hash)
- Collision Resistant: Extremely difficult to find two different inputs with same hash
- Avalanche Effect: Tiny change in input drastically changes output
Common Hash Algorithms:
| Algorithm |
Output Size |
Status |
| MD5 |
128 bits |
Broken - collisions found, don't use for security |
| SHA-1 |
160 bits |
Deprecated - collisions found in 2017 |
| SHA-256 |
256 bits |
Secure - widely used, recommended |
| SHA-3 |
Various (224-512 bits) |
Secure - newest standard, different design than SHA-2 |
| bcrypt/scrypt/Argon2 |
Various |
Secure - designed specifically for password hashing |
Use Cases for Hashing:
- Data Integrity: Verify files haven't been modified (checksums)
- Password Storage: Store hash of password, not the password itself
- Digital Signatures: Hash the message, sign the hash (faster than signing entire message)
- Blockchain: Each block contains hash of previous block
- File Deduplication: Identify duplicate files by comparing hashes
- Message Authentication Codes (HMAC): Hash + secret key for integrity + authentication
Password Hashing:
Never use fast hashes like SHA-256 for passwords! Attackers can compute billions of hashes per second.
Use specialized algorithms like bcrypt, scrypt, or Argon2 that are intentionally slow and memory-hard.
Digital Certificates and PKI
Public Key Infrastructure (PKI) solves a critical problem: How do you know a public key actually belongs to who it claims to?
The Trust Problem:
Man-in-the-Middle Risk:
Without verification, an attacker could intercept your connection and substitute their own public key.
You'd encrypt messages with the attacker's key, thinking it's your intended recipient's key.
How PKI Works:
- Certificate Authority (CA): Trusted third party that issues digital certificates
- Digital Certificate: Document binding a public key to an identity (person, organization, website)
- Certificate Signing: CA signs the certificate with their private key
- Verification: Anyone can verify certificate authenticity using CA's public key
- Chain of Trust: Root CAs certify intermediate CAs, which certify end entities
X.509 Certificate Contents:
- Subject (who the certificate belongs to)
- Public key
- Issuer (which CA signed it)
- Validity period (not before / not after dates)
- Serial number
- Digital signature (CA's signature on all the above)
Real-World Example - HTTPS:
When you visit https://bank.com, your browser: (1) Receives the website's certificate, (2) Verifies it was signed
by a trusted CA, (3) Checks it hasn't expired, (4) Confirms the domain matches, (5) Uses the public key from the
certificate to establish encrypted communication.
Certificate Revocation:
What if a private key is compromised? The certificate needs to be revoked before expiration:
- CRL (Certificate Revocation List): Published list of revoked certificates (slow, doesn't scale)
- OCSP (Online Certificate Status Protocol): Real-time check if certificate is valid (better, but privacy concerns)
- OCSP Stapling: Server provides OCSP response (best - fast and private)