mirror of
https://github.com/zach-sb/SENG2250_Assignment3.git
synced 2025-11-09 02:57:37 +11:00
v0
This commit is contained in:
parent
57bb200e16
commit
4a8846dfe9
39
Q2.java
Normal file
39
Q2.java
Normal file
@ -0,0 +1,39 @@
|
||||
public class Q2 {
|
||||
|
||||
//Setup_Request: Hello
|
||||
|
||||
//SETUP: Server's RSA public key
|
||||
|
||||
//Client_Hello:IDc
|
||||
|
||||
//Server_Hello: IDs , SID
|
||||
|
||||
//Ephemeral DH exchange
|
||||
|
||||
//Finished, check the shared key
|
||||
|
||||
//Data exchange
|
||||
|
||||
/**
|
||||
* Opens the client and server in seperate terminals
|
||||
* @param args
|
||||
*/
|
||||
public static void main(String[] args)
|
||||
{
|
||||
// try
|
||||
// {
|
||||
// Runtime.getRuntime().exec("cmd /c start cmd.exe /K \"java Q2Server\"");
|
||||
// Thread.sleep(500); //Just to ensure the server has enough time to open
|
||||
// Runtime.getRuntime().exec("cmd /c start cmd.exe /K \"java Q2Client\"");
|
||||
// }
|
||||
// catch (Exception e)
|
||||
// {
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
|
||||
Q2Tools toolbox = new Q2Tools();
|
||||
|
||||
toolbox.fastModularExpon(5, 117, 19);
|
||||
}
|
||||
|
||||
}
|
||||
BIN
Q2Client.class
Normal file
BIN
Q2Client.class
Normal file
Binary file not shown.
37
Q2Client.java
Normal file
37
Q2Client.java
Normal file
@ -0,0 +1,37 @@
|
||||
import java.net.Socket;
|
||||
import java.io.*;
|
||||
|
||||
public class Q2Client {
|
||||
|
||||
private Socket s;
|
||||
|
||||
public static void main(String[] args)
|
||||
{
|
||||
System.out.println("Runing Client");
|
||||
Q2Client client = new Q2Client();
|
||||
client.run();
|
||||
}
|
||||
|
||||
public void run()
|
||||
{
|
||||
try
|
||||
{
|
||||
s = new Socket("localhost", 4321);
|
||||
|
||||
DataOutputStream dout=new DataOutputStream(s.getOutputStream());
|
||||
dout.writeUTF("Hello"); //Setup_Request
|
||||
//dout.flush();
|
||||
|
||||
|
||||
|
||||
|
||||
dout.close();
|
||||
|
||||
s.close();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
System.out.println("Client Socket failed to connect.");
|
||||
}
|
||||
}
|
||||
}
|
||||
BIN
Q2Server.class
Normal file
BIN
Q2Server.class
Normal file
Binary file not shown.
39
Q2Server.java
Normal file
39
Q2Server.java
Normal file
@ -0,0 +1,39 @@
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
public class Q2Server {
|
||||
|
||||
private ServerSocket ss;
|
||||
private Socket s;
|
||||
|
||||
public static void main(String[] args)
|
||||
{
|
||||
System.out.println("Runing Server");
|
||||
Q2Server server = new Q2Server();
|
||||
server.run();
|
||||
}
|
||||
public void run()
|
||||
{
|
||||
try
|
||||
{
|
||||
ss = new ServerSocket(4321);
|
||||
s = ss.accept();
|
||||
|
||||
DataInputStream dis=new DataInputStream(s.getInputStream());
|
||||
String str=(String)dis.readUTF();
|
||||
System.out.println("message= "+str);
|
||||
|
||||
ss.close();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
System.out.println("Server Failed");
|
||||
System.out.println(e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
70
Q2Tools.java
Normal file
70
Q2Tools.java
Normal file
@ -0,0 +1,70 @@
|
||||
import java.math.BigInteger;
|
||||
|
||||
public class Q2Tools {
|
||||
|
||||
Q2Tools()
|
||||
{
|
||||
//Nothing to do here
|
||||
}
|
||||
|
||||
public void generateRSA()
|
||||
{
|
||||
|
||||
int e = 65537;
|
||||
|
||||
//Generate p
|
||||
|
||||
//Generate q
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* A^B mod C
|
||||
* @param A
|
||||
* @param B
|
||||
* @param C
|
||||
*/
|
||||
public void fastModularExpon(int A, int B, int C)
|
||||
{
|
||||
String bInBinary = Integer.toBinaryString(B);
|
||||
|
||||
int test = 0;
|
||||
|
||||
System.out.println(bInBinary);
|
||||
|
||||
int k = 0;
|
||||
for(int i = bInBinary.length()-1; i >= 0; i--)
|
||||
{
|
||||
|
||||
if(bInBinary.charAt(i)=='1')
|
||||
{
|
||||
double temp = Math.pow(2, k);
|
||||
System.out.println(2+"^"+k+"="+temp);
|
||||
test += temp;
|
||||
}
|
||||
else
|
||||
{
|
||||
System.out.println(0+"^"+k+"=0");
|
||||
}
|
||||
k++;
|
||||
}
|
||||
|
||||
System.out.println(test);
|
||||
}
|
||||
|
||||
/**
|
||||
* Simply wrapper for Integer.toBinaryString
|
||||
* @param number integer to be converted to binary
|
||||
* @return binary of number as an Biginteger
|
||||
*/
|
||||
public BigInteger convertToBinaryBigInt(int number)
|
||||
{
|
||||
//Take input number, convert to binary string
|
||||
String binaryString = Integer.toBinaryString(number);
|
||||
BigInteger bigBinary = new BigInteger(binaryString);
|
||||
return bigBinary;
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user