Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

ElGamal Encryption

Diffie–Hellman gives Alice and Bob a shared number, but it doesn’t send a message. In 1985 Taher Elgamal showed how to turn the same mathematics into public key encryption. (You’ll see the name written both “ElGamal” and “Elgamal”.)

The idea in one sentence: Alice does her half of a Diffie–Hellman exchange using Bob’s public key, which gives her a shared number, and she multiplies her message by that number. Bob can recompute the same shared number with his private key and divide it back out.

The Three Steps

Dividing mod pp means multiplying by an inverse, just like decrypting a multiplicative or affine cipher. For small numbers you can find the inverse by trying values. For large ones, use the Extended Euclidean Algorithm (see Cracking KidRSA) or Python’s built-in pow(k_M, -1, p).

Example

Bob uses p=23p = 23 and g=5g = 5, the same public values as the Diffie–Hellman example. His private key is d=7d = 7, so

B=57(mod23)=17B = 5^7 \pmod{23} = 17

and his public key is (23,5,17)(23, 5, 17).

Alice wants to send x=10x = 10. She picks i=3i = 3.

  • kE=53(mod23)=10k_E = 5^3 \pmod{23} = 10

  • kM=173(mod23)=14k_M = 17^3 \pmod{23} = 14

  • y=10⋅14(mod23)=140(mod23)=2y = 10 \cdot 14 \pmod{23} = 140 \pmod{23} = 2

She sends (kE,y)=(10,2)(k_E, y) = (10, 2).

Bob computes kM=107(mod23)=14k_M = 10^7 \pmod{23} = 14, the same masking key. The inverse of 14 mod 23 is 5, since 14⋅5=70=3⋅23+114 \cdot 5 = 70 = 3 \cdot 23 + 1. So x=2⋅5(mod23)=10x = 2 \cdot 5 \pmod{23} = 10, Alice’s message.

Here’s the same example in Python:

Bob's public key: (23, 5, 17)
Alice sends: (10, 2)
Bob decrypts: 10

Why a Fresh ii Every Time

Alice must pick a new random ii for every message. There are two reasons.

First, it makes the same message encrypt differently each time. With Bob’s key above, the message x=10x = 10 encrypts to y=2y = 2, 3 or 1 when i=3i = 3, 5 or 9:

i = 3 gives (10, 2)
i = 5 gives (20, 3)
i = 9 gives (11, 1)

An eavesdropper can’t even tell when the same message is sent twice. Compare that with a cipher that always turns the same plaintext into the same ciphertext, which leaks exactly that information.

Second, if ii repeats, kMk_M repeats. If Eve ever learns one plaintext xx and its ciphertext yy (maybe the message always starts the same way), she can compute kM=y⋅x−1(modp)k_M = y \cdot x^{-1} \pmod{p}, and then read every other message sent with the same ii. It’s the same mistake as reusing a one-time pad or a stream cipher’s keystream.

Encrypting Text

To encrypt text, turn it into a number: write each character as its 8-bit ASCII code (see converting text to binary) and read all the bits as one binary number. The toolkit’s text_to_integer and integer_to_text do this. The number has to be smaller than pp, so this example uses a bigger prime, p=231−1=2,147,483,647p = 2^{31} - 1 = 2,147,483,647, with the generator g=7g = 7. Real ElGamal uses primes with thousands of bits, which leaves room for longer messages.

message as a number: 26729
Alice sends: (1018773341, 1426530780)
Bob gets: 26729 hi

Where ElGamal Is Used

ElGamal was one of the encryption options in PGP and GnuPG email encryption, and its ideas show up in the elliptic curve systems at the end of this chapter. The security is the same as Diffie–Hellman’s: to find kMk_M from the public values, Eve has to solve a discrete log problem.

Like Diffie–Hellman, though, ElGamal has no way to check whose public key Alice is using. If an attacker can swap Bob’s public key for her own before Alice encrypts, Alice will happily encrypt her message for the attacker. The next section, Man-in-the-Middle Attacks, shows how.