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

Java Io Java Net Bob: Import Import Public Class

The document contains Java code for a simple implementation of a Diffie-Hellman key exchange between two clients, Bob and Alice, along with a hacker attempting to intercept their communication. Bob and Alice each generate their public keys and compute a shared secret key, while the hacker intercepts Bob's public key and sends a fake public key to Alice to perform a man-in-the-middle attack. The code demonstrates the vulnerabilities in the key exchange process when proper security measures are not in place.

Uploaded by

Safeeq Ahamed
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)
9 views5 pages

Java Io Java Net Bob: Import Import Public Class

The document contains Java code for a simple implementation of a Diffie-Hellman key exchange between two clients, Bob and Alice, along with a hacker attempting to intercept their communication. Bob and Alice each generate their public keys and compute a shared secret key, while the hacker intercepts Bob's public key and sends a fake public key to Alice to perform a man-in-the-middle attack. The code demonstrates the vulnerabilities in the key exchange process when proper security measures are not in place.

Uploaded by

Safeeq Ahamed
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/ 5

// Bob.

java - Client Code

import java.io.*;
import java.net.*;

public class Bob {

// Power function to return value of a ^ b mod P


private static long power(long a, long b, long p) {
if (b == 1)
return a;
else
return (((long)Math.pow(a, b)) % p);
}

public static void main(String[] args) throws IOException {


long P = 23;
long G = 9;
long b = 3; // Bob's private key
long y; // Bob's public key
long ka; // Shared secret key from Alice

// Connect to Alice's server


Socket socket = new Socket("localhost", 12345);
System.out.println("Bob (Client) connected to Alice!");

// Get output stream to send data to Alice


DataOutputStream out = new
DataOutputStream(socket.getOutputStream());
// Get input stream to receive data from Alice
DataInputStream in = new
DataInputStream(socket.getInputStream());

// Calculate Bob's public key and send it to Alice


y = power(G, b, P);
out.writeLong(y); // Sending Bob's public key to Alice

// Receive Alice's public key


long x = in.readLong();

// Compute shared secret key for Bob


long kb = power(x, b, P);
System.out.println("Secret key for Bob: " + kb);

// Close the connection


socket.close();
}
}
// Alice.java - Server Code

import java.io.*;
import java.net.*;

public class Alice {

// Power function to return value of a ^ b mod P


private static long power(long a, long b, long p) {
if (b == 1)
return a;
else
return (((long)Math.pow(a, b)) % p);
}

public static void main(String[] args) throws IOException {


long P = 23;
long G = 9;
long a = 4; // Alice's private key
long x; // Alice's public key

// Alice creates a socket server to listen for incoming


connections from Bob
ServerSocket serverSocket = new ServerSocket(12345);
System.out.println("Alice (Server) is waiting for
connection...");

// Wait for a connection from Bob


Socket socket = serverSocket.accept();
System.out.println("Alice (Server) connected with Bob!");

// Get output stream to send data to Bob


DataOutputStream out = new
DataOutputStream(socket.getOutputStream());
// Get input stream to receive data from Bob
DataInputStream in = new
DataInputStream(socket.getInputStream());

// Calculate Alice's public key and send it to Bob


x = power(G, a, P);
out.writeLong(x); // Sending Alice's public key to Bob

// Receive Bob's public key


long y = in.readLong();
// Compute shared secret key for Alice
long ka = power(y, a, P);
System.out.println("Secret key for Alice: " + ka);

// Close the connection


socket.close();
serverSocket.close();
}
}

// Hacker.java - Hacker Code (To intercept messages)

import java.io.*;
import java.net.*;

public class hacker {

// Power function to return value of a ^ b mod P


private static long power(long a, long b, long p) {
if (b == 1)
return a;
else
return (((long)Math.pow(a, b)) % p);
}

public static void main(String[] args) throws IOException {


long P = 23;
long G = 9;

// Simulate the hacker intercepting the communication


System.out.println("Hacker is intercepting communication
between Alice and Bob...");

// Connect to Alice's server and listen on the same port


Socket socket = new Socket("localhost", 12345);
System.out.println("Hacker connected to Alice!");

// Get output stream to send data to Alice


DataOutputStream out = new
DataOutputStream(socket.getOutputStream());
// Get input stream to receive data from Alice
DataInputStream in = new
DataInputStream(socket.getInputStream());
// Intercept Bob's public key (simulate receiving Bob's public
key)
long interceptedBobsPublicKey = in.readLong();
System.out.println("Hacker intercepted Bob's public key: " +
interceptedBobsPublicKey);

// Send an arbitrary key to Alice (simulating a man-in-the-


middle attack)
long fakeHackerPublicKey = power(G, 2, P); // Random private
key of the hacker
out.writeLong(fakeHackerPublicKey); // Sending fake public key
to Alice

// Simulate hacking the key by generating a secret key using


the fake public key
long hackedKey = power(interceptedBobsPublicKey, 2, P); //
Calculate the shared secret using the fake key
System.out.println("Hacker's calculated secret key: " +
hackedKey);

// Close the connection


socket.close();
}
}

You might also like