Mubee
Mubee

Reputation: 67

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()));

Upvotes: 1

Views: 109

Answers (1)

Maarten Bodewes
Maarten Bodewes

Reputation: 93968

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.

Upvotes: 2

Related Questions