0% found this document useful (0 votes)
9 views3 pages

Computer Network

This document discusses two key exchange algorithms - Diffie-Hellman key exchange and how it is vulnerable to man-in-the-middle attacks. It includes Python code examples to demonstrate how Diffie-Hellman key exchange works and how a man-in-the-middle attack can be performed by an attacker to secretly intercept the key exchange between two parties.
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)
9 views3 pages

Computer Network

This document discusses two key exchange algorithms - Diffie-Hellman key exchange and how it is vulnerable to man-in-the-middle attacks. It includes Python code examples to demonstrate how Diffie-Hellman key exchange works and how a man-in-the-middle attack can be performed by an attacker to secretly intercept the key exchange between two parties.
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/ 3

NAME: Aayush

REGN NO: 21BCE1658


CNS LAB 7

Code:

Diffie-Hellman:

from math import *


import random

p = int(input("Enter a prime number P: "))


g = int(input("Enter a primitive root of P called g: "))
a = random.randint(1, p)
b = random.randint(1, p)

def prime_checker(n):
for i in range(2, n):
if(n % i == 0):
return True
return False

def primitive_checker(p, g):


present = dict()
for i in range(1, p):
number = pow(g, i) % p
if(number in present.keys()):
return False

present[number] = True
return False

if(prime_checker(p) or primitive_checker(p, g)):


print("Either p or g is incorrect")
exit(0)

x = pow(g, a) % p
y = pow(g, b) % p

alice_secret_key = pow(y, a) % p
bob_secret_key = pow(x, b) % p

print(alice_secret_key)
print(bob_secret_key)

Output:
MITM:

import random

p = int(input('Enter a prime number : '))


g = int(input('Enter a number : '))

class A:
def __init__(self):
# Generating a random private number selected by alice
self.n = random.randint(1, p)

def publish(self):
# generating public values
return (g**self.n)%p

def compute_secret(self, gb):


# computing secret key
return (gb**self.n)%p

class B:
def __init__(self):
self.a = random.randint(1, p)
self.b = random.randint(1, p)
self.arr = [self.a,self.b]

def publish(self, i):


return (g**self.arr[i])%p

def compute_secret(self, ga, i):


return (ga**self.arr[i])%p

alice = A()
bob = A()
eve = B()

# Printing out the private selected number by Alice and Bob


print(f'Alice selected (a) : {alice.n}')
print(f'Bob selected (b) : {bob.n}')
print(f'Eve selected private number for Alice (c) : {eve.a}')
print(f'Eve selected private number for Bob (d) : {eve.b}')

# Generating public values


ga = alice.publish()
gb = bob.publish()
gea = eve.publish(0)
geb = eve.publish(1)
print(f'Alice published (ga): {ga}')
print(f'Bob published (gb): {gb}')
print(f'Eve published value for Alice (gc): {gea}')
print(f'Eve published value for Bob (gd): {geb}')

# Computing the secret key


sa = alice.compute_secret(gea)
sea = eve.compute_secret(ga,0)
sb = bob.compute_secret(geb)
seb = eve.compute_secret(gb,1)
print(f'Alice computed (S1) : {sa}')
print(f'Eve computed key for Alice (S1) : {sea}')
print(f'Bob computed (S2) : {sb}')
print(f'Eve computed key for Bob (S2) : {seb}')

Output:

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