Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

JAVA modify program I have a multi client / server chat program that uses Diffie Hellman key exchange to encrypt messages. Entire Program provided, can

JAVA modify program

I have a multi client / server chat program that uses Diffie Hellman key exchange to encrypt messages. Entire Program provided, can you please make changes to have it run on the command line where and port number, P and Q, are entered in the servers command line. thank you. JAVA

DiffieServer.Java

package chat.server;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import chat.net.Connection;
public class DiffieServer {
private ServerSocket ssock;
private ArrayList clients;
public static void main(String args[]) {
DiffieServer server = new DiffieServer(5050);
server.start();
}
public DiffieServer(int port) {
try {
ssock = new ServerSocket(port);
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
}
public void start() {
System.out.println("Server started...");
clients = new ArrayList();
BroadcastThread bct = new BroadcastThread();
bct.start();
try {
while (true) {
Socket csock = ssock.accept();
Connection newClient = new Connection(csock);
newClient.sendKey();
clients.add(newClient);
System.out.println("** New Client: " + csock.toString());
}
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
}
private class BroadcastThread extends Thread {
public void run() {
while (true) {
try {
for (int i = 0; i < clients.size(); i++) {
if (clients.get(i).hasMessage()) {
String message = clients.get(i).getMessage();
broadcastMessage(message);
System.out.println(message);
}
}
Thread.sleep(500);
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
}
private void broadcastMessage(String message) {
for (int i = 0; i < clients.size(); i++) {
clients.get(i).sendMessage(message);
}
}
}

}

DiffieClient.java

package chat.client;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import chat.net.Connection;
public class DiffieClient {
private Connection server;
public static void main(String args[]) {
DiffieClient client = new DiffieClient("192.168.1.139", 5051);
client.start();
}
public DiffieClient(String hostname, int port) {
try {
server = new Connection(new Socket(hostname, port));
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
}
public void start() {
server.getKey();
BroadcastListener bcl = new BroadcastListener();
bcl.start();
BufferedReader sysin = new BufferedReader(new InputStreamReader(
System.in));
while (true) {
try {
server.sendMessage(sysin.readLine());
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
}
}
private class BroadcastListener extends Thread {
public void run() {
try {
while (true) {
if (server.hasMessage()) {
System.out.println(server.getMessage());
}
Thread.sleep(500);
}
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
}

}

Connection.java

package chat.net;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.math.BigInteger;
import java.net.Socket;
import java.util.Random;
import java.util.Scanner;
public class Connection {
private Socket socket;
private BufferedReader br;
private PrintStream ps;
private BigInteger cipherKey;
public Connection(Socket sck) {
try {
socket = sck;
br = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
ps = new PrintStream(socket.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
}
public void sendKey() {
try {
// send P and Q to client
BigInteger p = new BigInteger("5");
BigInteger q = new BigInteger("23");
ps.println(p.toString() + " " + q.toString() + " ");
/*System.out.println("--> SEND: P=" + p.toString() + " Q="
+ q.toString());*/
// receive public key from client
BigInteger yourKey = new BigInteger(Integer.parseInt(br.readLine())
+ "");
//System.out.println("<-- RECEIVE: yourKey=" + yourKey.toString());
// send public key to client
Random rand = new Random();
BigInteger privateKey = new BigInteger((rand.nextInt(100) + 1) + "");
//System.out.println("(privateKey=" + privateKey.toString() + ")");
BigInteger publicKey = p.modPow(privateKey, q);
ps.println(publicKey.toString() + " ");
//System.out.println("--> SEND: publicKey=" + publicKey.toString());
// calculate cipher key
cipherKey = yourKey.modPow(privateKey, q);
//System.out.println("(cipherKey=" + cipherKey.toString() + ")");
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
}
public void getKey() {
try {
// receive P and Q from server
Scanner ioscan = new Scanner(br.readLine());
BigInteger p = new BigInteger(ioscan.nextInt() + "");
BigInteger q = new BigInteger(ioscan.nextInt() + "");
/*System.out.println("<-- RECEIVE: P=" + p.toString() + " Q="
+ q.toString());*/
// send public key to server
Random rand = new Random();
BigInteger privateKey = new BigInteger((rand.nextInt(100) + 1) + "");
//System.out.println("(privateKey=" + privateKey.toString() + ")");
BigInteger publicKey = p.modPow(privateKey, q);
ps.println(publicKey.toString() + " ");
//System.out.println("--> SEND: publicKey=" + publicKey.toString());
// receive public key from server
BigInteger yourKey = new BigInteger(Integer.parseInt(br.readLine())
+ "");
//System.out.println("<-- RECEIVE: yourKey=" + yourKey.toString());
// calculate cipher key
cipherKey = yourKey.modPow(privateKey, q);
//System.out.println("(cipherKey=" + cipherKey.toString() + ")");
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
}
public boolean hasMessage() throws IOException {
return br.ready();
}
public String getMessage() throws IOException {
return decode(br.readLine());
}
public void sendMessage(String message) {
ps.println(encode(message) + " ");
}
private String encode(String message) {
message = message.toUpperCase();
String encodedMessage = "";
for (int i = 0; i < message.length(); i++) {
char thisChar = message.charAt(i);
if (thisChar >= 65 && thisChar <= 90) {
int shiftValue = 1 + Integer.parseInt(cipherKey.mod(
new BigInteger("26")).toString());
//System.out.println("shiftValue = " + shiftValue);
//System.out.println("thisChar (before) = " + thisChar);
thisChar += shiftValue;
if (thisChar > 90)
thisChar -= 26;
//System.out.println("thisChar (after) = " + thisChar);
}
//System.out.println("thisChar (appending) = " + thisChar);
encodedMessage += thisChar;
}
return encodedMessage;
}
private String decode(String message) {
message = message.toUpperCase();
String decodedMessage = "";
for (int i = 0; i < message.length(); i++) {
char thisChar = message.charAt(i);
if (thisChar >= 65 && thisChar <= 90) {
int shiftValue = 1 + Integer.parseInt(cipherKey.mod(
new BigInteger("26")).toString());
thisChar -= shiftValue;
if (thisChar < 65)
thisChar += 26;
}
decodedMessage += thisChar;
}
return decodedMessage;
}
public void close() {
try {
ps.close();
br.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
}
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Microsoft Outlook 2023

Authors: James Holler

1st Edition

B0BP9P1VWJ, 979-8367217322

More Books

Students also viewed these Databases questions

Question

A mental grouping of similar things is called a .

Answered: 1 week ago