ElGamal turns a Diffie–Hellman shared group element into a mask for a message. This post covers encryption, why decryption works, and what its algebra lets us do to ciphertexts.
The group and the keys
Use a cyclic group of prime order , written multiplicatively, with generator . Every element is a power of , and exponents are taken modulo . A concrete example is a prime-order subgroup of the nonzero integers modulo a prime ; group multiplication is then multiplication modulo .
Alice chooses a secret exponent uniformly from and computes
Her public key is ; her private key is . Bob needs an authentic copy of her public key, or an attacker could substitute a different one.
The plaintext must be an element of . Arbitrary text or files are not automatically valid group elements. Message encoding or a hybrid encryption construction is a separate part of a complete system.
Encryption and decryption
Bob chooses a fresh, unpredictable exponent uniformly from , computes , and sends
Alice computes and recovers the message with the group inverse:
This works because both sides obtain the same mask:
Thus . In a modular group, the inverse is a modular inverse, not ordinary division. This is the generalized group form of the algorithm described in the Handbook of Applied Cryptography, §8.4.
A small worked example
Take , , and . The powers of 2 modulo 23 form the order-11 subgroup
Choose Alice’s private key , so . Bob wants to encrypt with :
The ciphertext is . Alice computes . Since , she recovers .
Here is the calculation in Python 3.8 or later. The tiny group and fixed exponents are for checking arithmetic only; they provide no security.
1p, q, g =23, 11, 2 2x, y, message =6, 7, 9 3assert pow(g, q, p) ==1and g !=1 4assert pow(message, q, p) ==1 5 6public_key = pow(g, x, p)
7c1 = pow(g, y, p)
8c2 = message * pow(public_key, y, p) % p
9shared = pow(c1, x, p)
10recovered = c2 * pow(shared, -1, p) % p
1112print(public_key, (c1, c2), recovered)
13assert recovered == message
Output:
118 (13, 8) 9
Multiplicative homomorphism
Write . Multiplying two ciphertexts component by component, under the same public key, gives
The resulting ciphertext decrypts to the product of the messages. The exponent sum is reduced modulo , and can be zero even though the original encryption exponents were nonzero. This identity concerns algebraic correctness, not a guarantee that combined ciphertexts have the same randomness distribution as fresh encryptions.
It also exposes malleability: anyone can replace by for a group element , causing decryption to return . An attacker can change the plaintext without learning it. Plain ElGamal therefore does not authenticate messages or provide chosen-ciphertext security.
What does root extraction mean here?
This property is about roots inside the chosen group, not ordinary real-number roots or recovery of the secret exponent.
Let be an integer relatively prime to . There is an exponent satisfying . For any , the element is its unique -th root, because
for some integer , using . Raising both ciphertext components to therefore produces
For our group, the inverse of 2 modulo 11 is 6. Squaring’s inverse is therefore raising to the sixth power. The square root of in this subgroup is , and . Raising the example ciphertext to the sixth power componentwise gives , which decrypts to 3.
This transformation reveals neither the plaintext nor the private key; it produces a ciphertext of a related plaintext. If , the inverse exponent does not exist and the argument fails. For example, every element of this order-11 group has eleventh power 1, so an arbitrary message cannot have a unique eleventh root.
Security assumptions and randomness
Efficiently computing discrete logarithms would reveal , but discrete-log hardness alone is not the full confidentiality claim. The usual proof of ElGamal’s security against chosen-plaintext attacks relies on the Decisional Diffie–Hellman (DDH) assumption in the selected group: a Diffie–Hellman mask should be computationally indistinguishable from a random group element. See the ElGamal discussion in Boneh and Shoup’s A Graduate Course in Applied Cryptography. Prime order alone does not establish that DDH holds.
Never reuse to encrypt different messages. Reuse gives the same mask and exposes
Knowing one message then reveals the other. With the worked example’s mask 6, encrypting 3 as well as 9 produces second components 18 and 8. Knowing the first message 9 recovers the mask as , and then recovers the other message as .
A deployable encryption protocol needs suitable group parameters, validation of received elements, secure randomness, authenticated public keys, and protection against ciphertext tampering. The arithmetic above explains the primitive; it is not a complete protocol for encrypting files or sending authenticated messages.