/** * Performs a handshake for key exchange between two people. This method must be called by both * people to ensure that the agreement has been reached on both ends. * * @param otherKey the serialized string version of the public key for the other person. Must not * be null. */ public void handShake(String otherKey) { if (DEBUG) { System.out.println("Performing handshake..."); } try { byte[] otherPubBytes = new Base64().decode(otherKey); ByteArrayInputStream bais = new ByteArrayInputStream(otherPubBytes); ObjectInputStream ois = new ObjectInputStream(bais); KeyAgreement keyAgree = KeyAgreement.getInstance("DiffieHellman"); keyAgree.init(privKey); Key otherPub = (Key) ois.readObject(); keyAgree.doPhase(otherPub, true); msgKey = keyAgree.generateSecret("DESede"); cipher = Cipher.getInstance("DESede"); mac = Mac.getInstance("HmacSHA512"); if (DEBUG) { System.out.println("Handshake completed"); } } catch (Exception e) { System.out.println("Could not complete handshake..."); System.out.println("Agreement not confirmed"); if (DEBUG) { e.printStackTrace(); } } }
public void run() { try { ObjectInputStream ois = new ObjectInputStream(s.getInputStream()); ObjectOutputStream oos = new ObjectOutputStream(s.getOutputStream()); BigInteger bg = dhSpec.getG(); BigInteger bp = dhSpec.getP(); oos.writeObject(bg); oos.writeObject(bp); KeyPairGenerator kpg = KeyPairGenerator.getInstance("DH"); kpg.initialize(1024); KeyPair kpa = (KeyPair) ois.readObject(); KeyAgreement dh = KeyAgreement.getInstance("DH"); KeyPair kp = kpg.generateKeyPair(); oos.writeObject(kp); dh.init(kp.getPrivate()); Key pk = dh.doPhase(kpa.getPublic(), true); MessageDigest sha256 = MessageDigest.getInstance("SHA-256"); byte[] rawbits = sha256.digest(dh.generateSecret()); Cipher c = Cipher.getInstance(CIPHER_MODE); SecretKey key = new SecretKeySpec(rawbits, 0, 16, "AES"); byte ivbits[] = (byte[]) ois.readObject(); IvParameterSpec iv = new IvParameterSpec(ivbits); c.init(Cipher.DECRYPT_MODE, key, iv); Mac m = Mac.getInstance("HmacSHA1"); SecretKey mackey = new SecretKeySpec(rawbits, 16, 16, "HmacSHA1"); m.init(mackey); byte ciphertext[], cleartext[], mac[]; try { while (true) { ciphertext = (byte[]) ois.readObject(); mac = (byte[]) ois.readObject(); if (Arrays.equals(mac, m.doFinal(ciphertext))) { cleartext = c.update(ciphertext); System.out.println(ct + " : " + new String(cleartext, "UTF-8")); } else { // System.exit(1); System.out.println(ct + "error"); } } } catch (EOFException e) { cleartext = c.doFinal(); System.out.println(ct + " : " + new String(cleartext, "UTF-8")); System.out.println("[" + ct + "]"); } finally { if (ois != null) ois.close(); if (oos != null) oos.close(); } } catch (Exception e) { e.printStackTrace(); } }
public byte[] getE() throws Exception { if (e == null) { DHParameterSpec dhSkipParamSpec = new DHParameterSpec(p, g); myKpairGen.initialize(dhSkipParamSpec); KeyPair myKpair = myKpairGen.generateKeyPair(); myKeyAgree.init(myKpair.getPrivate()); // BigInteger x=((javax.crypto.interfaces.DHPrivateKey)(myKpair.getPrivate())).getX(); byte[] myPubKeyEnc = myKpair.getPublic().getEncoded(); e = ((javax.crypto.interfaces.DHPublicKey) (myKpair.getPublic())).getY(); e_array = e.toByteArray(); } return e_array; }