Program to implement Deffi-Hellman Algorithm in Client-Server Fashion.
Prerequisite: Server Socket Programming, Diffie-Hellman algorithm
The Diffie Hellman Algorithm is being used to establish a shared secret that can be used for secret communications while exchanging data over a public network.
In the below program, the client will share the value of
,
, and public key
. Whereas, the server will accept the values and calculate its public key
and send it to the client.
Both Client and Server will calculate the secret key for symmetric encryption by using the public key.
Program 1: Server Program
import java.net.*; import java.io.*; public class GreetingServer { public static void main(String[] args) throws IOException { try { int port = 8088; // Server Key int b = 3; // Client p, g, and key double clientP, clientG, clientA, B, Bdash; String Bstr; // Established the Connection ServerSocket serverSocket = new ServerSocket(port); System.out.println("Waiting for client on port " + serverSocket.getLocalPort() + "..."); Socket server = serverSocket.accept(); System.out.println("Just connected to " + server.getRemoteSocketAddress()); // Server's Private Key System.out.println("From Server : Private Key = " + b); // Accepts the data from client DataInputStream in = new DataInputStream(server.getInputStream()); clientP = Integer.parseInt(in.readUTF()); // to accept p System.out.println("From Client : P = " + clientP); clientG = Integer.parseInt(in.readUTF()); // to accept g System.out.println("From Client : G = " + clientG); clientA = Double.parseDouble(in.readUTF()); // to accept A System.out.println("From Client : Public Key = " + clientA); B = ((Math.pow(clientG, b)) % clientP); // calculation of B Bstr = Double.toString(B); // Sends data to client // Value of B OutputStream outToclient = server.getOutputStream(); DataOutputStream out = new DataOutputStream(outToclient); out.writeUTF(Bstr); // Sending B Bdash = ((Math.pow(clientA, b)) % clientP); // calculation of Bdash System.out.println("Secret Key to perform Symmetric Encryption = " + Bdash); server.close(); } catch (SocketTimeoutException s) { System.out.println("Socket timed out!"); } catch (IOException e) { } } } |
Program 2: Client Program
import java.net.*; import java.io.*; public class GreetingClient { public static void main(String[] args) { try { String pstr, gstr, Astr; String serverName = "localhost"; int port = 8088; // Declare p, g, and Key of client int p = 23; int g = 9; int a = 4; double Adash, serverB; // Established the connection System.out.println("Connecting to " + serverName + " on port " + port); Socket client = new Socket(serverName, port); System.out.println("Just connected to " + client.getRemoteSocketAddress()); // Sends the data to client OutputStream outToServer = client.getOutputStream(); DataOutputStream out = new DataOutputStream(outToServer); pstr = Integer.toString(p); out.writeUTF(pstr); // Sending p gstr = Integer.toString(g); out.writeUTF(gstr); // Sending g double A = ((Math.pow(g, a)) % p); // calculation of A Astr = Double.toString(A); out.writeUTF(Astr); // Sending A // Client's Private Key System.out.println("From Client : Private Key = " + a); // Accepts the data DataInputStream in = new DataInputStream(client.getInputStream()); serverB = Double.parseDouble(in.readUTF()); System.out.println("From Server : Public Key = " + serverB); Adash = ((Math.pow(serverB, a)) % p); // calculation of Adash System.out.println("Secret Key to perform Symmetric Encryption = " + Adash); client.close(); } catch (Exception e) { e.printStackTrace(); } } } |
Use javac to Compile the programs, and open two console/terminal to run the system
Output:
In the first console run the server program, it will wait for the client’s connection. As soon as client is connected results will popup

In the second console, run the client’s program

Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.
Recommended Posts:
- TCP Server-Client implementation in C
- UDP Server-Client implementation in C
- UDP Client Server using connect | C implementation
- Establishing the two-way Communication between Server and Client in Java
- Difference between Client-Server and Peer-to-Peer Network
- Two way communication between Client and Server using Win32 Threads
- Difference between Server OS and Client OS
- Difference between Client /Server and Distributed DBMS
- Difference between Client side filter and Server side filters in Cyber Security
- Creating an Server-Client Application using the DatagramPacket and DatagramSocket classes
- TCP Client-Server Program to Check if a Given String is Palindrome
- Distributed Objects Computing: The next generation of client-server computing
- Multi-threaded Chat Application in Java | Set 2 (Client Side Programming)
- HTTP/2 Client feature of Java 9 with Example
- Creating a Socket to Display Message to a Single Client in Java
- Explicitly assigning port number to client in Socket
- getParameter() - Passing data from client to JSP
- Implementation of Diffie-Hellman Algorithm
- Johnson’s algorithm for All-pairs shortest paths | Implementation
- Box Blur Algorithm - With Python implementation
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

