Search code examples
cryptographybouncycastlediffie-hellman

Diffie-Hellman symmetry keys display issue


I am generating Symmetric-keys by using bouncy castle Diffie-Hellman key exchange protocol, but when I display my generated Secret then it gives me the following error

java.lang.IllegalStateException: Key agreement has not been completed yet

Assume from the below code that keys agreement process has be successfully completed and also from the hashes it confirms that both keys are the similar.

    MessageDigest   hash = MessageDigest.getInstance("SHA1");
    byte[] aShared = hash.digest(aKeyAgree.generateSecret());
    byte[] bShared = hash.digest(bKeyAgree.generateSecret());
    System.out.println(Arrays.toString(aKeyAgree.generateSecret()));

Solution

  • It's probably simply that calling aKeyAgree.generateSecret() twice is the issue. The key agreement should be performed just once. If you want to print the result you need to store it in a (temporary) variable instead.