0% found this document useful (0 votes)
22 views

Crypto - Lab - 8.ipynb - Colab

practical for information network security

Uploaded by

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

Crypto - Lab - 8.ipynb - Colab

practical for information network security

Uploaded by

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

11/9/24, 9:17 PM 232010012_crypto_lab_8.

ipynb - Colab

Name : Tejas Ingole

Roll NO : 232010012

Lab 8

aim : Implement Elgamal cryptosystem

# 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])

i t(" ^k d " )
https://colab.research.google.com/drive/1M2v0CBfp1Cd2MaXZhe6IzsO-yR77_i5E#scrollTo=DE… 1/3
11/9/24, 9:17 PM 232010012_crypto_lab_8.ipynb - Colab
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 a message to encrypt: ")
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()

Enter a message to encrypt: hello world


Original Message : hello world
g used : 19825011186334759312128595491439763765706996732650
g^a used : 57289196782681490151187960815620952936796158421600
g^k used : 20091654963257302181645661181698446654392336252265
g^ak used : 58661614095335207166737123072865984003409105458211
Decrypted Message : hello world

https://colab.research.google.com/drive/1M2v0CBfp1Cd2MaXZhe6IzsO-yR77_i5E#scrollTo=DE… 2/3

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy