title: Using elliptic curve Diffie-Hellman with cofactor key for generating symmetric key
body: <p>I am new to ECDH and wanted to generate the a secret key in Java. I wanted to use Elliptic curve Diffie-Hellman with cofactor key derivation. I'm using the P-256 curve for the elliptic curve operations. I'm planning to use the resulting secret as the symmetric key for my block cipher.</p>
  
  <p>I have been working on this, and have the below working sample. This generates the secret key.</p>
  
  <pre><code>Provider BC = new org.bouncycastle.jce.provider.BouncyCastleProvider();
  ProviderList.newList(BC);
  ECParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec("P-256");   
  KeyPairGenerator g = (KeyPairGenerator) KeyPairGenerator.getInstance(
              "ECDH",BC);
  g.initialize(ecSpec, new SecureRandom());
  
  KeyPair aKeyPair = g.generateKeyPair();
  
  KeyAgreement aKeyAgree = KeyAgreement.getInstance("ECDH", BC);
  
  aKeyAgree.init(aKeyPair.getPrivate());
  
  KeyPair bKeyPair = g.generateKeyPair();
  
  KeyAgreement bKeyAgree = KeyAgreement.getInstance("ECDH", BC);
  
  bKeyAgree.init(bKeyPair.getPrivate());
  
  aKeyAgree.doPhase(bKeyPair.getPublic(), true);
  bKeyAgree.doPhase(aKeyPair.getPublic(), true);
  
  byte[] aSecret = aKeyAgree.generateSecret();
  byte[] bSecret = bKeyAgree.generateSecret();
  
  System.out.println(new String(aSecret));
  System.out.println(new String(bSecret));
  </code></pre>
  
  <p>I am afraid that I am using an ephemeral private and public key instead of static keys. Please tell me how I can generate the secret key using a static private and public key.</p>