public static void main(String[] args) { if (args.length == 1) { if (args[0].equals("-genkey")) { try { CipherUtils.generateKey(); } catch (KeyStoreException | CertificateException | NoSuchAlgorithmException | IOException e) { e.printStackTrace(); } System.exit(0); } else { System.exit(-1); } } try { Socket s = new Socket("localhost", Servidor.PORT); ObjectOutputStream oos = new ObjectOutputStream(s.getOutputStream()); ObjectInputStream ois = new ObjectInputStream(s.getInputStream()); CipherUtils cipherUtils = new CipherUtils(ois, oos); String test; BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in)); while ((test = stdIn.readLine()) != null) { cipherUtils.encrypt(test); } } catch (Exception e) { e.printStackTrace(); } }
@Override public void run() { ObjectInputStream ois = null; ObjectOutputStream oos = null; try { oos = new ObjectOutputStream(mSocket.getOutputStream()); ois = new ObjectInputStream(mSocket.getInputStream()); // Enviar o nosso g^Y oos.writeUTF(mPrivateKey.toString()); oos.flush(); // Receber o g^X da Alice BigInteger gX = new BigInteger(ois.readUTF()); // Calcular a chave mKey = mGpowModP.modPow(gX, mPrime); CipherUtils cipherUtils = new CipherUtils(ois, oos, mKey); String test; BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in)); while ((test = stdIn.readLine()) != null) { cipherUtils.encrypt(test); } } catch (Exception e) { e.printStackTrace(); } finally { if (ois != null) { try { ois.close(); } catch (IOException e) { e.printStackTrace(); } } if (oos != null) { try { oos.close(); } catch (IOException e) { e.printStackTrace(); } } } }