TExcelTAD
Home › Number Theory & Cryptography › ElGamal Encryption

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 gg of prime order qq (in Zp×\mathbb{Z}_p^\times or on an elliptic curve). Secret key x∈[1,q−1]x \in [1, q - 1], public key h=gxh = g^x.

Encryption of a group element mm: pick a fresh random kk and send

(c1,c2)=(gk, m⋅hk)(c_1, c_2) = (g^k,\ m \cdot h^k)

Decryption: m=c2⋅(c1x)−1m = c_2 \cdot (c_1^x)^{-1}, because c1x=gkx=hkc_1^x = g^{kx} = h^k.

It is a Diffie-Hellman exchange in which the sender's ephemeral key is kk, and the shared secret hkh^k is used as a one-time multiplicative mask.

python · try it yourself
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:

(gk1,m1hk1)⋅(gk2,m2hk2)=(gk1+k2, m1m2hk1+k2)(g^{k_1}, m_1 h^{k_1}) \cdot (g^{k_2}, m_2 h^{k_2}) = (g^{k_1 + k_2},\ m_1 m_2 h^{k_1 + k_2})

Exponential ElGamal encrypts gmg^m instead of mm, 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.

python · try it yourself
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 kk

If two messages are encrypted with the same kk, then c2/c2′=m/m′c_2 / c_2' = m / m': knowing one plaintext reveals the other.

python · try it yourself
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 xx 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 hkh^k.

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

(c1,c2)(c_1, c_2) 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 p=23p = 23, g=5g = 5, x=6x = 6 (so h=8h = 8), encrypt m=10m = 10 with k=3k = 3 and decrypt it.

Answer

(c1,c2)=(10,14)(c_1, c_2) = (10, 14), which decrypts to 10.

Solution

c1=53=125≡10c_1 = 5^3 = 125 \equiv 10. hk=83=512≡6h^k = 8^3 = 512 \equiv 6 (512=22⋅23+6512 = 22 \cdot 23 + 6), so c2=10⋅6=60≡14c_2 = 10 \cdot 6 = 60 \equiv 14. Decrypt: c1x=106 mod 23c_1^x = 10^6 \bmod 23; 102=100≡810^2 = 100 \equiv 8, 106≡83≡610^6 \equiv 8^3 \equiv 6, and 6−1≡46^{-1} \equiv 4 (24≡124 \equiv 1). 14⋅4=56≡1014 \cdot 4 = 56 \equiv 10.

Exercise 3: Re-randomization (code)

Show that multiplying a ciphertext by an encryption of 1, (gr,hr)(g^r, h^r), 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
python · try it yourself
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 mm is…

  1. me mod nm^e \bmod n
  2. (gk,mhk)(g^k, m h^k)
  3. gmg^m
  4. (m,h)(m, h)

Exponential ElGamal is used in voting because…

  1. it is faster
  2. multiplying ciphertexts adds the encrypted votes
  3. it has no keys
  4. it compresses ballots

If the same kk encrypts two messages…

  1. nothing happens
  2. the ratio of the plaintexts leaks
  3. the private key changes
  4. decryption fails