|
| 1 | +import java.lang.*; |
| 2 | +import java.io.*; |
| 3 | +import java.net.*; |
| 4 | +import java.util.Scanner; |
| 5 | + |
| 6 | +import javax.crypto.*; |
| 7 | +import javax.crypto.spec.*; |
| 8 | +import java.math.BigInteger; |
| 9 | +import java.security.MessageDigest; |
| 10 | +import java.nio.charset.StandardCharsets; |
| 11 | + |
| 12 | +class Client { |
| 13 | + public static void main(String args[]) { |
| 14 | + try { |
| 15 | + Socket s = new Socket("localhost", 1234); |
| 16 | + System.out.println("Local Port: " + s.getLocalPort()); |
| 17 | + System.out.println("Server Port: " + s.getPort()); |
| 18 | + |
| 19 | + ObjectOutputStream oos = new ObjectOutputStream(s.getOutputStream()); |
| 20 | + ObjectInputStream ois = new ObjectInputStream(s.getInputStream()); |
| 21 | + |
| 22 | + BigInteger q = (BigInteger) ois.readObject(); |
| 23 | + System.out.println("Client: Recived modulus (q). "); |
| 24 | + |
| 25 | + BigInteger a = (BigInteger) ois.readObject(); |
| 26 | + System.out.println("Client: Recived prime root (a). "); |
| 27 | + |
| 28 | + System.out.println("Client: Computing public key (Ya)..."); |
| 29 | + DiffieHellman dh = new DiffieHellman(); |
| 30 | + BigInteger y = dh.computePublicKey(q,a); |
| 31 | + |
| 32 | + oos.writeObject(y); |
| 33 | + System.out.println("Client: Sending my Ya..."); |
| 34 | + |
| 35 | + System.out.println("Client: Waiting for Server's public key (Yb)..."); |
| 36 | + BigInteger yb = (BigInteger)ois.readObject(); |
| 37 | + System.out.println("Client: Recieved Yb."); |
| 38 | + |
| 39 | + BigInteger mkey = dh.computeMasterKey(yb,q); |
| 40 | + |
| 41 | + System.out.println("Client: Creating symmetric Key..."); |
| 42 | + |
| 43 | + String keyHash = DiffieHellman.createHashString(mkey); |
| 44 | + System.out.println("Client: Symmetric Key Created. \n"); |
| 45 | + System.out.println("Hash:" + keyHash); |
| 46 | + |
| 47 | + System.out.println("--------- Secured Communication Initialized -----------"); |
| 48 | + Scanner scanner = new Scanner(System.in); |
| 49 | + //AES aes = new AES(keyHash); |
| 50 | + |
| 51 | + MListener ml = new MListener(ois, oos, keyHash); |
| 52 | + ml.setDaemon(false); |
| 53 | + ml.start(); |
| 54 | + |
| 55 | + while (scanner.hasNext()){ |
| 56 | + String message = scanner.nextLine(); |
| 57 | + INFO info = new INFO("Rixing", message); |
| 58 | + // SealedObject sb = aes.encrypt(info);s |
| 59 | + // oos.writeObject(sb); |
| 60 | + ml.sendMessage(info); |
| 61 | + } |
| 62 | + |
| 63 | + // /* |
| 64 | + // * (1) RECEIVE a BigInteger |
| 65 | + // */ |
| 66 | + // BigInteger bi = (BigInteger) ois.readObject(); |
| 67 | + // System.out.println("Server sent to me: " + bi); |
| 68 | + // |
| 69 | + // /* |
| 70 | + // * (2) SEND a BigInteger |
| 71 | + // */ |
| 72 | + // oos.writeObject(bi.add(BigInteger.ONE)); |
| 73 | + // |
| 74 | + // |
| 75 | + // /* |
| 76 | + // * (3) RECEIVE a custom object, which is Serializable |
| 77 | + // */ |
| 78 | + // INFO info = (INFO) ois.readObject(); |
| 79 | + // System.out.println("INFO object sent by server: " ); |
| 80 | + // info.printInfo(); |
| 81 | + // for(int i=0; i < info.getArray().length; i++) { |
| 82 | + // System.out.println("\t["+ i + "] " + info.getArray()[i]); |
| 83 | + // } |
| 84 | + // |
| 85 | + // |
| 86 | + // /* |
| 87 | + // * (4) RECEIVE a String |
| 88 | + // */ |
| 89 | + // System.out.println("Server says: " + (String) ois.readObject()); |
| 90 | + // |
| 91 | + // |
| 92 | + // |
| 93 | + // System.out.println("--------- ENCRYPTED SEND/RECEIVE -----------"); |
| 94 | + // |
| 95 | + // |
| 96 | + // |
| 97 | + // |
| 98 | + // /* |
| 99 | + // * Create Key |
| 100 | + // */ |
| 101 | + // String password = "12345678"; |
| 102 | + // byte key[] = password.getBytes(); |
| 103 | + // DESKeySpec desKeySpec = new DESKeySpec(key); |
| 104 | + // SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); |
| 105 | + // SecretKey secretKey = keyFactory.generateSecret(desKeySpec); |
| 106 | + // |
| 107 | + // |
| 108 | + // /* |
| 109 | + // * Create Cipher |
| 110 | + // */ |
| 111 | + // Cipher desCipher_encr = Cipher.getInstance("DES/ECB/PKCS5Padding"); |
| 112 | + // Cipher desCipher_decr = Cipher.getInstance("DES/ECB/PKCS5Padding"); |
| 113 | + // |
| 114 | + // |
| 115 | + // /* |
| 116 | + // * Initialize Cipher |
| 117 | + // */ |
| 118 | + // desCipher_encr.init(Cipher.ENCRYPT_MODE, secretKey); |
| 119 | + // desCipher_decr.init(Cipher.DECRYPT_MODE, secretKey); |
| 120 | + // |
| 121 | + // |
| 122 | + // |
| 123 | + // /* |
| 124 | + // * RECEIVE a String (encrypted) |
| 125 | + // */ |
| 126 | + // SealedObject sobj = (SealedObject) ois.readObject(); |
| 127 | + // String ss = (String) sobj.getObject(desCipher_decr); |
| 128 | + // System.out.println("Server sent String: " + ss); |
| 129 | + // |
| 130 | + // System.out.println("--------- Secured Communication Initialized -----------"); |
| 131 | + |
| 132 | + // /* |
| 133 | + // * SEND a custom object (encrypted) |
| 134 | + // */ |
| 135 | + // INFO info_en = new INFO("CLIENT", new BigInteger("999999999")); |
| 136 | + // SealedObject info_sealed_obj = new SealedObject(info_en, desCipher_encr); |
| 137 | + // oos.writeObject(info_sealed_obj); |
| 138 | + // |
| 139 | + // /* |
| 140 | + // * RECEIVE IMAGE |
| 141 | + // */ |
| 142 | + // SealedObject si = (SealedObject) ois.readObject(); |
| 143 | + // System.out.println("(1)"); |
| 144 | + // IMAGE_OBJ iobj = (IMAGE_OBJ) si.getObject(desCipher_decr); |
| 145 | + // System.out.println("(2) "); |
| 146 | + // iobj.SaveImage("./IMAGE_RECEIVED.png"); |
| 147 | + // System.out.println("(3)"); |
| 148 | + |
| 149 | + // close streams |
| 150 | + oos.close(); |
| 151 | + ois.close(); |
| 152 | + |
| 153 | + /* |
| 154 | + * Close connection |
| 155 | + */ |
| 156 | + s.close(); |
| 157 | + } |
| 158 | + catch(Exception e) { |
| 159 | + System.err.print("[ERROR] ::" + e); |
| 160 | + } |
| 161 | + } |
| 162 | +} |
0 commit comments