title: Java Diffie-Hellman key exchange
body: <p>I'm trying to execute code to perform the Diffie-Hellman key exchange. I sourced the code from an example online (forget where now).
  I had to import the bouncycastle.jar, which I assumed worked up until execution.</p>
  
  <p><img src="https://i.stack.imgur.com/ypoi7.png" alt="stacktrace screenshot"></p>
  
  <p>my code:</p>
  
  <pre><code>package testproject;
  
  import java.math.BigInteger;
  import java.security.KeyPair;
  import java.security.KeyPairGenerator;
  import java.security.MessageDigest;
  import java.security.SecureRandom;
  import java.security.Security;
  import javax.crypto.KeyAgreement;
  import javax.crypto.spec.DHParameterSpec;
  
  public class KeyGen {
  
    private static BigInteger g512 = new BigInteger("1234567890", 16);
    //generates a random, non-negative integer for Base
  
    private static BigInteger p512 = new BigInteger("1234567890", 16);
    //generates a random, non-negative integer for Prime
  
    public static void main(String[] args) throws Exception {
      Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
      DHParameterSpec dhParams = new DHParameterSpec(p512, g512);
      //Specify parameters to use for the algorithm
      KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DH", "BC");
      //Define specific algorithm to use "diffie-hellman", with provider "bc"
  
      keyGen.initialize(dhParams, new SecureRandom());
      //initialize with parameters &amp; secure random seed
  
      KeyAgreement aKeyAgree = KeyAgreement.getInstance("DH", "BC");
      //define algorithm for A's key agreement
      KeyPair aPair = keyGen.generateKeyPair();
      //generate keyPair for A
  
      KeyAgreement bKeyAgree = KeyAgreement.getInstance("DH", "BC");
      //define algorithm for B's key agreement
      KeyPair bPair = keyGen.generateKeyPair();
      //generate keyPair for B
  
      aKeyAgree.init(aPair.getPrivate());
      //initialize A's keyAgreement with A's private key
      bKeyAgree.init(bPair.getPrivate());
      //initialize B's keyAgreement with B's private key
  
      aKeyAgree.doPhase(bPair.getPublic(), true);
      //do last phase of A's keyAgreement with B's public key
      bKeyAgree.doPhase(aPair.getPublic(), true);
      //do last phase of B's keyAgreement with A's public key
  
      MessageDigest hash = MessageDigest.getInstance("SHA1", "BC");
  
      System.out.println(new String(hash.digest(aKeyAgree.generateSecret())));
      //generate secret key for A, hash it.
      System.out.println(new String(hash.digest(bKeyAgree.generateSecret())));
      //generate secret key for B, hash it.
    }
  }
  </code></pre>
  
  <p>This is the line causing the problem:</p>
  
  <pre><code>KeyPair aPair = keyGen.generateKeyPair();
  </code></pre>
  
  <p>I'm confused as to what the error is, as I've found each of the methods it's returning 'unknown source' for.</p>
  
  <p>Any light shed on this would be much appreciated.</p>
  
  <p>Continued(Edit):
  <a href="https://stackoverflow.com/questions/22492417/java-diffie-hellman-encryption-wrong-output">Java - Diffie-Hellman Encryption - Wrong Output</a></p>