Diffie-Hellman key exchange algorithm over Java TCP socket

Implementation in Java of the Diffie-Hellman algorithm key exchange. The code if part of a Computer Security project of Karlstad University.

Client code:

/*
* Class client.java
* @author Guillermo Moraleda
*
* Diffie-Hellman key exchange algorithm
* over a TCP Socket.
*
*/
package kau.cs.lab.connection;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.Socket;
import java.math.BigInteger;

class client {

private final static int p = 23;
private final static int g = 5;
private static int A;
private static int B;
private static int a;
private static BigInteger powresult;
private static BigInteger big_a;
private static BigInteger big_p;
private static int key;

public static void main(String argv[]) throws Exception {

//Diffie-CHellman Algorithm

a = (int) Math.round(Math.random() * 100);
while (a >= p) {
a = a - p;
}
A = (int) (Math.pow(g, a) % p);
System.out.println("p = " + p + 
"\ng = " + g + "\na = " + a + "\nA = " + A);
BufferedReader inFromUser = new 
BufferedReader(new InputStreamReader(System.in));
Socket clientSocket = new Socket("localhost", 6789);
DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

//Send A=g^a mod p
outToServer.writeBytes(String.valueOf(A) + '\n');


//Receive B=g^b mod p
B = Integer.parseInt(inFromServer.readLine());
System.out.println("B = " + B);

//Create Key
powresult = new BigInteger(Integer.toString(B));
big_a = new BigInteger(Integer.toString(a));
big_p = new BigInteger(Integer.toString(p));
key = (powresult.modPow(big_a, big_p)).intValue();
System.out.println("Key = " + key);

clientSocket.close();
}
}
Server code:
/*
* Class server.java
* @author Guillermo Moraleda
*
* Diffie-Hellman key exchange algorithm
* over a TCP Socket.
*
*/
package kau.cs.lab.connection;

import java.io.*;
import java.net.*;
import java.math.BigInteger;

class server {

private final static int p = 23;
private final static int g = 5;
private static int A;
private static int B;
private static int b;
private static BigInteger powresult;
private static BigInteger big_b;
private static BigInteger big_p;
private static int key;

public static void main(String argv[]) throws Exception {

//Diffie-CHellman Algorithm

b = (int) Math.round(Math.random() * 100);
while (b >= p) {
b = b - p;
}
B = (int) (Math.pow(g, b) % p);
System.out.println("p = " + p + 
"\ng = " + g + "\nb = " + b + "\nB = " + B);

ServerSocket welcomeSocket = new ServerSocket(6789);
Socket connectionSocket = welcomeSocket.accept();
BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());

//Send B = g^b mod p
outToClient.writeBytes(String.valueOf(B) + '\n');


//Receive A = g^a mod p
A = Integer.parseInt(inFromClient.readLine());

System.out.println("A = " + A);

//Create Key
powresult = new BigInteger(Integer.toString(A));
big_b = new BigInteger(Integer.toString(b));
big_p = new BigInteger(Integer.toString(p));
key = (powresult.modPow(big_b, big_p)).intValue();
System.out.println("Key = " + key);

connectionSocket.close();
welcomeSocket.close();
}
}

0 comentarios :: Diffie-Hellman key exchange algorithm over Java TCP socket

Publicar un comentario