public static void main(String[] args) throws InvalidKeyException, IllegalStateException, NoSuchAlgorithmException { Security.addProvider(new BouncyCastleProvider()); ECDH alice = new ECDH(Curves.P256); ECDH bob = new ECDH(Curves.P256); PublicKey bobP = bob.getPublicKey(); PublicKey aliceP = alice.getPublicKey(); System.out.println("Alice C: "+alice.curve); System.out.println("Bob C: "+bob.curve); System.out.println("Alice P: "+toHex(aliceP.getEncoded())); System.out.println("Bob P: "+toHex(bobP.getEncoded())); System.out.println("Alice S: "+toHex(alice.getAgreedSecret(bob.getPublicKey()))); System.out.println("Bob S: "+toHex(bob.getAgreedSecret(alice.getPublicKey()))); }
public byte[] getHMACKey(ECPublicKey peerExponential) { synchronized(this) { lastUsedTime = System.currentTimeMillis(); } byte[] sharedKey = ecdh.getAgreedSecret(peerExponential); if (logMINOR) { Logger.minor(this, "Curve in use: " + ecdh.curve.toString()); if(logDEBUG) { Logger.debug(this, "My exponential: " + HexUtil.bytesToHex(ecdh.getPublicKey().getEncoded())); Logger.debug( this, "Peer's exponential: " + HexUtil.bytesToHex(peerExponential.getEncoded())); Logger.debug(this, "SharedSecret = " + HexUtil.bytesToHex(sharedKey)); } } return sharedKey; }
public void testGetAgreedSecret() throws InvalidKeyException, IllegalStateException, NoSuchAlgorithmException, InvalidAlgorithmParameterException { byte[] aliceS = alice.getAgreedSecret(bob.getPublicKey()); byte[] bobS = bob.getAgreedSecret(alice.getPublicKey()); assertNotNull(aliceS); assertNotNull(bobS); assertEquals(toHex(aliceS), toHex(bobS)); assertEquals(aliceS.length, curveToTest.derivedSecretSize); assertEquals(bobS.length, curveToTest.derivedSecretSize); }