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

Ex5 22bce1851

The document describes a lab exercise on implementing a Man in the Middle attack during the Diffie-Hellman key exchange protocol. It includes Java code for three components: an Attacker that intercepts and manipulates keys between Alice and Bob, and the clients Alice and Bob that generate and send their keys. The output sections indicate the results of the operations performed by each component during the attack simulation.
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)
28 views6 pages

Ex5 22bce1851

The document describes a lab exercise on implementing a Man in the Middle attack during the Diffie-Hellman key exchange protocol. It includes Java code for three components: an Attacker that intercepts and manipulates keys between Alice and Bob, and the clients Alice and Bob that generate and send their keys. The output sections indicate the results of the operations performed by each component during the attack simulation.
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

Course Title: Cryptography and Course Code: BCSE309P

Network Security Lab


Faculty: Prof. Karthika V Slot: L31-L32
Regno: 22BCE1851 Name: Yerra Chaitanya

Ex5: Man in the Middle Attack in Diffie Hellman Key Exchange


Code:
1)Attacker (Server):

import java.io.*;
import java.net.*;
import java.math.BigInteger;
import java.security.SecureRandom;
public class Attacker {
public static void main(String[] args) throws Exception {
ServerSocket server = new ServerSocket(5000);
System.out.println("Attacker is waiting for connections...");
Socket aliceSocket = server.accept();
System.out.println("[Attacker] Alice connected.");
Socket bobSocket = server.accept();
System.out.println("[Attacker] Bob connected.");
BufferedReader aliceIn = new BufferedReader(new
InputStreamReader(aliceSocket.getInputStream()));
PrintWriter aliceOut = new PrintWriter(aliceSocket.getOutputStream(), true);
BufferedReader bobIn = new BufferedReader(new
InputStreamReader(bobSocket.getInputStream()));
PrintWriter bobOut = new PrintWriter(bobSocket.getOutputStream(), true);
System.out.println("Attacker");
System.out.print("Enter prime number: ");
BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in));
BigInteger p = new BigInteger(userInput.readLine());
System.out.print("Enter primitive root: ");
BigInteger g = new BigInteger(userInput.readLine());
bobOut.println(p);
bobOut.println(g);
SecureRandom rand = new SecureRandom();
BigInteger Xd1 = new BigInteger(p.bitLength() - 1, rand);
BigInteger Xd2 = new BigInteger(p.bitLength() - 1, rand);
System.out.println("Chosen private keys: Xd1 = " + Xd1 + ", Xd2 = " + Xd2);
BigInteger Ya = new BigInteger(aliceIn.readLine());
System.out.println("Intercepted Alice's public key: " + Ya);
BigInteger Yd1 = g.modPow(Xd1, p);
bobOut.println(Yd1);
System.out.println("Sent manipulated key to Bob: " + Yd1);
BigInteger Yb = new BigInteger(bobIn.readLine());
System.out.println("Intercepted Bob's public key: " + Yb);
BigInteger Yd2 = g.modPow(Xd2, p);
aliceOut.println(Yd2);
System.out.println("Sent manipulated key to Alice: " + Yd2);

BigInteger Ka = Ya.modPow(Xd1, p);


BigInteger Kb = Yb.modPow(Xd2, p);
System.out.println("Computed common key with Alice: " + Ka);
System.out.println("Computed common key with Bob: " + Kb);
aliceSocket.close();
bobSocket.close();
server.close();
}

}
2)Alice (Client):
import java.io.*;
import java.net.*;
import java.math.BigInteger;
import java.security.SecureRandom;
public class Alice {
public static void main(String[] args) throws Exception {
Socket socket = new Socket("127.0.0.1", 5000);
BufferedReader in = new BufferedReader(new
InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Alice");
System.out.print("Enter prime number: ");
BigInteger p = new BigInteger(userInput.readLine());
System.out.print("Enter primitive root: ");
BigInteger g = new BigInteger(userInput.readLine());
out.println(p);
out.println(g);
SecureRandom rand = new SecureRandom();
BigInteger Xa = new BigInteger(p.bitLength() - 1, rand);
System.out.println("Chosen private key (Xa): " + Xa);
BigInteger Ya = g.modPow(Xa, p);
System.out.println("Computed public key (Ya): " + Ya);
out.println(Ya); // Send to attacker
BigInteger Yd2 = new BigInteger(in.readLine()); // Receive manipulated key from
attacker
System.out.println("Received public key from Attacker: " + Yd2);
BigInteger Ka = Yd2.modPow(Xa, p);
System.out.println("Computed common key with Attacker: " + Ka);
socket.close() }}
3)BOB(Client):

import java.io.*;
import java.net.*;
import java.math.BigInteger;
import java.security.SecureRandom;
public class Bob {
public static void main(String[] args) throws Exception {
Socket socket = new Socket("127.0.0.1", 5000);
BufferedReader in = new BufferedReader(new
InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Bob");
BigInteger p = new BigInteger(in.readLine());
BigInteger g = new BigInteger(in.readLine());
SecureRandom rand = new SecureRandom();
BigInteger Xb = new BigInteger(p.bitLength() - 1, rand);
System.out.println("Chosen private key (Xb): " + Xb);
BigInteger Yb = g.modPow(Xb, p);
System.out.println("Computed public key (Yb): " + Yb);
BigInteger Yd1 = new BigInteger(in.readLine()); // Receive manipulated key from
attacker
System.out.println("Received public key from Attacker: " + Yd1);
BigInteger Kb = Yd1.modPow(Xb, p);
System.out.println("Computed common key with Attacker: " + Kb);
out.println(Yb); // Send Bob’s public key
socket.close();
}
}
Output:

Alice Output:

Bob Output:
Attacker Output:

You might also like