Asymmetric Encryption
Cryptography

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

1. Keypair Creation
Bob creates a keypair: a public key (shared openly) and a private key (guarded in secure storage)
2. Public Key Sharing
Alice gets Bob's public key from a directory or verified digital certificate
3. Asymmetric Encryption
Alice encrypts her message using Bob's public key. Once encrypted, even Alice cannot decrypt it!
4. Network Transmission
The ciphertext travels across the public internet to Bob's server
5. Interception Fails
An attacker who intercepts the ciphertext cannot read it without Bob's secret private key
6. Authorized Decryption
Bob uses his private key to decrypt the ciphertext and read the original message

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

  1. 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.

  2. 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.

  3. 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.

  4. 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

Asymmetric RSA Encryption and Decryption in Node.js / TypeScripttypescript
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")}`);
This TypeScript example demonstrates RSA public-key encryption. Notice the use of Optimal Asymmetric Encryption Padding (OAEP) with SHA-256. Using OAEP instead of older PKCS#1 v1.5 padding protects against Bleichenbacher-style padding attacks.

RSA (Rivest-Shamir-Adleman) vs ECC (Elliptic Curve Cryptography)

AspectRSA (Rivest-Shamir-Adleman)ECC (Elliptic Curve Cryptography)
Mathematical FoundationFactoring large composite numbers into prime productsDiscrete logarithm problem on algebraic elliptic curves
Key Size for 128-bit Security3,072 bits (larger key sizes and signatures)256 bits (compact, lightweight keys)
PerformanceSlower, requiring heavy calculations on large integersMuch faster, ideal for mobile devices, smartcards, and IoT
Bandwidth OverheadLarger certificate sizes and handshake dataSmall network payload and compact handshakes
Current StatusSupported everywhere for legacy systems, gradually phasing downModern industry standard (Ed25519, X25519, Curve25519)

Post-Quantum Cryptography (PQC) & NIST Standards

The cybersecurity community is actively preparing for the future arrival of cryptanalytically relevant quantum computers: • 'Harvest Now, Decrypt Later' (HNDL): Threat actors and intelligence agencies are known to record and store encrypted traffic today, intending to decrypt it years from now when powerful quantum computers become available. • NIST Standardized Algorithms (August 2024): NIST released official post-quantum standards including FIPS 203 (ML-KEM, based on Kyber for key encapsulation) and FIPS 204 (ML-DSA, based on Dilithium for digital signatures). • Hybrid Deployments: Major web browsers and content delivery networks have deployed hybrid key exchanges combining standard X25519 with ML-KEM during TLS 1.3 handshakes, protecting current web sessions against future quantum decryption.

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

  1. 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.
  2. 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.