I have to modify a server program and chat program to work as the following instructions but I am completely clueless as to where to start. I'd appreciate any help on how to atleast get started. This must be done in java.
Diffie-Hellman
Two parties use a key agreement protocol to generate identical secret keys for encryption without ever having to transmit the secret key. The protocol works by both parties agreeing on a set of values (a) and (q).
Write a client-server (this means TWO programs: a client and
another one the server) program that illustrates how Diffie-Hellman
works.
Use the following to implement this algorithm:
Public information q=353 which is a prime number (the module) Public information a=3 which is a primitive root of q (a < q) UserA selects a secret XA=97 (XA < q) UserA calculates the public component of the key YA=(a to the power of XA) mod q UserA transmits YA to UserB UserB selects a secret XB=233 (XB < q) UserB calculates the public component of the key YB=(a to the power of XB) mod q UserB transmits YB to UserA At this point both users can calculate a common key K. UserA calculates Ka= (YB to the power of XA) mod q UserB calculates Kb= (YA to the power of XB) mod q
Ka is equal to Kb! But why?
What to Submit
After you test your programs with the obove parameters, you must modify your programs to use:
Please note that both entities (in this case the client and the server) know ahead of time the (q) and the (a) numbers!
How do you generate an N-bit prime number? Look at the following program:
/************************************************************************* * Compilation: javac RandomPrime.java * Execution: java RandomPrime N * * Generate a N-bit integer that is (probably) prime. * *************************************************************************/ import java.math.BigInteger; import java.util.Random; import java.security.SecureRandom; public class RandomPrime { public static void main(String[] args) { int N = Integer.parseInt(args[0]); SecureRandom random = new SecureRandom(); BigInteger prime = BigInteger.probablePrime(N, random); System.out.println(prime); } }
How do you create (a) and (q)?
(need to import java.math.BigInteger) BigInteger q = new BigInteger("353"); BigInteger a = new BigInteger("3"); System.out.println("a is: " + a + " and q is: " + q);
You should be familiar with all the constructors of BigInteger and the modPow() method. For example your can calculate YA by doing:
YA = a.modPow(XA, q);
Complete your programs where you show clearly in your output how
this algorithm works, something like the following:
Public information a=3 and q=353 |
|
SERVER |
CLIENT |
Select XA=?? |
|
Calculate YA=?? |
|
---- Transmit YA to Client ----> |
|
|
Select XB=?? |
|
Calculate YB=?? |
|
<---- Transmit YB to Server ---- |
Calculated Key =?? |
Calculated Key =?? |
Here, the client initiates the connection. Then, the server
accepts the connection and selects XA, computes YA and transmits
the YA to the client. Then the client selects XB, computes YB and
then transmits the YB to the server. Now the server (and the
client) can continue to compute their symmetric secret key.
Of course you can have the client perform the first steps. You can
also transmit to the other side the values of (a) and (q) or simply
assume that everyone knows about these two numbers. This is up to
you and your protocol.
sample code provided:
this is the Client sample provided:
import java.lang.*; import java.io.*; import java.net.*; import java.math.BigInteger; class Client { public static void main(String args[]) { try { // // Instead of "localhost", you can pass the IP address of the server // The port number the server is listening on is: 1234 (see server code). // Socket s = new Socket("localhost", 1234); System.out.println("Local Port: " + s.getLocalPort()); System.out.println("Server Port: " + s.getPort()); ObjectOutputStream oos = new ObjectOutputStream(s.getOutputStream()); ObjectInputStream ois = new ObjectInputStream(s.getInputStream()); /* * (1) RECEIVE a BigInteger */ BigInteger bi = (BigInteger) ois.readObject(); System.out.println("Server sent to me: " + bi); /* * (2) SEND a BigInteger */ oos.writeObject(bi.add(BigInteger.ONE)); // close streams oos.close(); ois.close(); /* * Close connection */ s.close(); } catch(Exception e) { System.err.print("[ERROR] ::" + e); } } }
this is the server program:
import java.lang.*; import java.io.*; import java.net.*; import java.math.BigInteger; public class Server extends Thread { private ServerSocket servSock = null; private void printInfo(Socket s) { InetAddress ia; System.out.println("\tLocal Port : " + s.getLocalPort()); System.out.println("\tRemote Port: " + s.getPort()); ia = s.getInetAddress(); // REMOTE System.out.println("\t==> Remote IP: " +ia.getHostAddress()); System.out.println("\t==> Remote Name: " +ia.getHostName()); System.out.println("\t==> Remote DNS Name: " +ia.getCanonicalHostName()); ia = s.getLocalAddress(); // LOCAL System.out.println("\t==> Local IP: " +ia.getHostAddress()); System.out.println("\t==> Local Name: " +ia.getHostName()); System.out.println("\t==> Local DNS Name: " +ia.getCanonicalHostName()); } public Server(int port) { try { servSock = new ServerSocket(port, 5); System.out.println("Listening on port " + port); } catch(Exception e) { System.err.println("[ERROR] + " + e); } this.start(); } public void run() { while(true) { try { System.out.println("Waiting for connections......"); Socket s = servSock.accept(); System.out.println("Server accepted connection from: " + s.getInetAddress().getHostAddress()); printInfo(s); new ClientHandler(s).start(); } catch(Exception e) { System.err.println("[ERROR] + " + e); } } //servSock.close(); // At some point we need to close this (when we shutdown the server), for now let's put it here } public static void main(String args[]) { new Server(1234); } } /** * Handles connection with the client. */ class ClientHandler extends Thread { private Socket s = null; private ObjectOutputStream oos = null; private ObjectInputStream ois = null; public ClientHandler(Socket s) { this.s = s; try { oos = new ObjectOutputStream(s.getOutputStream()); ois = new ObjectInputStream(s.getInputStream()); } catch (Exception e) { System.err.println("Exception: " + e); } } public void run() { try { /* * (1) SEND a BigInteger, which is Serializable */ oos.writeObject(new BigInteger("123456788")); /* * (2) RECEIVE a BigInteger, which is Serializable */ BigInteger bi = (BigInteger) ois.readObject(); System.out.println("Client sent to me: " + bi); /* * Close stream */ oos.close(); ois.close(); /* * Close connection */ s.close(); } catch(Exception e) { System.err.println("Exception: " + e); } } }
Here is how you start!
Just think how a chat executes.
The system uses message exchange. Reading from the keyboard , sending data and receiving data.
The first step is to ensure whether the keys shared by both of the sender or receiver is same , if they share the same key - they chat can be enabled or exist from the server program.
Diffieheman protocol -
Step 1 : Choose two prime numbers g(primitive root of p) and p.
Step 2 : Alice selects a secret no(a) and computes g^a mod p , let’s call it A. Alice sends A to Bob.
Step 3 : Bob selects a secret no(b) and computes g^b mod p, let’s call it B. Bob sends B to Alice.
Step 4 : Alice computes S_A = B^a mod p
Step 5 : Bob computes S_B = A^b mod p
Step 6 : If S_A=S_B then Alice and Bob can agree for future communication.
Hope this helps , let me know if any help required! The server and client program is already mentioned. \you need to compute the various operations in DifffieHelman protocol and send and receive vice versa
I have to modify a server program and chat program to work as the following instructions...
I need help with my IM (instant messaging) java program, I created the Server, Client, and Message class. I somehow can't get both the server and client to message to each other. I am at a roadblock. Here is the question below. Create an IM (instant messaging) java program so that client and server communicate via serialized objects (e.g. of type Message). Each Message object encapsulates the name of the sender and the response typed by the sender. You may...
Error: Main method not found in class ServiceProvider, please define the main method as: public static void main(String[] args) or a JavaFX application class must extend javafx.application.Application This is the error im getting while executing the following code Can you modify the error import java.net.*; import java.io.*; public class ServiceProvider extends Thread { //initialize socket and input stream private Socket socket = null; private ServerSocket server = null; private DataInputStream in = null; private DataOutputStream out = null; private int...
Using java socket programming rewrite the following program to handle multiple clients simultaneously (multi threaded programming) import java.io.*; import java.net.*; public class WelcomeClient { public static void main(String[] args) throws IOException { if (args.length != 2) { System.err.println( "Usage: java EchoClient <host name> <port number>"); System.exit(1); } String hostName = args[0]; int portNumber = Integer.parseInt(args[1]); try ( Socket kkSocket = new Socket(hostName, portNumber); PrintWriter out = new PrintWriter(kkSocket.getOutputStream(), true); BufferedReader in = new BufferedReader( new InputStreamReader(kkSocket.getInputStream())); ) { BufferedReader...
In java write a simple 1-room chat server that is compatible with the given client code. 9 public class Client private static String addr; private static int port; private static String username; 14 private static iter> currenthriter new AtomicReference>(null); public static void main(String[] args) throws Exception [ addr -args[]; port Integer.parseInt (args[1]); username-args[21 Thread keyboardHandler new Thread(Main: handlekeyboardInput); 18 19 while (true) [ try (Socket socket -new Socket (addr, port) println(" CONNECTED!; Printwriter writer new Printwriter(socket.getoutputStreamO); writer.println(username); writer.flush); currenthriter.set(writer); BufferedReader...
The DictionaryClient program featured in the class lecture also used the try-catch when creating a socket. Socket are auto-closeable and thus can use a try-with-resources instead. Edit the dictionary program to use try-with-resources instead. package MySockets; import java.net.*; import java.io.*; public class DictionaryClient { private static final String SERVER = "dict.org"; private static final int PORT = 2628; private static final int TIMEOUT = 15000; public static void main(String[] args) { Socket ...
Q7 The following Client side Java code can send a message to the server side via UDP socket. The client side Java code and the server side Java code are running in two different hosts respectively. The server side host name is “MyFileServer”. The server side receives the message and converts all the letters in the message received to uppercase, then sends the modified message back to the client side. Please read the code carefully, and fill out the blanks...
The following Client side Java code can send a message to the server side via UDP socket. The client side Java code and the server side Java code are running in two different hosts respectively. The server side host name is “MyFileServer”. The server side receives the message and converts all the letters in the message received to uppercase, then sends the modified message back to the client side. Please read the code carefully, and fill out the blanks with...
My Question is: I have to modify this program, even a small modification is fine. Can anyone give any suggestion and solution? Thanks in Advanced. import java.util.*; class arrayQueue { protected int Queue[]; protected int front, rear, size, len; public arrayQueue(int n) { size = n; len = 0; Queue = new int[size]; front = -1; rear = -1; } public boolean isEmpty() { return front == -1; } public boolean isFull() { return front == 0 && rear ==size...
My client java a simple client program java .net import java.io*i public class MyClient (public static void main(String args() thrown IO Exception (part I: initialize rocket and stream BufferedReader inFromUser - new BufferedReader(new inputStreamReader(System in)); Socket clientSocket - new Socket(___. ___); DataOutputStream oldToServer - new DataOutputStream(clientSocket.getOutputStrea, ())); part 2: interact with server