Short answer: ElGamal encrypts an integer as a pair. The public key is g^x mod p. A ciphertext is (g^k mod p, m * (g^x)^k mod p). Decryption divides the second element by the first element raised to x, which is multiplication by the modular inverse. The fragment uses p = 23, g = 5, x = 6, m = 7, and k = 3, and the pair is 10 19. These are the equations on a group small enough to read. A deployment uses a group whose discrete logarithm is hard.
What the problem actually asks
The sender has to hide m using a published group element, without learning x. The ephemeral exponent k makes two encryptions of the same m differ. The fragment fixes k so the printed pair stays put. The group modulo 23 is a demonstration, in the same sense as a textbook RSA modulus built from two small primes. The ciphertext is two residues, not a padded byte string.
What the algorithm guarantees
With these parameters, raising the first residue to x and inverting it recovers 7 from the second residue. A different k produces a different pair for the same message. The map is not authenticated. A changed pair decrypts to some residue, or fails the range check, but the equations alone do not provide a tag. The discrete logarithm in a 23-element group is trivial. The equations are what the fragment checks.
The working steps
- Publish
p,g, andy = g^x mod p. Keepx. - Draw
kand sendc1 = g^k mod ptogether withc2 = m * y^k mod p. - Decrypt by computing
c2 * c1^(p - 1 - x) mod p, which multiplies by the inverse ofc1^x.
Approaches that look simpler and fail
| Approach | Why it fails in a group this small, or without a fresh exponent |
|---|---|
| Use modulo 23 as a deployed group | The discrete logarithm is a short search. A deployment uses a large group. |
Reuse one k for every message |
The ephemeral exponent is what makes equal messages encrypt differently. A fixed k is a fixed multiplier. |
Send m as a raw residue when m is a long message |
One residue holds one group element. A long message uses a symmetric key, and that key is what the residue hides. |
Skip the inverse and subtract x |
The second element is a product. Decryption multiplies by the modular inverse of c1^x. |
RSA hides an integer with a public exponent. ElGamal hides it with a published group element and an ephemeral exponent.
A working example
This Python fragment encrypts the integer 7 in the multiplicative group modulo 23. It prints 10 19.
def elgamal_demo() -> str:
p, g = 23, 5
private = 6
public = pow(g, private, p)
message, k = 7, 3
c1 = pow(g, k, p)
c2 = (message * pow(public, k, p)) % p
recovered = (c2 * pow(c1, p - 1 - private, p)) % p
assert recovered == message
return f"{c1} {c2}"
print(elgamal_demo())
Where it is used
OpenPGP used ElGamal to encrypt a session key, which is the historical GnuPG encryption algorithm. Some encrypted-voting designs add ElGamal ciphertexts because the product of ciphertexts encrypts the sum of the messages. A deployed mailbox today is more often an elliptic-curve encryption.
When you can drop this algorithm
Drop the 23-element group for any secret. Keep the pair of powers where the group is large, each encryption draws a fresh k, and a long message is carried by a symmetric key rather than by a single residue.
Why this stays published
We publish this so a search for ElGamal returns the ciphertext pair and the decryption by the inverse power. ElGamal sends g^k and m times y^k, and decryption multiplies by the inverse of g^(kx).
本文由 HoHo 與 AI 協作整理,最後更新於 2026 年 11 月 13 日。
