ElGamal Encryption
Taher ElGamal turned Diffie-Hellman into a public-key encryption scheme in 1985. Unlike textbook RSA it is randomized from the start, so equal messages give different ciphertexts. Its multiplicative structure makes it useful for electronic voting and mix-nets, and dangerous when a random value is reused.
The scheme
Key generation: a group with generator of prime order (in or on an elliptic curve). Secret key , public key .
Encryption of a group element : pick a fresh random and send
Decryption: , because .
It is a Diffie-Hellman exchange in which the sender's ephemeral key is , and the shared secret is used as a one-time multiplicative mask.
p, q, g = 2027, 1013, 4 # safe prime 2027 = 2*1013 + 1; g generates the order-q subgroup
x = 555 # private key
h = pow(g, x, p) # public key
def encrypt(m, k): # k must be fresh and unpredictable for every message
return pow(g, k, p), m * pow(h, k, p) % p
def decrypt(c1, c2):
return c2 * pow(pow(c1, x, p), -1, p) % p
m = 16 # a message encoded as a subgroup element (16 = 4^2)
for k in (101, 202, 303):
c = encrypt(m, k)
print("k =", k, " ciphertext:", c, " decrypts to:", decrypt(*c))k = 101 ciphertext: (655, 996) decrypts to: 16
k = 202 ciphertext: (1328, 1191) decrypts to: 16
k = 303 ciphertext: (257, 661) decrypts to: 16
Three encryptions of the same message look unrelated. Messages must be encoded as elements of the prime-order subgroup (for example squares modulo a safe prime); otherwise the Legendre symbol leaks a bit, as explained in The Discrete Logarithm Problem.
Homomorphic property
Multiplying two ciphertexts componentwise multiplies the plaintexts:
Exponential ElGamal encrypts instead of , which turns this into addition of votes: multiply all encrypted ballots, decrypt once, and take a small discrete log of the total. Nobody ever decrypts an individual vote.
p, q, g = 2027, 1013, 4
x = 555; h = pow(g, x, p)
enc = lambda m, k: (pow(g, k, p), pow(g, m, p) * pow(h, k, p) % p) # exponential ElGamal
votes = [1, 0, 1, 1, 0, 1, 1] # 1 = yes
ks = [17, 230, 451, 77, 909, 312, 640] # fresh randomness per voter
ballots = [enc(v, k) for v, k in zip(votes, ks)]
C1 = C2 = 1
for c1, c2 in ballots:
C1, C2 = C1 * c1 % p, C2 * c2 % p
gm = C2 * pow(pow(C1, x, p), -1, p) % p
tally = next(t for t in range(len(votes) + 1) if pow(g, t, p) == gm)
print("encrypted tally decrypts to", tally, "yes votes; true count:", sum(votes))encrypted tally decrypts to 5 yes votes; true count: 5
The same property means ElGamal is malleable: it is IND-CPA secure (under DDH) but not IND-CCA. Schemes such as Cramer-Shoup or the hybrid ECIES/DHIES add integrity checks.
Never reuse
If two messages are encrypted with the same , then : knowing one plaintext reveals the other.
p, q, g = 2027, 1013, 4
x = 555; h = pow(g, x, p)
k = 404 # reused by mistake
m_known, m_secret = 9, 25
c_a = (pow(g, k, p), m_known * pow(h, k, p) % p)
c_b = (pow(g, k, p), m_secret * pow(h, k, p) % p)
print("same c1:", c_a[0] == c_b[0])
recovered = c_b[1] * pow(c_a[1], -1, p) * m_known % p
print("attacker recovers:", recovered)same c1: True
attacker recovers: 25
The equal first components even announce the reuse. The same mistake in ElGamal-type signatures (DSA, ECDSA) leaks the private key itself (ECDSA Signatures).
In software
- GnuPG supports ElGamal encryption keys (historically paired with DSA signing keys).
- Helios and other verifiable voting systems use exponential ElGamal; threshold decryption splits among trustees.
- Modern hybrid encryption (ECIES, HPKE in RFC 9180) uses the ElGamal idea with a KDF and an authenticated symmetric cipher instead of multiplying by .
Exercises
Exercise 1: Ciphertext size
How long is an ElGamal ciphertext compared with the message, for a 2048-bit group?
Answer
Twice as long: two 2048-bit group elements.
Solution
are both group elements. The expansion factor 2 is the price of randomization; hybrid schemes avoid it by encrypting only a short symmetric key.
Exercise 2: By hand
With , , (so ), encrypt with and decrypt it.
Answer
, which decrypts to 10.
Solution
. (), so . Decrypt: ; , , and (). .
Exercise 3: Re-randomization (code)
Show that multiplying a ciphertext by an encryption of 1, , gives a new-looking ciphertext of the same message. (Mix-nets use this to shuffle votes.)
Answer
The new pair differs but decrypts to the same message.
Solution
p, g, x = 2027, 4, 555
h = pow(g, x, p)
c1, c2 = pow(g, 101, p), 16 * pow(h, 101, p) % p
r = 77
d1, d2 = c1 * pow(g, r, p) % p, c2 * pow(h, r, p) % p
dec = lambda a, b: b * pow(pow(a, x, p), -1, p) % p
print((c1, c2), (d1, d2), dec(c1, c2), dec(d1, d2))(655, 996) (401, 1525) 16 16
Test yourself
An ElGamal ciphertext of is…
Exponential ElGamal is used in voting because…
- it is faster
- multiplying ciphertexts adds the encrypted votes
- it has no keys
- it compresses ballots
If the same encrypts two messages…
- nothing happens
- the ratio of the plaintexts leaks
- the private key changes
- decryption fails