Study Atlas › Computer Science › Security

Public-key cryptography (Diffie–Hellman, RSA)

Two-key encryption: a public key to encrypt or verify, a private key to decrypt or sign.

Edited by Alex C, EduAlx · Sources cited below

Explanation

Diffie and Hellman (1976) introduced public-key key exchange; Rivest, Shamir and Adleman (1977–78) built RSA from the difficulty of factoring large numbers. Elliptic-curve cryptography gives the same security with shorter keys. Quantum computers running Shor's algorithm would break both, leading to post-quantum standards (NIST, 2024: ML-KEM, ML-DSA).

Example

Toy RSA: p = 61, q = 53, n = 3233, e = 17, d = 2753; m = 65 encrypts to c = 65^17 mod 3233 = 2790.

Code

p, q = 61, 53
n, phi = p*q, (p-1)*(q-1)
e = 17
d = pow(e, -1, phi)   # 2753
c = pow(65, e, n)      # 2790
assert pow(c, d, n) == 65

How it works, step by step

  1. Choose two large primes p and q
  2. Compute n = pq and φ(n)
  3. Choose e coprime to φ(n); compute d = e⁻¹ mod φ(n)
  4. Encrypt c = m^e mod n
  5. Decrypt m = c^d mod n

Open the interactive page with diagrams →

People behind it

References

  1. W. Diffie, M. E. Hellman, IEEE Trans. Inf. Theory 1976, 22, 644–654.
  2. R. L. Rivest, A. Shamir, L. Adleman, Commun. ACM 1978, 21, 120–126.

Textbooks

Related topics

Computer security fundamentals (CIA triad)Hash functions and digital signaturesProgramming paradigmsInformation theory and entropyCompilersWorld Wide Web, HTTP and HTMLMachine learningComputer networks and the TCP/IP modelNeural networks and backpropagationRelational databases and SQL