The Wayback Machine - https://web.archive.org/web/20240627201801/https://www.geeksforgeeks.org/elgamal-encryption-algorithm/
Open In App

ElGamal Encryption Algorithm

Last Updated : 22 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

ElGamal encryption is a public-key cryptosystem. It uses asymmetric key encryption for communicating between two parties and encrypting the message. This cryptosystem is based on the difficulty of finding discrete logarithm in a cyclic group that is even if we know ga and gk, it is extremely difficult to compute gak.

Idea of ElGamal cryptosystem: 

Suppose Alice wants to communicate with Bob.  

  1. Bob generates public and private keys: 
    • Bob chooses a very large number q and a cyclic group Fq.
    • From the cyclic group Fq, he choose any element g and
      an element a such that gcd(a, q) = 1.
    • Then he computes h = ga.
    • Bob publishes F, h = ga, q, and g as his public key and retains a as private key.
  2. Alice encrypts data using Bob’s public key : 
    • Alice selects an element k from cyclic group F 
      such that gcd(k, q) = 1.
    • Then she computes p = gk and s = hk = gak.
    • She multiples s with M.
    • Then she sends (p, M*s) = (gk, M*s).
  3. Bob decrypts the message : 
    • Bob calculates s′ = pa = gak.
    • He divides M*s by s′ to obtain M as s = s′.

Following is the implementation of the ElGamal cryptosystem in Python 

Python3




# Python program to illustrate ElGamal encryption
 
import random
from math import pow
 
a = random.randint(2, 10)
 
def gcd(a, b):
    if a < b:
        return gcd(b, a)
    elif a % b == 0:
        return b;
    else:
        return gcd(b, a % b)
 
# Generating large random numbers
def gen_key(q):
 
    key = random.randint(pow(10, 20), q)
    while gcd(q, key) != 1:
        key = random.randint(pow(10, 20), q)
 
    return key
 
# Modular exponentiation
def power(a, b, c):
    x = 1
    y = a
 
    while b > 0:
        if b % 2 != 0:
            x = (x * y) % c;
        y = (y * y) % c
        b = int(b / 2)
 
    return x % c
 
# Asymmetric encryption
def encrypt(msg, q, h, g):
 
    en_msg = []
 
    k = gen_key(q)# Private key for sender
    s = power(h, k, q)
    p = power(g, k, q)
     
    for i in range(0, len(msg)):
        en_msg.append(msg[i])
 
    print("g^k used : ", p)
    print("g^ak used : ", s)
    for i in range(0, len(en_msg)):
        en_msg[i] = s * ord(en_msg[i])
 
    return en_msg, p
 
def decrypt(en_msg, p, key, q):
 
    dr_msg = []
    h = power(p, key, q)
    for i in range(0, len(en_msg)):
        dr_msg.append(chr(int(en_msg[i]/h)))
         
    return dr_msg
 
# Driver code
def main():
 
    msg = 'encryption'
    print("Original Message :", msg)
 
    q = random.randint(pow(10, 20), pow(10, 50))
    g = random.randint(2, q)
 
    key = gen_key(q)# Private key for receiver
    h = power(g, key, q)
    print("g used : ", g)
    print("g^a used : ", h)
 
    en_msg, p = encrypt(msg, q, h, g)
    dr_msg = decrypt(en_msg, p, key, q)
    dmsg = ''.join(dr_msg)
    print("Decrypted Message :", dmsg);
 
 
if __name__ == '__main__':
    main()


Sample Output:

Original Message : encryption
g used :  5860696954522417707188952371547944035333315907890
g^a used :  4711309755639364289552454834506215144653958055252
g^k used :  12475188089503227615789015740709091911412567126782
g^ak used :  39448787632167136161153337226654906357756740068295
Decrypted Message : encryption

In this cryptosystem, the original message M is masked by multiplying gak to it. To remove the mask, a clue is given in form of gk. Unless someone knows a, he will not be able to retrieve M. This is because finding discrete log in a cyclic group is difficult and simplifying knowing ga and gk is not good enough to compute gak.

Advantages:

  • Security: ElGamal is based on the discrete logarithm problem, which is considered to be a hard problem to solve. This makes it secure against attacks from hackers.
  • Key distribution: The encryption and decryption keys are different, making it easier to distribute keys securely. This allows for secure communication between multiple parties.
  • Digital signatures: ElGamal can also be used for digital signatures, which allows for secure authentication of messages.

Disadvantages:

  • Slow processing: ElGamal is slower compared to other encryption algorithms, especially when used with long keys. This can make it impractical for certain applications that require fast processing speeds.
  • Key size: ElGamal requires larger key sizes to achieve the same level of security as other algorithms. This can make it more difficult to use in some applications.
  • Vulnerability to certain attacks: ElGamal is vulnerable to attacks based on the discrete logarithm problem, such as the index calculus algorithm. This can reduce the security of the algorithm in certain situations.


Previous Article
Next Article

Similar Reads

Difference between Software Encryption and Hardware Encryption
1. Software Encryption: Software Encryption, as the name suggests, is a process of keeping data safe using the software. In this, the software is generally installed in the host computer that encrypts and decrypts data. It is more cost-effective for smaller companies. In this, the password is the key that one needs to have access to data. It usuall
5 min read
RC4 Encryption Algorithm
RC4 is a stream cipher and variable-length key algorithm. This algorithm encrypts one byte at a time (or larger units at a time). A key input is a pseudorandom bit generator that produces a stream 8-bit number that is unpredictable without knowledge of input key, The output of the generator is called key-stream, is combined one byte at a time with
5 min read
Twofish Encryption Algorithm
When it comes to data protection, encryption methods act as our buffering agents. One example of an excellent block cipher is the Twofish encryption algorithm. Although it was a competitor of another block cipher in the Advanced Encryption Standard competition and was later succeeded by the latter, it can still be used as a safe approach to protect
6 min read
RC5 Encryption Algorithm
RC5 is a symmetric key block encryption algorithm designed by Ron Rivest in 1994. It is notable for being simple, fast (on account of using only primitive computer operations like XOR, shift, etc.) and consumes less memory. Example: Key : 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00Plain Text : 00000000 00000000Cipher Text : EEDBA521 6D8F4B15RC5
9 min read
Knapsack Encryption Algorithm in Cryptography
Knapsack Encryption Algorithm is the first general public key cryptography algorithm. It was developed by Ralph Merkle and Mertin Hellman in 1978. As it is a Public key cryptography, it needs two different keys. One is the Public key which is used for the Encryption process and the other one is the Private key which is used for the Decryption proce
6 min read
End to End Encryption (E2EE) in Computer Networks
What is Encryption? Have you ever wondered how our emails, texts, photos, videos are sent over the Internet? Whenever you send anything over the Internet, it passes through a lot of servers, routers, and other devices where anyone can intercept it and get access to your private information. Thus, to tackle such mishap, encryption was introduced. In
2 min read
Difference between Encryption and Decryption
Encryption is the process of converting normal message (plaintext) into meaningless message (Ciphertext). Whereas Decryption is the process of converting meaningless message (Ciphertext) into its original form (Plaintext). The major distinction between secret writing associated secret writing is that the conversion of a message into an unintelligib
2 min read
Strength of Data encryption standard (DES)
Data encryption standard (DES) is a symmetric key block cipher algorithm. The algorithm is based on Feistel network. The algorithm uses a 56-bit key to encrypt data in 64-bit blocks. There are mainly two categories of concerns about the strength of Data encryption standard. They are: Concerns about the particular algorithm used. Concerns about the
2 min read
What is E2EE(End to End Encryption)?
E2EE or End to End Encryption refers to the process in which encryption of data are being done at the end host. It is an implementation of Asymmetric encryption and hence ensures a secure way of data communication. It is the most secure way to communicate privately and securely as data can be read-only by the sender and the receiver. No one else ca
3 min read
Conventional Encryption
Conventional encryption is a cryptographic system that uses the same key used by the sender to encrypt the message and by the receiver to decrypt the message. It was the only type of encryption in use prior to the development of public-key encryption. It is still much preferred of the two types of encryption systems due to its simplicity. It is a r
3 min read
Article Tags :