0% found this document useful (0 votes)
16 views6 pages

NSC Lab 13

Uploaded by

Hussam Ather
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views6 pages

NSC Lab 13

Uploaded by

Hussam Ather
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Name: ALI SHER BAZ Reg.

No: 20-CP-93

Network Security and Cryptography Lab


Lab Report 13
Date: 5-04-2024

Submitted to: Sir Adnan Mustafa


Name: ALI SHER BAZ Reg. No: 20-CP-93

ElGamal Encryption
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 logarithms in a cyclic group that is even if we know ga and gk, it
is extremely difficult to compute gak.
The ElGamal Algorithm provides an alternative to the RSA for public key encryption.
1. Security of the RSA depends on the (presumed) difficulty of factoring large integers.
2. The security of the ElGamal algorithm depends on the (presumed) difficulty of computing
discrete logs in a large prime modulus.
ElGamal has the disadvantage that the ciphertext is twice as long as the plaintext. It has the
advantage that the same plaintext gives a different ciphertext (with near certainty) each time it is
encrypted.
Traditional encryption relies on a single secret key shared between communicating parties. This
symmetric key approach poses challenges in key distribution and management.
Public-key cryptography offers a more efficient solution by employing a key pair:
• Public Key: Widely disseminated and used for encryption.
• Private Key: Kept secret and used for decryption.
ElGamal encryption leverages this public-key paradigm, enabling secure communication even
when the recipient's private key remains unknown.
Working:
1. Key Generation:
 Choose a large prime number 𝑝p.
 Choose a generator 𝑔g for the multiplicative group modulo 𝑝p. The generator 𝑔g is an
integer such that all numbers from 11 to 𝑝−1p−1 can be generated by repeatedly applying
the group operation (multiplication modulo 𝑝p) to 𝑔g.
 Choose a private key 𝑥x randomly from the range 11 to 𝑝−1p−1.
 Calculate the corresponding public key 𝑦y such that 𝑦=𝑔𝑥mod 𝑝y=gxmodp.
2. Encryption:
 To encrypt a message 𝑀M, choose a random integer 𝑘k from the range 11 to 𝑝−1p−1.
 Calculate the ciphertext pair (𝑐1,𝑐2)(c1,c2) where:
 𝑐1=𝑔𝑘mod 𝑝c1=gkmodp
 𝑐2=𝑀⋅𝑦𝑘mod 𝑝c2=M⋅ykmodp
Name: ALI SHER BAZ Reg. No: 20-CP-93

3. Decryption:
 To decrypt the ciphertext pair (𝑐1,𝑐2)(c1,c2), use the private key 𝑥x to calculate the
plaintext 𝑀M as follows:
 𝑀=𝑐2⋅(𝑐1𝑥)−1mod 𝑝M=c2⋅(c1x)−1modp
 (𝑐1𝑥)−1(c1x)−1 denotes the modular inverse of 𝑐1𝑥c1x modulo 𝑝p.

Flow chart of the algorithm:


Name: ALI SHER BAZ Reg. No: 20-CP-93

Code:
# 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


Name: ALI SHER BAZ Reg. No: 20-CP-93

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 = input('Enter message for 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()
Name: ALI SHER BAZ Reg. No: 20-CP-93

Output:

You might also like