Issue
I have a small program that starts by exchanging session key for communication and for that it use Diffie-Hellman key agreement. In Java first part is done like this:
KeyPairGenerator keyPairGenerator1 = KeyPairGenerator.getInstance("DH");
keyPairGenerator1.initialize(Skip.sDHParameterSpec);
KeyPair keyPair1 = keyPairGenerator1.generateKeyPair();
byte[] localKey1 = keyPair1.getPublic().getEncoded();
KeyAgreement keyAgreement1 = KeyAgreement.getInstance("DH");
keyAgreement1.init(keyPair1.getPrivate());
// getting remote key
keyAgreement1.doPhase(theirPublicKey2, true);
byte[] sharedKey1 = keyAgreement1.generateSecret();
then localKey is sent to the remote part, who sends back they part of data for calculation DH shared key. Problem, another program expects to get row data (big integer), and from java program I sent X509 encoded.
So how can I get that BigInteger (local Y value of DH protocol) from PublicKey? Or maybe there's another way to generate necessary DH parameters?
Solution
Just cast the key to a more specialized type...
DHPublicKey localKey = (DHPublicKey) keyPair1.getPublic();
BigInteger localY = localKey.getY();
Of course, if you haven't agreed on pre-defined parameters you may want to retrieve the parameters from the localKey
as well.
Answered By - Maarten Bodewes
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.