title: BouncyCastle ECDH Key Agreement Fails
body: <p>I implemented a Elliptic Curve Diffie Hellman cryptography using BouncyCastle API. But, it doesn't seem that the key agreement is working properly. It prints false.</p>
  
  <p>Where did I do wrong? Thank you.</p>
  
  <pre><code>ECParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec("B-571");
  
      KeyPairGenerator g = 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());
  
      //
      // agreement
      //
      aKeyAgree.doPhase(bKeyPair.getPublic(), true);
      bKeyAgree.doPhase(aKeyPair.getPublic(), true);
  
      byte[] aSecret = aKeyAgree.generateSecret();
      byte[] bSecret = bKeyAgree.generateSecret();
  
      System.out.println(aSecret);
      System.out.println(bSecret);
      if (aSecret.equals(bSecret)){
          return true;
      } else { return false; }
  </code></pre>