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 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 and , the same public values as the Diffie–Hellman example. His private key is , so
and his public key is .
Alice wants to send . She picks .
She sends .
Bob computes , the same masking key. The inverse of 14 mod 23 is 5, since . So , Alice’s message.
Here’s the same example in Python:
def elgamal_encrypt(x, public_key, i):
p, g, B = public_key
k_E = pow(g, i, p) # ephemeral key
k_M = pow(B, i, p) # masking key
y = (x * k_M) % p
return (k_E, y)
def elgamal_decrypt(ciphertext, d, p):
k_E, y = ciphertext
k_M = pow(k_E, d, p)
return (y * pow(k_M, -1, p)) % p
p, g, d = 23, 5, 7
B = pow(g, d, p)
print("Bob's public key:", (p, g, B))
ciphertext = elgamal_encrypt(10, (p, g, B), 3)
print("Alice sends:", ciphertext)
print("Bob decrypts:", elgamal_decrypt(ciphertext, d, p))Bob's public key: (23, 5, 17)
Alice sends: (10, 2)
Bob decrypts: 10
Why a Fresh Every Time¶
Alice must pick a new random for every message. There are two reasons.
First, it makes the same message encrypt differently each time. With Bob’s key above, the message encrypts to , 3 or 1 when , 5 or 9:
for i in [3, 5, 9]:
print("i =", i, "gives", elgamal_encrypt(10, (23, 5, 17), i))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 repeats, repeats. If Eve ever learns one plaintext and its ciphertext (maybe the message always starts the same way), she can compute , and then read every other message sent with the same . 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 , so this example uses a bigger prime, , with the generator . Real ElGamal uses primes with thousands of bits, which leaves room for longer messages.
p = 2**31 - 1
g = 7
d = 123456789 # Bob's private key
B = pow(g, d, p) # part of Bob's public key
x = text_to_integer("hi")
print("message as a number:", x)
ciphertext = elgamal_encrypt(x, (p, g, B), 987654321)
print("Alice sends:", ciphertext)
decrypted = elgamal_decrypt(ciphertext, d, p)
print("Bob gets:", decrypted, integer_to_text(decrypted))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 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.