×

ElGamal Algorithm

What is ElGamal Algorithm?

ElGamal Algorithm is an asymmetric key encryption technique for communicating between two parties and encrypting the message. With the help of cryptography, the employee can communicate securely. In encryption, the plain text is converted to encrypted text. The encrypted text is converted back to plain text with the help of a decryption algorithm. The encryption and decryption key need to be secure. There are two types of cryptography. These two are as follows:

  • Symmetric cryptography: There is one shared key used for both encryption and decryption.
  • Asymmetric cryptography: There are two keys (private key and public key) where one of the keys is for encryption and the other for decryption.

For asymmetric cryptosystems, we require public-key encryption to start the communication. One of these public-key encryption schemes is the ElGamal encryption based on the Diffie-Hellman Key exchange protocol for symmetric cryptosystems.

ElGamal encryption is a public key cryptosystem. It uses the method of asymmetric key encryption to encrypt the message. This cryptosystem is based on the finding of discrete logarithm. If we have two groups, ga and gk, it is much more challenging to find the value of gak

The idea of the ElGamal Cryptosystem

Let's take an example to understand this. There are two siblings, Alex and Lucy. Suppose Alex want to communicate with Lucy.

1. First, Lucy has to generate one public and one private key.

  • Lucy has to choose a large number, "q", and a cyclic group Fq.
  • Then she has to choose any element "g" from the cyclic group. 
  • She has to choose another element, "a", such that gcd(a,q)=1.
  • Then she has to compute the value of "h", h=ga.
  • Then Lucy publishes the value of F, h=ga, q and g in her public key and "a" as a private key.

2. Then Alex has to encrypt the data using Lucy's public key.

  • Alex has to select the element "K" from the cyclic group "F" in such a way that gcd(k,q)=1.
  • Then Alex has to compute p = gk and s = hk = gak. Then he has to multiply the value of s and M.
  • Then he has to send (p, M*s) = (gk, M*s).

3. Lucy has to decrypt the message.

  • Lucy has to calculate s' = pa = gak.
  • Lucy has to divide M*s by s' to obtain M as s = s'.

Implementation of ElGamal encryption In Python

Example:

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)




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
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
def encrypt(msg, q, h, g):


	en_msg = []


	k = gen_key(q)
	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
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)
	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()

Output:

What is ElGamal Algorithm

Conclusion

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.


Related Topics

Getting Started with Bitcoin

The goal of this part is to show you how to get started with Bitcoin in a simple way. 1. Get to Know Bitcoin Bitcoin has certain unusual characteristics. It behaves similarly...

4 minutes read.

How can Blockchain Technology help IoT to reach its full potential

The Blockchain technology has huge potential to speed up the IoT's efficient operation. Since then, security has been a big worry with IoT, which has hampered its widespread adoption. IoT...

4 minutes read.

Blockchain Pow

Blockchain Pow is an acronym that stamds for Blockchain sProof of work. Pow (Proof of work) is an original agreement (consensus) algorithm in the Blockchain network. This algorithm is generally...

3 minutes read.

Key Areas of Blockchain

As we know that Blockchain is the backbone of the technology of digital cryptocurrency (Bitcoin). The Blockchain contains all transaction records which are stored in a distributed database; it even...

3 minutes read.

How to choose Bitcoin Wallet

This section will walk you through the process of selecting a Bitcoin wallet. If you wish to participate in Bitcoin, you'll need a wallet. You may receive Bitcoins, transmit Bitcoins,...

3 minutes read.

Feistel Cipher in Cryptography

What is Feistel Cipher? A Feistel Cipher is a structural model that is used to build various symmetric block Ciphers similar to DES. This structural model can be self in veritable....

4 minutes read.

Blockchain Terminologies

Blockchain Terminologies: In this section, we will learn about a few terminologies used in Blockchain technology. Understanding these terms will help you to learn the blockchain technology in an efficient...

3 minutes read.

Uses of Blockchain

Nowadays, there are a lot of applications for Blockchain technology exist. The main use of Blockchains is to serve as a distributed ledger for cryptocurrencies like Bitcoin; by the end...

5 minutes read.

Blockchain Explorer

Blockchain Explorer A blockchain explorer is an online webpage similar to Mozilla and Google Chrome, which allows users to browse and explore information about blockchain technology. All bitcoin and ether users depend on blockchain explorer...

3 minutes read.

Consensus Algorithms in Blockchain

We all know that Blockchain is a decentralized distributed network that provides immutability, privacy, security, and transparency. Considering the absence of a central authority to authenticate & verify transactions, the...

4 minutes read.

Advantages of Cryptography

Cryptography is the practice of secure communication in the presence of third parties. It allows for the secure exchange of information between two or more parties and can be used...

4 minutes read.

Decentralized Voting System using Blockchain

In the era of Industry 4.0, Blockchain is a technology that is fast gaining traction. It is widely utilized in supply chain management systems, healthcare, payments, business, IoT, voting systems,...

3 minutes read.

Kerberos in Cryptography and Network Security

Kerberos is a system that is used for managing user authentication. With the help of Kerberos, the user can access their service without remembering multiple authentication passwords. It is not...

3 minutes read.

Need of Blockchain

Blockchain is an open and decentralized network, which stores redundant data at multiple sites called nodes (computers). Its properties, such as immutability, distributed control, highly secured encrypted blocks, and anonymity, make it ideal...

3 minutes read.

Blockchain Versions

Blockchain Versions Blockchain technology has gained popularity because it offers decentralized and transparent storage to record transactions and data. Generally  Blockchain relates with the bitcoin and cryptocurrency. Since 2009, Blockchain technology has been...

2 minutes read.

Introduction and Features of Cryptography

What is Cryptography? Secret coding has been a part of military activities for many years. In old times people know how to communicate privately. So we can say that private communication...

3 minutes read.

Limitations of Blockchain

Blockchain is a timestamped collection of immutable records that are attached in the form of a never-ending linear chain. Its characteristics, such as decentralized network and anonymity, make it the most trustworthy technology...

3 minutes read.

Demur-rage currencies in Blockchain

One of the key concepts of Blockchain technology that may be extended and re-understood is currency. Currency, often known as a digital token, allows for quantifiable transfer mechanisms. Demur-rage money...

4 minutes read.

Blockchain Bitcoin Cash

Bitcoin Cash is an internet- grounded peer-to- peer electronic cash system. It is fully decentralized, with no central bank, and doesn’t calculate on third parties to serve. Bitcoin Cash is...

4 minutes read.

ElGamal Cryptosystem

The ElGamal cryptosystem was first discovered by Taher ElGamal in the year of 1984. It is based on Discrete Logarithm. When the discrete logarithm cannot find the assumption at the...

6 minutes read.