On this page
T. ElGamal published a public-key system built on discrete logarithms in 1985, in IEEE Transactions on Information Theory. It shows how a stranger can send you a secret using only powers, remainders and inverses.
What is ElGamal encryption?
ElGamal encryption is a public-key scheme that can be viewed as Diffie-Hellman key agreement used to carry a message, according to the Handbook of Applied Cryptography, chapter 8. Anyone can encrypt with your public key; only you can decrypt.
It works with the numbers 1 to p − 1 modulo a prime p, using a generator g: a number whose powers run through every one of those values. If that idea is new, read our explainer on primitive roots modulo a prime first.
How do ElGamal key generation, encryption and decryption work?
The steps below follow Algorithms 8.17 and 8.18 of the Handbook of Applied Cryptography.
Key generation: choose a large prime p and a generator g. Pick a secret integer x with 1 ≤ x ≤ p − 2 and compute h = gˣ mod p. Publish (p, g, h); keep x.
Encryption: write the message as a number m from 0 to p − 1. Pick a fresh random k with 1 ≤ k ≤ p − 2. Send the pair c₁ = gᵏ mod p and c₂ = m·hᵏ mod p.
Decryption: compute s = c₁ˣ mod p. Then m = c₂·s⁻¹ mod p, where s⁻¹ is the inverse of s modulo p.
Step 3 works because c₁ˣ = (gᵏ)ˣ = (gˣ)ᵏ = hᵏ. The sender masked m with hᵏ; you rebuild the same mask from the other side and divide it out.
To compute these powers by hand, use modular exponentiation by square and multiply.
A worked ElGamal example with p = 23
Take p = 23 and g = 5. The powers of 5 modulo 23 are 1, 5, 2, 10, 4, 20, 8, 17, 16, 11, 9, 22, 18, 21, 13, 19, 3, 15, 6, 7, 12, 14 and then 1 again. That is every non-zero remainder, so 5 is a generator.
Key generation. Choose the secret x = 6. Since 5² = 25 ≡ 2, h = 5⁶ ≡ 2³ = 8. Your public key is (23, 5, 8).
Encryption. A friend wants to send m = 10 and picks k = 3.
c₁ = 5³ = 125 = 5 × 23 + 10, so c₁ = 10.
hᵏ = 8³ = 512 = 22 × 23 + 6, so the mask is 6.
c₂ = 10 × 6 = 60 = 2 × 23 + 14, so c₂ = 14.
The ciphertext is (10, 14).
Decryption. You compute s = 10⁶ mod 23. Since 10² = 100 ≡ 8 and 10⁴ ≡ 8² = 64 ≡ 18, you get 10⁶ ≡ 18 × 8 = 144 ≡ 6. The inverse of 6 modulo 23 is 4, because 6 × 4 = 24 ≡ 1. So m = 14 × 4 = 56 ≡ 10.
Now try it yourself: encrypt m = 7 with k = 5 under the same key, then decrypt. You should get the ciphertext (20, 20).
Why does ElGamal rest on the discrete logarithm problem?
Finding x from h = gˣ mod p, the discrete logarithm, would decrypt everything. The handbook states that recovering m from the public key and ciphertext is equivalent to solving the Diffie-Hellman problem, and that ElGamal is said to rest on the discrete logarithm problem, although that equivalence has not been proven (Handbook of Applied Cryptography, Note 8.23). Our post on the discrete logarithm problem shows why finding x becomes slow once p is large.
With p = 23 you can find x by listing powers, so real primes are huge: the handbook recommended a modulus of at least 768 bits as of 1996, and 1024 bits or more for long-term security (Note 8.24). Those figures are far too small today; check current standards before choosing a size. The ciphertext is also twice as long as the plaintext (Note 8.21).
What happens if you reuse k in ElGamal?
The handbook calls it critical that a different random k is used for every message (Note 8.23). With the same k, both ciphertexts carry the same mask hᵏ, so c₂/c₂′ = m₁/m₂, and anyone who knows m₁ can compute m₂.
Suppose your friend also sends m₂ = 7 with the same k = 3. Since 7 × 6 = 42 ≡ 19, the ciphertext is (10, 19). The repeated c₁ = 10 gives it away. An attacker who knows the first message was 10 recovers the mask: 10⁻¹ is 7 (because 70 ≡ 1), so the mask is 14 × 7 = 98 ≡ 6. Then m₂ = 19 × 6⁻¹ = 19 × 4 = 76 ≡ 7. No secret key needed.
To see how generators and discrete logarithms connect, explore the Fermi Concept Library, where each concept has its own lesson and place in the map. To practise, the Fermi Problem Library has worked problems with a step-by-step solver that explains the reasoning rather than printing an answer.



