Public Key Cryptography
Modern encryption technologies securing the internet today
Introduction to Public Key Cryptography
Public key cryptography, also known as asymmetric cryptography, revolutionized secure communication by solving one of the oldest problems in cryptography: how to share secret keys securely over an insecure channel.
Public key cryptography was invented in the 1970s and is the foundation of secure internet communications, including HTTPS, email encryption, and digital signatures.
The Key Exchange Problem
Traditional symmetric encryption requires both parties to share the same secret key. But how do you securely share this key when the communication channel itself is insecure? This is known as the key distribution problem.
The Problem:
Alice wants to send Bob a secret message, but they've never met. With symmetric encryption:
- Alice needs to send Bob the secret key
- But if Eve intercepts the key, she can read all future messages
- There's no way to securely share the key over an insecure channel
Visualizing the Problem
Alice
Eve (Eavesdropper)
Insecure Channel
Bob
Problem: Eve can intercept the key!
The Public Key Solution
Public key cryptography solves this with a brilliant insight: use two different keysβone for encryption (public key) and one for decryption (private key).
How It Works:
- Public Key: Can be shared with anyone, used to encrypt messages
- Private Key: Kept secret, used to decrypt messages
- Mathematical Relationship: The keys are mathematically related but you can't derive the private key from the public key
Public Key Communication
Alice
Eve
Can't decrypt!
Bob
Solution: Only Bob's private key can decrypt the message!
RSA Encryption
RSA (RivestβShamirβAdleman) is one of the first and most widely used public-key cryptosystems. It's based on the mathematical difficulty of factoring large numbers.
The Mathematics Behind RSA
RSA security relies on the fact that while it's easy to multiply two large prime numbers, it's extremely difficult to factor their product back into the original primes.
Simple Example with Small Numbers
- Choose two prime numbers: p = 61, q = 53
- Calculate n: n = p Γ q = 61 Γ 53 = 3233
- Calculate Ο(n): Ο(n) = (p-1) Γ (q-1) = 60 Γ 52 = 3120
- Choose e: e = 17 (must be coprime with Ο(n))
- Calculate d: d = 2753 (the modular multiplicative inverse)
- Public Key: (e, n) = (17, 3233)
- Private Key: (d, n) = (2753, 3233)
RSA Encryption and Decryption
Encryption: C = Me mod n
Decryption: M = Cd mod n
where M is the message, C is the ciphertext, e is the public exponent, d is the private exponent, and n is the modulus.
Interactive RSA Demo
Note: This uses small numbers for demonstration. Real RSA uses numbers with hundreds of digits!
Step 1: Generate Keys
Step 2: Encrypt a Message
Step 3: Decrypt the Message
Digital Signatures
Public key cryptography also enables digital signatures, which verify the authenticity and integrity of messages.
How Digital Signatures Work:
- Alice creates a hash of her message
- Alice encrypts the hash with her private key (this is the signature)
- Alice sends both the message and signature to Bob
- Bob decrypts the signature using Alice's public key
- Bob hashes the message and compares it with the decrypted signature
- If they match, the message is authentic and unmodified
Digital Signature Process
1. Signing (Alice)
Message β Hash β Encrypt with Private Key β Signature
2. Verification (Bob)
Signature β Decrypt with Public Key β Compare with Message Hash
β Verified: Message is authentic and unmodified
Try Digital Signatures
Real-World Applications
Public key cryptography is everywhere in modern technology:
HTTPS/SSL/TLS
Secures web browsing, ensuring that websites you visit are authentic and your data is encrypted.
Email Encryption
PGP and S/MIME use public key cryptography to encrypt emails and verify sender identity.
SSH
Secure Shell uses public key authentication for secure remote server access.
Cryptocurrencies
Bitcoin and other cryptocurrencies use public key cryptography for wallet addresses and transaction signing.
Code Signing
Software developers sign their applications to prove authenticity and prevent tampering.
Digital Certificates
X.509 certificates verify the identity of websites, servers, and individuals.
Diffie-Hellman Key Exchange
Another groundbreaking public key protocol is Diffie-Hellman key exchange, which allows two parties to establish a shared secret over an insecure channel without ever transmitting the secret itself.
The Paint Mixing Analogy
Imagine Alice and Bob want to create a shared secret color:
- They agree on a common base color (public, known to everyone)
- Alice picks a secret color and mixes it with the base
- Bob picks a secret color and mixes it with the base
- They exchange their mixed colors (public)
- Alice adds her secret color to Bob's mix
- Bob adds his secret color to Alice's mix
- They both end up with the same final color!
Eve can see all the public information but can't "unmix" the colors to find the secret.
Diffie-Hellman Visualization
Alice
Secret Color
Public Base
Bob
Secret Color
Strengths and Limitations
Strengths:
- Solves the key distribution problem
- Enables secure communication without prior key exchange
- Provides authentication through digital signatures
- Supports non-repudiation (signer cannot deny signing)
- Foundation of modern internet security
Limitations:
- Much slower than symmetric encryption (100-1000x slower)
- Requires larger key sizes for equivalent security
- Vulnerable to quantum computers (Shor's algorithm)
- Requires trusted certificate authorities for authentication
- More complex to implement correctly
In practice, public key cryptography is often used to securely exchange a symmetric key, which is then used for the actual data encryption (hybrid cryptosystem).
Key Sizes and Security
The security of public key cryptography depends on key length:
| RSA Key Size | Security Level | Status |
|---|---|---|
| 1024 bits | ~80 bits | β Deprecated (unsafe) |
| 2048 bits | ~112 bits | β Current minimum |
| 3072 bits | ~128 bits | β Recommended |
| 4096 bits | ~152 bits | β High security |
Summary
Key Points
- Public key cryptography uses two mathematically related keys: public and private
- The public key encrypts, the private key decrypts
- RSA security is based on the difficulty of factoring large numbers
- Digital signatures provide authentication and non-repudiation
- Diffie-Hellman enables secure key exchange over insecure channels
- Public key cryptography is slower but solves the key distribution problem
- It's the foundation of HTTPS, email encryption, and secure internet communication
- Hybrid systems combine public key and symmetric encryption for optimal security and performance