Skip to main content
Logo image

Section 16.3 ElGamal crypto system

The ElGamal encryption system is a public key encryption algorithm by Taher Elgamal [3] in 1985 that is based on the Diffie-Hellman key exchange. We give an introduction to the ElGamal Encryption System and an example in the video in Figure 16.20.
Figure 16.20. ElGamal Encryption System by Matt Farmer and Stephen Steward
We assume that the message m that Alice encrypts and sends to Bob is an integer. In Chapter 12 we saw how a message can be encoded into integers. We describe the three components of ElGamal encryption, namely key generation, encryption, and decryption.
Bob: Key Generation
To generate his private key and his public key Bob does the following.
  1. Bob chooses a prime p and and a generator gZp.
  2. Bob chooses a random bN.
  3. Bob computes B=gb in (Zp,).
  4. Bob publishes his public key p, g, B in the key directory.
Alice: Encryption
To encrypt a message mZp Alice does the following.
  1. Alice gets Bob’s public key p, g, B from the key directory.
  2. Alice chooses a random aN.
  3. Alice computes the shared secret s=Ba.
  4. Alice computes A=ga.
  5. Alice encrypts m by computing X=ms.
  6. Alice sends (A,X) to Bob.
Bob: Decryption
The information available to Bob to decrypt a message are his private key b and his public key consisting of the prime p, the generator g, and B=gb. To decrypt a message (A,X) Bob does the following.
  1. Bob receives (A,X) from Alice.
  2. Bob computes the shared secret s=Ab.
  3. Bob computes the inverse s1 of s in (Zp,).
  4. Bob decrypts the message by computing M=Xs1.
We now show that the message M received by Bob is equal to Alice’s plain text message m. We have
M=Xs1=(ms)s1=m(ss1)=m1=m.
Figure 16.21. ElGamal Encryption System using the group (Zp,) where :Zp×ZpZp is given by ab=(ab)modp. The inverse of sZp with respect to is denoted by s1 and bn=(bn)modp.
Investigate the dependencies of the steps in the ElGamal crypto system in the interactive Example 16.22.

Example 16.22. ElGamal interactive.

In Checkpoint 16.23 Fill in the blanks to complete the description of key generation, encryption, and decryption following the ElGamal protocol.

Checkpoint 16.23. ElGamal protocol.

When using the ElGamal cryptosystem Bob and Alice do the following.
Bob: Key generation
To generate his public key Bob chooses a prime number p and a generator g in the group ({1,2,3,...,p1},) where ab=(ab)modp.
In the dropdown menus we write g^c for gc.
Bob chooses an element b in {1,2,3,...,p1} and computes B :=
  • select
  • g*a
  • g*b
  • g^a
  • g^b
  • g^p
  • A^b
  • B^a
  • m*s
  • X*t
.
Bob publishes his public key
  • select
  • (a,X)
  • (A,X)
  • (B,X)
  • (g,b,B)
  • (p,g,b)
  • (p,g,B)
  • (p,b,B)
.
Alice: Encryption
Alice wants to send the secret message m to Bob.
Alice obtains Bob’s public key
  • select
  • (a,X)
  • (A,X)
  • (B,X)
  • (g,b,B)
  • (p,g,b)
  • (p,g,B)
  • (p,b,B)
from the public key directory.
Alices chooses a in {1,2,3,...,p1} and computes A :=
  • select
  • g*a
  • g*b
  • g^a
  • g^b
  • g^p
  • A^b
  • B^a
  • m*s
  • X*t
.
Alice computes the shared secret s :=
  • select
  • g*a
  • g*b
  • g^a
  • g^b
  • g^p
  • A^b
  • B^a
  • m*s
  • X*t
.
To encrypt m in {1,2,3,...,p1} Alice computes X :=
  • select
  • g*a
  • g*b
  • g^a
  • g^b
  • g^p
  • A^b
  • B^a
  • m*s
  • X*t
Alice sends
  • select
  • (a,X)
  • (A,X)
  • (A,m)
  • (B,X)
  • (s,m)
  • (s,X)
to Bob.
Bob: Decryption
Bob receives
  • select
  • (a,X)
  • (A,X)
  • (A,m)
  • (B,X)
  • (s,m)
  • (s,X)
from Alice
Bob computes the shared secret
  • select
  • g*a
  • g*b
  • g^a
  • g^b
  • g^p
  • A^b
  • B^a
  • m*s
  • X*t
.
Bob computes the inverse t of s in the group ({1,2,3,...,p1},).
Bob obtains the message m by computing
  • select
  • g*a
  • g*b
  • g^a
  • g^b
  • g^p
  • A^b
  • B^a
  • m*s
  • X*t
.
We work through a small example.

Example 16.24. ElGamal with small numbers.

We follow the steps above to generate a private and a public key, encrypt a message, and decrypt a message.
Bob: Key Generation
First Bob chooses the group, the generator, and his private key and computes and publishes his public key.
  1. Bob chooses the prime p=29 and g=2.
  2. Bob chooses b=5 as his private key,
  3. Bob computes B=25=(25)mod29=32mod29=3.
  4. Bob publishes his public key p=29, g=2, B=3 in the public key directory.
Alice: Encryption
Alice wants to send the secret message m=6 to Bob.
  1. Alice obtains p=29, g=2, B=3 from the public key directory.
  2. Alice chooses her secret a=4.
  3. Alice computes the shared secret s=Ba =34= (34)mod29 =81mod29=23.
  4. Alice computes A=ga=24=16.
  5. Alice encrypts the message m=6 as X=ms =623 =138mod29=22.
  6. Alice sends (A,X)=(16,22) to Bob.
Bob: Decryption
Bob uses A and his private key b to decrypt the message
  1. Bob computes the shared secret
    s=Ab =165 =16416 =(162)2 =24216 =2516= 400mod29=23.
  2. Bob finds the inverse s1=24 of s=23 in (Z29,). This can be done with the Euclidean algorithm and Bézout’s identity.
  3. Bob decrypts the message by computing Xs1=2224=528mod29=6, which is Alice’s original message.
In Checkpoint 16.25 work through the steps of key generation, encryption, and decryption yourself.

Checkpoint 16.25. ElGamal with small numbers.

Alice and Bob use the ElGamal cryptosystem for their secure communication.
Bob: Key Generation
Bob chooses the prime p=11. So he will work in the group (Z11,) where ab=(ab)mod11. He chooses g=2Z11.
Bob chooses his secret key b=7 and computes B=(gb)modp=.
Bob publishes p, g, and B in the public key directory.
Directory of Public Keys
Aaron: p=53, g=33, B=29
Alice: p=23, g=2, B=16
Bob: p=, g=, B=
Sebastian: p=23, g=2, B=4
Victoria: p=43, g=39, B=4
Alice: Encryption
Alice wants to send the message m=4 to Bob.
Alice gets Bob’s public key from the directory: p=, g=, B=
Alice chooses her secret a=2.
Alice computes the shared secret s=(Ba)modp=.
She computes A=(ga)modp=.
Alice encrypts the message by computing X=(ms)modp=.
Alice sends A and X to Bob.
Bob: Decryption
Bob receives A and X from Alice.
Bob computes the shared secret s=(Ab)modp= .
Bob finds the inverse s1= of s in the group (Z11,).
Bob decrypts the message by computing M=(Xs1)modp= .
Hint:
In (Z11,) we have
11=1,
21=6,
31=4,
41=3,
51=9,
61=2,
71=8,
81=7,
91=5,
101=10,
Considering only Bob’s side we get:

Problem 16.26. ElGamal decryption.

Bob has published his public key p=13, g=7, B=10. His private key is b=2. Alice sends his the encrypted message (3,8). What is the plain text of the message ?
Solution.
From Alice’s message Bob gets A=3 and X=8. So the shared secret is s=Ab=32=9. The inverse of s=9 is s1=3, since 93=27mod13=1. Bob decrypts the message by computing Xs1=83=24mod13=11.

Checkpoint 16.27. ElGamal: Bob’s perspective.

Alice and Bob use the ElGamal cryptosystem for their secure communication.
Bob: Key Generation
Bob chooses the prime p=19813 and the generator g=2Z19813.
Bob chooses his secret key b=2Z19813 and computes B=(gb)mod19813=.
Bob publishes p, g, and B in the public key directory.
Directory of Public Keys Aaron: p=19861, g=2163, B=4818
Beth: p=19861, g=3530, B=17045
Bob: p=, g=, B=
Sebastian: p=19861, g=2163, B=12868
Victoria: p=19867, g=128, B=4040
Bob: Decryption
Bob receives A=8 and X=19077 from Alice.
Bob computes the shared secret s=(Ab)mod19813= .
Bob computes the inverse s1=5882 of s in the group (Z19813,).
Bob decrypts the message by computing M=(Xs1)mod19813= .
Bob finds the expanded base 27 form of M, namely M=272+27+.
Decoding these numbers with C1 yields the message .
Considering only Alice’s side we get:

Problem 16.28. ElGamal encryption.

Alice and Bob use the ElGamal crypto system for their secure communication. From the key directory Alice obtains Bob’s public key is p=5, g=2, B=4. Alice chooses her secret a=2. Alice encrypts the message m=4. What does she send to Bob ?
Solution.
Alice computes:
A=(ga)mod5=22mod5=4mod5=4s=(Ba)mod5=42mod5=16mod5=1X=(ms)modp=(41)mod5=4
Thus Alice sends (A,X)=(4,4) to Bob.

Checkpoint 16.29. ElGamal: Alice’s perspective.

Alice and Bob use the ElGamal cryptosystem for their secure communication. Alice sends an encrypted message to Bob.
Directory of Public Keys
Bob: p=19813, g=2, B=4
Nathan: p=19861, g=3530, B=17045
Thom: p=19861, g=2163, B=12868
Alice: Encryption
Alice wants to send the message ’mom’ to Bob.
Alice gets Bob’s public key from the directory: p=, g=, B=
She applies the encoding function C:{,a,b,c,...z}{0,1,2,3,...26} with C()=0, C(a)=1, , C(z)=26 to the characters in message. She obtains C(m)=, C(o)=, and C(m)=.
She encodes this into one number by computing m=C(m)272+C(o)27+C(m)=.
Alice chooses her secret a=2.
Alice computes the shared secret s=(Ba)modp=.
She computes A=(ga)modp=.
Alice encrypts the message by computing X=(ms)modp=.
Alice sends A and X to Bob.
We end with an example that includes the encoding of a message.

Example 16.30. ElGamal with encoding and decoding.

Alice and Bob use the ElGamal crypto system for their secure communication. In the following we present all steps involved in Alice sending an encrypted message to Bob. For encoding text into numbers we apply the method from Chapter 12.
Bob: Key Generation
Bob chooses the prime p=19777 and the generator g=11Z19777. Bob chooses his secret key b=3 and computes B=(gb)modp=1331. Bob publishes p, g, and B in the public key directory.
Directory of Public Keys
Aaron: p=19841 g=243 B=4821
Beth: p=19867 g=128 B=15522
Bob: p=19777 g=11 B=1331
Sebastian: p=19891 g=32 B=7297
Victoria: p=19913 g=2187 B=5531
Alice: Encoding and Encryption
Alice wants to send the message bat to Bob. Alice gets Bob’s public key from the directory: p=19777, g=11, B=1331. She applies the encoding function
C:{,a,b,c,...z}{0,1,2,3,...26}
with C()=0, C(a)=1, , C(z)=26 to the characters in the message. She obtains C(b)=2, C(a)=1, and C(t)=20. She encodes this into one number by computing m=C(b)272+C(a)27+C(t)=1505.
Alice chooses her secret a=2. Alice computes the shared secret s=(Ba)modp=11408. She computes A=(ga)modp=121
Alice encrypts the message by computing X=(ms)modp=2604. Alice sends A and X to Bob.
Bob: Decryption and Decoding
Bob receives A and X from Alice.
Bob computes the shared secret s=(Ab)modp=11408. Bob computes the inverse s1=14727 of s in the group (Z19777,).
Bob decrypts the message by computing M=(Xs1)modp=1505.
Bob finds the expanded base 27 form of M, namely M=2272+127+20. Decoding these numbers with C1 yields the message bat.

Checkpoint 16.31. ElGamal with encoding and decoding.

Alice and Bob use the El Gamal crypto system for their secure communication.
Bob: Key Generation
Bob chooses the prime p=19853 and the generator g=2Z19853.
Bob chooses his secret key b=2 and computes B=(gb)modp=.
Bob publishes p, g, and B in the public key directory.
Directory of Public Keys Aaron: p=19843, g=15567, B=14339
Beth: p=19853, g=128, B=6782
Bob: p=, g=, B=
Sebastian: p=19861, g=2163, B=12868
Victoria: p=19913, g=2187, B=11065
Alice: Encryption
Alice wants to send the message ’hit’ to Bob.
Alice gets Bob’s public key from the directory: p=, g=, B=
She applies the encoding function C:{,a,b,c,...z}{0,1,2,3,...26} with C()=0, C(a)=1, , C(z)=26 to the characters in message. She obtains C(h)=, C(i)=, and C(t)=.
She encodes this into one number by computing m=C(h)272+C(i)27+C(t)=.
Alice chooses her secret a=3.
Alice computes the shared secret s=(Ba)modp=.
She computes A=(ga)modp=.
Alice encrypts the message by computing X=(ms)modp=.
Alice sends A and X to Bob.
Bob: Decryption
Bob receives A and X from Alice.
Bob computes the shared secret s=(Ab)modp= .
Bob computes the inverse s1=18302 of s in the group (Z19853,).
Bob decrypts the message by computing M=(Xs1)modp= .
Bob finds the expanded base 27 form of M, namely M=272+27+.
Decoding these numbers with C1 yields the message .
In real world applications p is chosen much larger.