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(); } } } }
/* * Initial IV client to server: HASH (K || H || "A" || session_id) Initial * IV server to client: HASH (K || H || "B" || session_id) Encryption key * client to server: HASH (K || H || "C" || session_id) Encryption key * server to client: HASH (K || H || "D" || session_id) Integrity key client * to server: HASH (K || H || "E" || session_id) Integrity key server to * client: HASH (K || H || "F" || session_id) */ public void init( String encryptionAlgorithm, String cipherAlgorithm, int keyLength, String macAlgorithm, byte[] K, byte[] H, byte[] sessionId) throws GeneralSecurityException { MessageDigest sha = MessageDigest.getInstance("SHA-1"); sha.reset(); sha.update( new SshPacketBuilder().writeMpInt(K).append(H).writeByte('A').append(sessionId).finish()); byte[] iv = sha.digest(); sha.reset(); sha.update( new SshPacketBuilder().writeMpInt(K).append(H).writeByte('C').append(sessionId).finish()); byte[] cipherKey = sha.digest(); try { cipher = Cipher.getInstance(encryptionAlgorithm + "/" + cipherAlgorithm + "/NoPadding"); iv = CipherUtils.expandKey(K, H, iv, sha, cipher.getBlockSize()); cipherKey = CipherUtils.expandKey(K, H, cipherKey, sha, keyLength); iv = CipherUtils.shrinkKey(iv, cipher.getBlockSize()); cipherKey = CipherUtils.shrinkKey(cipherKey, keyLength); cipher.init( Cipher.ENCRYPT_MODE, new SecretKeySpec(cipherKey, encryptionAlgorithm), new IvParameterSpec(iv)); sha.reset(); sha.update( new SshPacketBuilder().writeMpInt(K).append(H).writeByte('E').append(sessionId).finish()); byte[] macKey = sha.digest(); mac = Mac.getInstance(macAlgorithm); macKey = CipherUtils.expandKey(K, H, macKey, sha, mac.getMacLength()); macKey = CipherUtils.shrinkKey(macKey, mac.getMacLength()); mac.init(new SecretKeySpec(macKey, macAlgorithm)); } catch (GeneralSecurityException e) { cipher = null; mac = null; throw e; } }