@Test public void computeSharedKey() { KeyPair keyPair1 = encryptor.generateNewKeys(); KeyPair keyPair2 = encryptor.generateNewKeys(); SharedKey sharedPub1Priv2 = encryptor.computeSharedKey(keyPair1.getPublicKey(), keyPair2.getPrivateKey()); SharedKey sharedPriv1Pub2 = encryptor.computeSharedKey(keyPair2.getPublicKey(), keyPair1.getPrivateKey()); assertEquals(sharedPub1Priv2, sharedPriv1Pub2); LOGGER.info("SharedKey: {}", sharedPriv1Pub2.encodeToBase64()); }
@Override public SharedKey computeSharedKey(PublicKey publicKey, PrivateKey privateKey) { try { KeyAgreement keyAgreement = KeyAgreement.getInstance("ECDH"); java.security.PrivateKey privKey = keyFactory.generatePrivate(new PKCS8EncodedKeySpec(privateKey.getKeyBytes())); keyAgreement.init(privKey); LOGGER.info("Encode public key {}",publicKey.encodeToBase64()); X509EncodedKeySpec encodedKeySpec = new X509EncodedKeySpec(publicKey.getKeyBytes()); java.security.PublicKey pubKey = keyFactory.generatePublic(encodedKeySpec); keyAgreement.doPhase(pubKey, true); byte[] secret = keyAgreement.generateSecret(); MessageDigest sha3256 = new SHA3.Digest256(); final byte[] digest = sha3256.digest(secret); return SharedKey.from(digest); } catch (InvalidKeyException | NoSuchAlgorithmException | InvalidKeySpecException e) { LOGGER.error("unable to generate shared secret", e); throw new EncryptorException("unable to generate shared secret"); } }
EllipticalCurveEncryptor implements Encryptor { @Override public SharedKey computeSharedKey(PublicKey publicKey, PrivateKey privateKey) { try { KeyAgreement keyAgreement = KeyAgreement.getInstance("ECDH"); java.security.PrivateKey privKey = keyFactory.generatePrivate(new PKCS8EncodedKeySpec(privateKey.getKeyBytes())); keyAgreement.init(privKey); LOGGER.info("Encode public key {}",publicKey.encodeToBase64()); X509EncodedKeySpec encodedKeySpec = new X509EncodedKeySpec(publicKey.getKeyBytes()); java.security.PublicKey pubKey = keyFactory.generatePublic(encodedKeySpec); keyAgreement.doPhase(pubKey, true); byte[] secret = keyAgreement.generateSecret(); MessageDigest sha3256 = new SHA3.Digest256(); final byte[] digest = sha3256.digest(secret); return SharedKey.from(digest); } catch (InvalidKeyException | NoSuchAlgorithmException | InvalidKeySpecException e) { LOGGER.error("unable to generate shared secret", e); throw new EncryptorException("unable to generate shared secret"); } } }
EllipticalCurveEncryptor implements Encryptor { @Override public SharedKey computeSharedKey(PublicKey publicKey, PrivateKey privateKey) { try { KeyAgreement keyAgreement = KeyAgreement.getInstance("ECDH"); java.security.PrivateKey privKey = keyFactory.generatePrivate(new PKCS8EncodedKeySpec(privateKey.getKeyBytes())); keyAgreement.init(privKey); LOGGER.info("Encode public key {}",publicKey.encodeToBase64()); X509EncodedKeySpec encodedKeySpec = new X509EncodedKeySpec(publicKey.getKeyBytes()); java.security.PublicKey pubKey = keyFactory.generatePublic(encodedKeySpec); keyAgreement.doPhase(pubKey, true); byte[] secret = keyAgreement.generateSecret(); MessageDigest sha3256 = new SHA3.Digest256(); final byte[] digest = sha3256.digest(secret); return SharedKey.from(digest); } catch (InvalidKeyException | NoSuchAlgorithmException | InvalidKeySpecException e) { LOGGER.error("unable to generate shared secret", e); throw new EncryptorException("unable to generate shared secret"); } } EllipticalCurveEncryptor(final String symmetricCipher, final String ellipticCurve);  EllipticalCurveEncryptor(final String symmetricCipher, final String ellipticCurve, int nonceLength, int sharedKeyLength); }
EllipticalCurveEncryptor implements Encryptor { @Override public SharedKey computeSharedKey(PublicKey publicKey, PrivateKey privateKey) { try { KeyAgreement keyAgreement = KeyAgreement.getInstance("ECDH"); java.security.PrivateKey privKey = keyFactory.generatePrivate(new PKCS8EncodedKeySpec(privateKey.getKeyBytes())); keyAgreement.init(privKey); LOGGER.info("Encode public key {}",publicKey.encodeToBase64()); X509EncodedKeySpec encodedKeySpec = new X509EncodedKeySpec(publicKey.getKeyBytes()); java.security.PublicKey pubKey = keyFactory.generatePublic(encodedKeySpec); keyAgreement.doPhase(pubKey, true); byte[] secret = keyAgreement.generateSecret(); MessageDigest sha3256 = new SHA3.Digest256(); final byte[] digest = sha3256.digest(secret); return SharedKey.from(digest); } catch (InvalidKeyException | NoSuchAlgorithmException | InvalidKeySpecException e) { LOGGER.error("unable to generate shared secret", e); throw new EncryptorException("unable to generate shared secret"); } } EllipticalCurveEncryptor(final String symmetricCipher, final String ellipticCurve);  EllipticalCurveEncryptor(final String symmetricCipher, final String ellipticCurve, int nonceLength, int sharedKeyLength); @Override SharedKey computeSharedKey(PublicKey publicKey, PrivateKey privateKey); @Override byte[] seal(byte[] message, Nonce nonce, PublicKey publicKey, PrivateKey privateKey); @Override byte[] open(byte[] cipherText, Nonce nonce, PublicKey publicKey, PrivateKey privateKey); @Override byte[] sealAfterPrecomputation(byte[] message, Nonce nonce, SharedKey sharedKey); @Override byte[] openAfterPrecomputation(byte[] cipherText, Nonce nonce, SharedKey sharedKey); @Override Nonce randomNonce(); @Override KeyPair generateNewKeys(); @Override SharedKey createSingleKey(); }
EllipticalCurveEncryptor implements Encryptor { @Override public SharedKey computeSharedKey(PublicKey publicKey, PrivateKey privateKey) { try { KeyAgreement keyAgreement = KeyAgreement.getInstance("ECDH"); java.security.PrivateKey privKey = keyFactory.generatePrivate(new PKCS8EncodedKeySpec(privateKey.getKeyBytes())); keyAgreement.init(privKey); LOGGER.info("Encode public key {}",publicKey.encodeToBase64()); X509EncodedKeySpec encodedKeySpec = new X509EncodedKeySpec(publicKey.getKeyBytes()); java.security.PublicKey pubKey = keyFactory.generatePublic(encodedKeySpec); keyAgreement.doPhase(pubKey, true); byte[] secret = keyAgreement.generateSecret(); MessageDigest sha3256 = new SHA3.Digest256(); final byte[] digest = sha3256.digest(secret); return SharedKey.from(digest); } catch (InvalidKeyException | NoSuchAlgorithmException | InvalidKeySpecException e) { LOGGER.error("unable to generate shared secret", e); throw new EncryptorException("unable to generate shared secret"); } } EllipticalCurveEncryptor(final String symmetricCipher, final String ellipticCurve);  EllipticalCurveEncryptor(final String symmetricCipher, final String ellipticCurve, int nonceLength, int sharedKeyLength); @Override SharedKey computeSharedKey(PublicKey publicKey, PrivateKey privateKey); @Override byte[] seal(byte[] message, Nonce nonce, PublicKey publicKey, PrivateKey privateKey); @Override byte[] open(byte[] cipherText, Nonce nonce, PublicKey publicKey, PrivateKey privateKey); @Override byte[] sealAfterPrecomputation(byte[] message, Nonce nonce, SharedKey sharedKey); @Override byte[] openAfterPrecomputation(byte[] cipherText, Nonce nonce, SharedKey sharedKey); @Override Nonce randomNonce(); @Override KeyPair generateNewKeys(); @Override SharedKey createSingleKey();  }