Search

Search papers and lessons

Public-Key Cryptography

Level 3 · 9 min read

Public-key cryptography gives each party a key pair: a public key anyone can see and a private key only its owner keeps, so strangers can communicate securely.

On this page

  • Colour = topic mix
  • Discrete math
  • Security & cryptography
  • Algorithms
  • Sign in to see your progress

TL;DR

In public-key cryptography, each party holds a pair of mathematically linked keys. The public key can be shared with anyone; the private key never leaves its owner. Anyone can encrypt a message with your public key, and only your private key can decrypt it. Your private key can also sign a message that anyone can check with your public key.

This solves the problem that two strangers cannot agree on a shared secret over a channel that an eavesdropper can read.

Where you'll see it: in HTTPS, where a browser and a server agree on a session key; in software signing, where a publisher signs an update so that users can verify it; and in messaging apps that exchange keys between devices.

This lesson explains how a cryptographic construction works. It is not advice about choosing or configuring cryptographic software.

Intuition

A mailbox with a slot

Picture a mailbox with a slot. Anyone can drop a letter through the slot, but only the owner has the key to open the box. The slot is the public key: it lets anyone send in. The key that opens the box is the private key. You can publish the slot's design on your website, and nobody who reads it can take letters back out.

Signing works the other way round. You seal a message with a stamp that only you can make, and anyone with a copy of your stamp design can check that the stamp is genuine.

Why the idea exists

Symmetric encryption, where both parties share one secret key, is fast and well understood. Its weakness is distribution: two parties who have never met have no secure way to agree on that key over an open network. Before 1976 the usual answer was a trusted courier or a pre-shared secret.

Public-key cryptography removes that step. The two parties can exchange public information in the open and end up with a shared secret that an eavesdropper, who sees all the public information, cannot compute. The mathematics that makes this work is a One-Way Function: easy to compute in one direction, infeasible to reverse without extra information.

The math

A public-key system consists of three algorithms:

  • KeyGen()→(pk,sk)\mathrm{KeyGen}() \to (pk, sk): generates a public key pkpk and a private key sksk.
  • Enc(pk,m)→c\mathrm{Enc}(pk, m) \to c: encrypts message mm into ciphertext cc.
  • Dec(sk,c)→m\mathrm{Dec}(sk, c) \to m: decrypts cc using the private key.

Correctness requires Dec(sk,Enc(pk,m))=m\mathrm{Dec}(sk, \mathrm{Enc}(pk, m)) = m for every message. Security requires that an attacker who knows pkpk and cc cannot recover mm with non-negligible probability. The keys are linked by a One-Way Function with a trapdoor: deriving sksk from pkpk must be infeasible, but the owner knows the trapdoor that makes decryption easy.

RSA

RSA uses modular exponentiation (see Modular Arithmetic). Choose two large primes pp and qq, and let n=pqn = pq and φ=(p−1)(q−1)\varphi = (p-1)(q-1).

  • Choose a public exponent ee with gcd⁡(e,φ)=1\gcd(e, \varphi) = 1.
  • Compute the private exponent dd with ed≡1(modφ)ed \equiv 1 \pmod{\varphi}.
  • Public key: (n,e)(n, e). Private key: dd (and the factors, which make decryption faster).
  • Encrypt: c=me mod nc = m^{e} \bmod n. Decrypt: m=cd mod nm = c^{d} \bmod n.

Decryption works because cd=medc^{d} = m^{ed}, and ed=1+kφed = 1 + k\varphi for some integer kk. Euler's theorem (a generalisation of Fermat's little theorem) then gives med≡m(modn)m^{ed} \equiv m \pmod n for messages m<nm \lt n that are coprime to nn; the identity also holds for the other messages, which can be checked prime by prime. The message must be encoded as a number below nn. Knowing φ\varphi is equivalent to knowing the factors of nn, so recovering dd from (n,e)(n, e) amounts to factoring nn, which is believed to be infeasible for large nn. That is the trapdoor.

Diffie–Hellman key exchange

Diffie–Hellman does not encrypt a message. It lets two parties agree on a shared secret. Fix a prime pp and a generator gg (both public). Alice picks a secret aa and publishes A=ga mod pA = g^{a} \bmod p. Bob picks a secret bb and publishes B=gb mod pB = g^{b} \bmod p. Each computes the shared secret from the other's public value:

Ba mod p=gba mod p=gab mod p=Ab mod p.B^{a} \bmod p = g^{ba} \bmod p = g^{ab} \bmod p = A^{b} \bmod p.

An eavesdropper sees pp, gg, AA and BB. Recovering aa from AA is the Discrete Logarithm Problem, which is assumed hard.

sequenceDiagram
    participant Alice
    participant Eve as Eavesdropper
    participant Bob
    Alice->>Bob: A = g^a mod p (public)
    Bob->>Alice: B = g^b mod p (public)
    Note over Eve: sees p, g, A and B only
    Note over Alice,Bob: both compute g^(ab) mod p

Signatures

To sign a message mm with RSA, the signer computes s=h(m)d mod ns = h(m)^{d} \bmod n, where hh is a hash function. Anyone checks by computing se mod ns^{e} \bmod n and comparing it with h(m)h(m). Only the holder of dd can produce a value that passes this check.

Worked examples

Example
Diffie–Hellman with small numbers

Public values: p=23p = 23, g=5g = 5. Alice's secret a=6a = 6, Bob's secret b=15b = 15.

  1. Alice publishes A=56 mod 23A = 5^{6} \bmod 23. Successive powers: 52=25≡25^{2} = 25 \equiv 2, 53≡105^{3} \equiv 10, 56≡100≡85^{6} \equiv 100 \equiv 8. So A=8A = 8.
  2. Bob publishes B=515 mod 23B = 5^{15} \bmod 23. Using 56≡85^{6} \equiv 8 and 512≡64≡185^{12} \equiv 64 \equiv 18, we get 515=512⋅53≡18⋅10=180≡195^{15} = 5^{12} \cdot 5^{3} \equiv 18 \cdot 10 = 180 \equiv 19. So B=19B = 19.
  3. Alice computes Ba=196 mod 23B^{a} = 19^{6} \bmod 23. Since 192≡1619^{2} \equiv 16, 193≡519^{3} \equiv 5, and 196≡25≡219^{6} \equiv 25 \equiv 2. Shared secret: 22.
  4. Bob computes Ab=815 mod 23A^{b} = 8^{15} \bmod 23, which also gives 22.
  5. An eavesdropper knows A=8A = 8 and B=19B = 19. For a modulus this small, trying a=1,2,…a = 1, 2, \dots until 5a≡85^{a} \equiv 8 finds a=6a = 6 in six steps. This is why real systems use primes hundreds of digits long.

Interpretation. Both sides reach the same secret 22 without ever sending it. At realistic sizes the eavesdropper's search is infeasible.

Example
RSA with the classic small primes

Take p=61p = 61, q=53q = 53, so n=3233n = 3233 and φ=60⋅52=3120\varphi = 60 \cdot 52 = 3120. Choose e=17e = 17. Then d=2753d = 2753, since 17⋅2753=46801=15⋅3120+117 \cdot 2753 = 46801 = 15 \cdot 3120 + 1.

  1. Public key: (3233,17)(3233, 17). Private key: d=2753d = 2753.
  2. Encrypt m=65m = 65: c=6517 mod 3233=2790c = 65^{17} \bmod 3233 = 2790.
  3. Decrypt c=2790c = 2790: m=27902753 mod 3233=65m = 2790^{2753} \bmod 3233 = 65.
  4. An attacker who knows only (3233,17)(3233, 17) must factor 3233=61×533233 = 61 \times 53 to find dd. Trial division finds the factors in a few dozen steps. At 2048-bit sizes that search is infeasible.

Interpretation. The example is correct but insecure by design: the modulus is tiny, so factoring is trivial. The mechanism is the same at real sizes, where only the parameters change.

Tradeoffs and failure modes

SituationProblemBetter choice
Bulk data encryptionPublic-key operations are slowUse public-key cryptography to agree on a key, then symmetric encryption for the data
Small or poorly chosen parametersFactoring or discrete logs become feasibleUse modern key sizes and standard curves
Unpadded RSA encryptionDeterministic encryption leaks information and allows attacksUse padded schemes such as OAEP
Unauthenticated Diffie–HellmanA man in the middle can impersonate either sideAuthenticate the public values with signatures or certificates
Quantum computersShor's algorithm breaks RSA and discrete-log schemes at scalePost-quantum public-key schemes
Lost private keyEncrypted data cannot be recoveredKey backup or escrow designed with the threat model in mind

When to use it. Use public-key cryptography when parties cannot share a secret in advance, or when you need signatures that third parties can verify.

When not to. Do not use it to encrypt large amounts of data directly. It is far slower than symmetric encryption, which is why protocols combine the two.

Alternatives. Symmetric Encryption is faster but needs a pre-shared key. Elliptic-curve schemes give comparable security with smaller keys and are common in modern protocols. Hybrid encryption combines a public-key step with a symmetric cipher.

History

Ralph Merkle proposed public-key distribution through hard puzzles in the mid-1970s. Whitfield Diffie and Martin Hellman (1976), in New Directions in Cryptography, proposed public-key key exchange and the idea of separate encryption and decryption keys. Ron Rivest, Adi Shamir and Leonard Adleman (1977, published 1978) gave the first practical public-key encryption and signature scheme, RSA.

Researchers at the UK's GCHQ had independently developed similar ideas earlier in the 1970s, but their work was classified until the late 1990s. The public nature of the 1976–1978 work is what made the approach spread: it let commercial and academic systems adopt it, and it became the foundation of secure internet protocols.

Check yourself

QuestionRecall: what does a public key let someone do, and what does it not let them do?Show answer

A public key lets anyone encrypt a message to its owner or verify a signature made by the owner. It does not let anyone decrypt messages or create signatures, because those operations need the private key.

QuestionComputation: with p=7p = 7 and g=3g = 3, Alice's secret is a=2a = 2. What is her public value AA?Show answer

A=32 mod 7=9 mod 7=2A = 3^{2} \bmod 7 = 9 \bmod 7 = 2.

QuestionWhy: why does Diffie–Hellman need authentication to be secure against an active attacker?Show answer

The protocol is secure against a passive eavesdropper, who only watches the public values. An active attacker can sit between the parties, replace each public value with its own, and establish a separate shared secret with each side. Each party then believes it is talking to the other, but it is talking to the attacker. Signatures or certificates that bind the public values to identities close this gap.

QuestionApplied: a messaging app sends the full message encrypted under RSA with a large public exponent for every message. What would you recommend?Show answer

Use RSA only to protect a short symmetric key, and encrypt the message itself with a symmetric cipher. RSA encryption is much slower per byte than symmetric encryption, and encrypting large data directly with unpadded RSA leaks information. The hybrid approach gets the key-distribution benefit of public-key cryptography at the speed of symmetric encryption.

Sources