×

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

Obtaining Free Test Ethers for the Rinkeby Test Network on the Ethereum Blockchain

On the Ethereum Blockchain network, each of us must pay a fee known as gas in order to conduct a transaction or carry out a contract. Therefore, on the Ethereum...

2 minutes read.

Blockchain Cryptocurrency

As we know, cryptocurrencies are digital assets which can also be exchanged between two parties. For securing, verifying the transfer of assets & to have control over the creation of...

3 minutes read.

Sending and Receiving 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.

Blockchain Merkle Tree

The Merkle tree is an essential component of Blockchain technology. It is a mathematical data structure composed of hash of various data blocks that serves as an overview of all...

4 minutes read.

History of Blockchain

History of Blockchain: Satoshi Nakamoto modeled the first Blockchain and explained the details of how a technology was well-found to enhance digital trust, given the immutable and decentralization aspect that...

2 minutes read.

Data Encryption Standards

The Data Encryption Standards was introduced by the National Institute of Standards and Technology. A Feistel Cipher is implemented in DES. The Data Encryption Standards was developed by the company...

3 minutes read.

Cryptography in Blockchain

What is Cryptography? Cryptography is a method for developing codes that prevent third parties from viewing private data. It helps in protecting data and information by using codes, such that it can only be...

4 minutes read.

What does the IT industry mean by BaaS (Blockchain as a Service)?

Technology is developing quite quickly and all businesses are racing to catch up with these technological advancements or face the danger of going out of business in the long term....

5 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.

Smart Contracts

What is a Smart Contract? A smart contract is a self-executing computer program. It is a decentralized computation program, which directly controls the transfer of digital assets between different parties under certain pre-defined...

5 minutes read.

Blockchain DAO

In Blockchain, the full form of DAO is Decentralized Autonomous Organization. As the DAO full form says, the organization is both decentralized & autonomous. Sometimes DAO is also known as...

3 minutes read.

What does Blockchain contain?

A Blockchain is a network of interconnected blocks. Four components make up a block: NonceMerkle tree rootPrevious HashThe timestamp A cryptographic hash of the contents from the preceding block is contained in...

3 minutes read.

Working of Blockchain

How Blockchain Works A Blockchain is simply a collection of records of time-stamped transactions that are managed by a group of servers. There is no central authority who controls the database....

2 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.

How to Block Hashes Work in Blockchain

Working of Block Hashes in Blockchain It is a concept for storing data digitally. It will be available to everyone (publicly). Whoever wants to see it will be able to see...

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.

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.

IDEA: International Data Encryption Algorithm

The International data encryption algorithm (IDEA) is a symmetric key algorithm that is designed to convert text into an unreadable text format. It uses a 128 bits block size and...

3 minutes read.

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....

3 minutes read.

Role of Bitcoin Miners

Let us first know what Bitcoin Mining is and then learn about the role of Bitcoin Miners. What is Bitcoin Mining? Bitcoin Mining is a process where transaction records are added to...

3 minutes read.