The ElGamal algorithm is an asymmetric key encryption algorithm for public key cryptography which is based on discrete logarithms. It was created by Taher ElGamal. The ElGamal algorithm is used in the free GNU Privacy Guard software, recent versions of PGP, and other cryptosystems. The Digital Signature Algorithm, even though it is a signature scheme and performs no encryption, is similar to ElGamal in many respects.
ElGamal can be defined over any cyclic group <math>G<math>. Its security depends upon the difficulty of a certain problem in <math>G<math> related to computing discrete logs (see below).
The algorithm
As with all asymmetric key encryption algorithms, ElGamal consists of three components: the key generator, the encryption algorithm, and the decryption algorithm.
The key generator works as follows:
 Alice generates an efficient description of a cyclic group <math>G<math> of order <math>q<math> with generator <math>g<math>. See below for specific examples of how this can be done.
 Alice chooses a random <math>x<math> from <math>\{0, \ldots, q1\}<math>.
 Alice computes <math>h = g^x<math>.
 Alice publishes <math>h<math>, along with the description of <math>G, q, g<math>, as her public key. Alice retains <math>x<math> as her secret key.
The encryption algorithm works as follows: to encrypt a message <math>m<math> to Alice under her public key <math>(G,q,g,h)<math>,
 Bob converts <math>m<math> into an element of <math>G<math>.
 Bob chooses a random <math>k<math> from <math>\{0, \ldots, q1\}<math>, then calculates <math>c_1=g^k<math> and <math>c_2=m\cdot h^k<math>.
 Bob sends the ciphertext <math>(c_1,c_2)<math> to Alice.
The decryption algorithm works as follows: to decrypt a ciphertext <math>(c_1,c_2)<math> with her secret key <math>x<math>,
 Alice computes <math>c_2/c_1^x<math> as the plaintext message.
Note that the decryption algorithm produces the intended message:
<math>\frac{c_2}{c_1^x} = \frac{m\cdot h^k}{g^{xk}} = \frac{m\cdot g^{xk}}{g^{xk}} = m<math>
If the space of possible messages is larger than the size of <math>G<math>, then the message can be split into several pieces and each piece can be encrypted independently. Typically, however, a short key to a symmetrickey cipher is first encrypted under ElGamal, and the (much longer) intended message is encrypted more efficiently using the symmetrickey cipher — this is termed hybrid encryption.
Security
ElGamal is a simple example of a semantically secure asymmetric key encryption algorithm (under reasonable assumptions). It is probabilistic, meaning that a single plaintext can be encrypted to many possible ciphertexts, with the consequence that a general ElGamal encryption produces a 2:1 expansion in size from plaintext to ciphertext.
ElGamal's security rests, in part, on the difficulty of solving the discrete logarithm problem in <math>G<math>. Specifically, if the discrete logarithm problem could be solved efficiently, then ElGamal would be broken. However, the security of ElGamal actually relies on the socalled Decisional DiffieHellman (DDH) assumption. This assumption is often stronger than the discrete log assumption, but is still believed to be true for many classes of groups.
Generating the group G
As described above, ElGamal can be defined over any cyclic group <math>G<math>, and is secure if a certain computational assumption (the "DDH Assumption") about that group is true. Unfortunately, the straightforward use of <math>G=Z_p<math> for a prime <math>p<math> is insecure, because the DDH Assumption is false in this group. (In contrast, note that computing discrete logs is believed to be hard in <math>Z_p<math>, but this is not enough for the security of ElGamal.)
The two most popular types of groups used in ElGamal are subgroups of <math>Z_p<math> and groups defined over certain elliptic curves. Here is one popular way of choosing an appropriate subgroup of <math>Z_p<math> which is believed to be secure:
 Choose a random large prime <math>p<math> such that <math>p1=kq<math> for some small integer <math>k<math> and large prime <math>q<math>. This can be done, for example with <math>k=2<math>, by first choosing a random large prime <math>q<math> and checking if <math>p=2q+1<math> is prime.
 Choose a random element <math>g \in Z_p<math> such that <math>g \neq 1<math> and <math>g^q=1 \bmod p<math>, i.e. such that <math>g<math> is of order <math>q<math>.
 The group <math>G<math> is the subgroup of <math>Z_p<math> generated by <math>g<math>, i.e. the set of <math>k<math>th residues mod <math>p<math>.
Note also that when encrypting, care must be taken to properly encode the message <math>m<math> as an element of <math>G<math>, and not, say, as just an arbitrary element of <math>Z_p<math>.
Efficiency
Encryption under ElGamal requires two exponentiations; however, these exponentiations are independent of the message and can be computed ahead of time if need be. Decryption only requires one exponentiation (plus one division, which is typically much faster). Unlike in the RSA and Rabin systems, ElGamal decryption cannot be sped up via the Chinese remainder theorem.
Miscellaneous
Note that ElGamal is malleable in an extreme way: for example, given a encryption <math>(c_1, c_2)<math> of some (possibly unknown) message <math>m<math>, one can easily construct an encryption <math>(c_1, 2 \cdot c_2)<math> of the message <math>2m<math>. Therefore ElGamal is not secure under chosenciphertext attack. On the other hand, the CramerShoup system (which is based on ElGamal) is secure under chosenciphertext attack.
References
 Taher ElGamal, "A PublicKey Cryptosystem and a Signature Scheme Based on Discrete Logarithms", IEEE Transactions on Information Theory, v. IT31, n. 4, 1985, pp469–472 or CRYPTO 84, pp10–18, SpringerVerlag.
 Handbook of Applied Cryptography (http://www.cacr.math.uwaterloo.ca/hac/), contains a detailed description of ElGamal Algorithm in Chapter 8 (http://www.cacr.math.uwaterloo.ca/hac/about/chap8.pdf) (PDF file).
