Question

For client and server communication By using Diffe and Helman algorithm(DH) generate a secret session key(k)...

For client and server communication By using Diffe and Helman algorithm(DH) generate a secret session key(k) from both sides (from client and server separately) by using Java .and also by accepting some message from client by using the generated secret session key (K) encrypt the message from client-side and send to server and server uses the generated secret session key(k) of his own and decrypt the received encrypted message in java. thanks in advance.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

code

solution

//output

//copyable code

//serverside.java

import java.net.*;

import java.io.*;

public class serverside {

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

{

try {

int portnumber = 8088;

// set Key

int b1 = 3;

// declare the variable

double cl_P, cl_G, cl_A, B, B_DASH;

String B_string;

// Connection

ServerSocket server = new ServerSocket(portnumber);

System.out.println("wait client portnumber " + server.getLocalPort() + "...");

Socket s1 = server.accept();

System.out.println("connected to " + s1.getRemoteSocketAddress());

// Private Key

System.out.println("Server side: Private Key = " + b1);

// Accepts data

DataInputStream in1 = new DataInputStream(s1.getInputStream());

cl_P = Integer.parseInt(in1.readUTF());

System.out.println("Client side: P = " + cl_P);

cl_G = Integer.parseInt(in1.readUTF());

System.out.println("Client side: G = " + cl_G);

cl_A = Double.parseDouble(in1.readUTF());

System.out.println("Client side : Public Key = " + cl_A);

B = ((Math.pow(cl_G, b1)) % cl_P);

B_string = Double.toString(B);

OutputStream os = s1.getOutputStream();

DataOutputStream out1 = new DataOutputStream(os);

out1.writeUTF(B_string);

B_DASH = ((Math.pow(cl_A, b1)) % cl_P);

System.out.println("Symmetric Encryption secret key= "

+ B_DASH);

s1.close();

}

catch (SocketTimeoutException s) {

System.out.println("timed out!");

}

catch (IOException e) {

}

}

}

//clientside.java

import java.net.*;

import java.io.*;

public class clientside {

public static void main(String[] args)

{

try {

String p_string, g_string, A_string;

String Name = "localhost";

int portnumber = 8088;

// Declare variable

int p1 = 23;

int g1 = 9;

int a1 = 4;

double A_dash, server_B;

// connection

System.out.println("Connecting to " + Name

+ " on portnumber " + portnumber);

Socket cl = new Socket(Name, portnumber);

System.out.println("connected to "

+ cl.getRemoteSocketAddress());

// Sends DATA

OutputStream OS = cl.getOutputStream();

DataOutputStream out = new DataOutputStream(OS);

p_string = Integer.toString(p1);

out.writeUTF(p_string);

g_string = Integer.toString(g1);

out.writeUTF(g_string);

double A1 = ((Math.pow(g1, a1)) % p1);

A_string = Double.toString(A1);

out.writeUTF(A_string);

// KEY

System.out.println("Client Side: Private Key = " + a1);

// Accepts the data

DataInputStream in1 = new DataInputStream(cl.getInputStream());

server_B = Double.parseDouble(in1.readUTF());

System.out.println("Server side : Public Key = " + server_B);

A_dash = ((Math.pow(server_B, a1)) % p1);

System.out.println("Symmetric Encryption for secret key= "

+ A_dash);

cl.close();

}

catch (Exception e) {

e.printStackTrace();

}

}

}

Add a comment
Know the answer?
Add Answer to:
For client and server communication By using Diffe and Helman algorithm(DH) generate a secret session key(k)...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT