KeyAgreement

Elliptic curve DiffieHellman key agreement is also supported. The KeyPairGenerator instance must be created with "ECDH" as the algorithm.

The following example also exports the public key from the parties and imports them again to be used for the generation of the secret.

final KeyPairGenerator kpg = KeyPairGenerator.getInstance("ECDH", SepiorProvider.PROVIDER_NAME);
kpg.initialize(256);
final KeyPair kp1 = kpg.generateKeyPair();
final KeyPair kp2 = kpg.generateKeyPair();

final KeyFactory kf = KeyFactory.getInstance("EC"); //The standard Java KeyFactory

X509EncodedKeySpec ks1 = new X509EncodedKeySpec(kp1.getPublic().getEncoded());
final PublicKey pk1 = kf.generatePublic(ks1);

X509EncodedKeySpec ks2 = new X509EncodedKeySpec(kp2.getPublic().getEncoded());
final PublicKey pk2 = kf.generatePublic(ks2);

final KeyAgreement ka1 = KeyAgreement.getInstance("ECDH", SepiorProvider.PROVIDER_NAME);
ka1.init(kp1.getPrivate(), new SecureRandom());

final KeyAgreement ka2 = KeyAgreement.getInstance("ECDH", SepiorProvider.PROVIDER_NAME);
ka2.init(kp2.getPrivate(), new SecureRandom());

ka1.doPhase(pk2, false);
ka2.doPhase(pk1, false);

final byte[] secret1 = ka1.generateSecret();
final byte[] secret2 = ka2.generateSecret();