import java.math.BigInteger; 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) { Q2 system = new Q2(); system.run(); } public void run() { // 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(); // } Q2RSA rsa = new Q2RSA(); BigInteger[] publicKey = rsa.getPublicKey(); BigInteger privateKey = rsa.getPrivateKey(); BigInteger result1 = encrypt(BigInteger.valueOf(3436), publicKey); BigInteger result2 = decrypt(result1, privateKey, publicKey); } public BigInteger encrypt(BigInteger X, BigInteger[] publicKey) { return fme(X ,publicKey[1],publicKey[0]); } public BigInteger decrypt(BigInteger Y, BigInteger privateKey, BigInteger[] publicKey) { return fme(Y, privateKey, publicKey[0]); } /** * Performs a fast modular exponation, (base^exponent)%modulus. * Takes input values as strings containg the numbers * @param baseVal * @param exponentVal * @param modulusVal * @return */ public BigInteger fme(BigInteger baseVal, BigInteger exponentVal, BigInteger modulusVal) { if(modulusVal.equals(BigInteger.valueOf(1))) { return BigInteger.valueOf(0); } BigInteger result = new BigInteger("1"); while(exponentVal.compareTo(BigInteger.valueOf(0))==1) { if(exponentVal.and(BigInteger.valueOf(1)).compareTo(BigInteger.valueOf(1))==0) { result = result.multiply(baseVal); result = result.mod(modulusVal); } exponentVal = exponentVal.shiftRight(1); baseVal = (baseVal.multiply(baseVal)).mod(modulusVal); } return result; } }