Asymmetric Encryption
A complete guide to asymmetric encryption, covering public and private keypairs, RSA prime factorization, Elliptic Curve Cryptography (ECC), and hybrid encryption.
In the mid-1970s, cryptographers solved a problem that had puzzled security thinkers for centuries: How can two people who have never met communicate in complete secrecy across an open network without first meeting in person to exchange a shared key? Consider the basic dilemma: If Alice wants to send a secret message to Bob across the internet using symmetric encryption, she must first send Bob the secret key. But if someone is eavesdropping on the network connection, they intercept the key and can read every message that follows. Whitfield Diffie, Martin Hellman, and later Ron Rivest, Adi Shamir, and Leonard Adleman (RSA) solved this puzzle with Public-Key Cryptography (also known as Asymmetric Encryption). Instead of a single shared key, asymmetric encryption uses a mathematically linked pair of keys: a Public Key (which you can freely share with anyone) and a Private Key (which you keep secret and never share). Anyone can use your public key to encrypt a message for you, but only your private key can decrypt it.
Definition
Asymmetric encryption (public-key cryptography) is a cryptographic system that uses a mathematically linked pair of keys: a public key used for encryption and signature verification, and a private key used for decryption and digital signature creation.
Why It Matters
Asymmetric encryption makes modern internet commerce, secure online banking, and private communications possible. When you visit an online store or banking website, your browser has never interacted with their servers before, and you have not pre-shared any password. Asymmetric cryptography enables your browser to verify the identity of the remote server and safely agree on a temporary shared session key in just a few milliseconds, even over thousands of miles of public network infrastructure.
How It Works
Asymmetric encryption relies on one-way mathematical functions with a 'trapdoor': operations that are easy to compute in one direction, but practically impossible to reverse unless you know a specific secret piece of information (the trapdoor): 1. RSA (The Prime Factorization Trapdoor): • Forward Direction: Multiplying two very large prime numbers (such as two 1,024-bit numbers) together is computationally fast: P * Q = N. • Reverse Direction: Given only the huge resulting number N, finding the original prime factors P and Q is extraordinarily difficult. Factoring a 2,048-bit or 4,096-bit number would take modern supercomputers billions of years. • The Public Key (e, N) is derived from N; the Private Key (d, N) is derived from the prime factors P and Q using Euler's totient function. 2. Elliptic Curve Cryptography (ECC, The Discrete Logarithm Trapdoor): Modern systems often use the geometry of elliptic curves over finite fields instead of giant prime numbers. • Point Multiplication: Taking a starting point G on a curve and multiplying it by an integer scalar k to reach point P (where P = k * G) is fast. • The Reverse Problem: Given only points P and G, finding the original scalar k is mathematically intractable with classical computers. • The Big Advantage: A 256-bit ECC key (such as Curve25519) provides the same cryptographic strength as a massive 3,072-bit RSA key, while running roughly ten times faster and using much less memory and battery power. 3. Hybrid Cryptosystems (How Production Systems Work): Asymmetric encryption involves heavy mathematical calculations and is not designed to encrypt large files directly. Because of this, real-world systems use hybrid cryptography: • Step A (Key Exchange): The client and server use asymmetric encryption or key exchange (like Ephemeral Elliptic Curve Diffie-Hellman, or ECDHE) to safely agree on a fresh, temporary 256-bit symmetric session key. • Step B (Bulk Transfer): Once both sides have the shared session key, they switch to fast symmetric encryption (such as AES-256-GCM) to transfer application data at high speed.
The Asymmetric Public-Key Encryption Pipeline
Key Points
- Asymmetric encryption uses two mathematically linked keys: a public key to encrypt and a private key to decrypt.
- You can share your public key with anyone in the world without weakening your security.
- Elliptic Curve Cryptography (ECC) achieves the same security as RSA with significantly smaller keys and faster performance.
- Asymmetric math is computationally heavy; production systems use it to negotiate symmetric keys rather than encrypt bulk payloads.
- Hybrid encryption combines the easy key sharing of asymmetric crypto with the raw speed of symmetric AES.
Steps
Choose Modern Algorithms
Prefer Elliptic Curve Cryptography (Curve25519 for key exchange, Ed25519 for digital signatures). When using RSA for backward compatibility, use at least 3,072 or 4,096 bits.
Isolate and Protect the Private Key
Generate and store private keys inside hardware security modules, TPM chips, or smartcards (like YubiKeys). Private keys must never sit unencrypted on accessible file systems.
Bind Public Keys to Identity with Certificates
Distribute public keys inside X.509 digital certificates signed by recognized Certificate Authorities (CAs) so users can be certain who owns the key.
Use Hybrid Encryption for Real Data
Do not encrypt files directly with RSA. Use asymmetric algorithms only to safely exchange or wrap symmetric AES session keys.
Examples
The Public Mailbox Analogy
Think of a public street mailbox. Anyone can walk up and drop an envelope through the slot (encrypting with the public key). Once the envelope falls inside, nobody on the street can reach in and pull it out. Only the postal worker with the metal door key (the private key) can open the mailbox and retrieve the letters.
Real-World Examples
SSH Key-Based Server Authentication
System administrators connect to remote Linux servers securely using SSH keypairs instead of passwords. You generate an Ed25519 keypair on your laptop and place the public key in the server's authorized_keys file. When you connect, the server sends a cryptographic challenge that only your local private key can solve, verifying your identity without transmitting any password over the network.
Technical Examples
import crypto from "crypto";
// 1. GENERATE AN RSA KEYPAIR
// 4096-bit key for strong security
const { publicKey, privateKey } = crypto.generateKeyPairSync("rsa", {
modulusLength: 4096,
publicKeyEncoding: { type: "spki", format: "pem" },
privateKeyEncoding: { type: "pkcs8", format: "pem" },
});
const secretSessionKey = "session-key-9482f710a3c48e2194b01e";
// 2. ENCRYPT WITH THE PUBLIC KEY (USING OAEP PADDING)
// OAEP padding prevents mathematical padding oracle attacks
const encryptedSessionKey = crypto.publicEncrypt(
{
key: publicKey,
padding: crypto.constants.RSA_PKCS1_OAEP_PADDING,
oaepHash: "sha256",
},
Buffer.from(secretSessionKey, "utf8")
);
console.log(`Encrypted (Base64): ${encryptedSessionKey.toString("base64").substring(0, 48)}...`);
// 3. DECRYPT WITH THE MATCHING PRIVATE KEY
const decryptedSessionKey = crypto.privateDecrypt(
{
key: privateKey,
padding: crypto.constants.RSA_PKCS1_OAEP_PADDING,
oaepHash: "sha256",
},
encryptedSessionKey
);
console.log(`Decrypted Session Key: ${decryptedSessionKey.toString("utf8")}`);RSA (Rivest-Shamir-Adleman) vs ECC (Elliptic Curve Cryptography)
| Aspect | RSA (Rivest-Shamir-Adleman) | ECC (Elliptic Curve Cryptography) |
|---|---|---|
| Mathematical Foundation | Factoring large composite numbers into prime products | Discrete logarithm problem on algebraic elliptic curves |
| Key Size for 128-bit Security | 3,072 bits (larger key sizes and signatures) | 256 bits (compact, lightweight keys) |
| Performance | Slower, requiring heavy calculations on large integers | Much faster, ideal for mobile devices, smartcards, and IoT |
| Bandwidth Overhead | Larger certificate sizes and handshake data | Small network payload and compact handshakes |
| Current Status | Supported everywhere for legacy systems, gradually phasing down | Modern industry standard (Ed25519, X25519, Curve25519) |
Post-Quantum Cryptography (PQC) & NIST Standards
Advantages
- Solves Key Distribution: Allows two parties to communicate securely without having to share a secret key beforehand.
- Enables Digital Signatures: Provides mathematical proof of who authored a message and ensures non-repudiation.
- Simplified Key Management: In an organization of N users, each person needs only one keypair (2N total keys) rather than hundreds of individual shared keys.
Limitations
- Vulnerable to Future Quantum Attacks: Shor's algorithm on a sufficiently large quantum computer could break classical RSA and ECC, driving the transition to post-quantum cryptography.
- Higher Computational Cost: Hundreds of times slower than symmetric AES, making it unsuitable for bulk data encryption.
Interview Questions
Explain how RSA key generation works from prime numbers to public and private exponents.
RSA begins by selecting two large prime numbers, p and q. The modulus n is computed as n = p * q, and Euler's totient function is calculated as phi(n) = (p - 1) * (q - 1). A public exponent e is selected such that 1 < e < phi(n) and gcd(e, phi(n)) = 1 (typically 65537). The private exponent d is calculated as the modular multiplicative inverse of e modulo phi(n). To encrypt a message m, compute c = m^e mod n. To decrypt, compute m = c^d mod n. An attacker who knows public values e and n cannot compute d without phi(n), which requires knowing the prime factors p and q.
How does Shor's Algorithm threaten asymmetric cryptography, and what are the new Post-Quantum standards?
Shor's quantum algorithm can solve prime factorization and discrete logarithms in polynomial time on a fault-tolerant quantum computer. This would break classical RSA, Diffie-Hellman, and Elliptic Curve Cryptography. In response, the US National Institute of Standards and Technology (NIST) standardized post-quantum cryptography algorithms based on lattice mathematics: ML-KEM (derived from Kyber) for general public-key encryption and key exchange, and ML-DSA (derived from Dilithium) and SLH-DSA (SPHINCS+) for digital signatures.
Practice Questions
- Create an Ed25519 SSH keypair on your computer using ssh-keygen -t ed25519 and compare the contents of the public .pub file with the private key file.
- Explain to a colleague how the Diffie-Hellman key exchange lets two people agree on a secret number over an open channel without an eavesdropper learning it.
