Class KeyAgreement

java.lang.Object
com.codename1.security.KeyAgreement

public final class KeyAgreement extends Object

Elliptic-curve Diffie-Hellman key agreement over P-256.

Two parties that each hold a key pair from KeyGenerator.ec(int) exchange public keys; each combines its own private key with the other's public key and both arrive at the same secret, which no one watching the exchange can compute. That secret is not a key yet -- run it through Hkdf first.

KeyPair alice = KeyGenerator.ec(256);
KeyPair bob = KeyGenerator.ec(256);
byte[] a = KeyAgreement.ecdh(alice.getPrivateKey(), bob.getPublicKey());
byte[] b = KeyAgreement.ecdh(bob.getPrivateKey(), alice.getPublicKey());
// a and b hold the same 32 bytes

The computation runs in the platform's native crypto provider.

  • Method Details

    • ecdh

      public static byte[] ecdh(PrivateKey mine, PublicKey theirs)

      Computes the ECDH shared secret: the 32-byte x coordinate of the product of mine and theirs, as SEC 1 and the JCA define it.

      Parameters
      • mine: this side's EC private key

      • theirs: the other side's EC public key

      Throws
      • CryptoException: if either key is not a P-256 key, or the public key is not a point on the curve