Quote:
Originally Posted by TomasLT
hmmm. but when i try to generate new Proxy public and privates keys, i got this output:
Code:
Proxy priv key 64 : 20c70d0a520e615b865b43493d22e9a463b6d105f5aa514c93d8c5894a68cf7b
Proxy pub key 128 : 2ded810c4e6b8bc9517499af8f578b13e7d0d151173c456939531de1b8621dc589696b00f951513a9280f92ff3a34c9443b46c70dcc295e5bb5495a81ee42409
And why priv key is only 64b only ?
And here is how do i generate it:
Code:
BigInteger p = new BigInteger(p1, 0x10);
BigInteger g = new BigInteger(g1, 0x10);
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DiffieHellman");
DHParameterSpec params = new DHParameterSpec(p, g, 128);
keyGen.initialize(params);
KeyPair kp = keyGen.generateKeyPair();
ProxyPrivKey = ((DHPrivateKey) kp.getPrivate()).getX().toString(16);
ProxyPubKey = ((DHPublicKey) kp.getPublic()).getY().toString(16);
|
getInstance("DH") is good.. doing DiffieHellman should be fine too.
you should be using another overload of DHParameterSpec constructor.
DHParameterSpec(P, G);
the private key should be 97 bytes usually.
Here are a couple of my snippets
obtaining DHParameterSpec, nvm about the br.readInt32() thingy.. it is only a binary reader i wrote myself. I didn't know about bytebuffer back then. should have used bytebuffer
Code:
this.P_string = new String(br.readBytes((int) br.readInt32()), "UTF8");
this.G_string = new String(br.readBytes((int) br.readInt32()), "UTF8");
this.P = new BigInteger(this.P_string, 16);
this.G = new BigInteger(this.G_string, 16);
//------------------------------------------------------
this.dhparam = new DHParameterSpec(this.P, this.G); //ok
this.serverPublicKey = new String(br.readBytes((int) br.readInt32()), "UTF8");
this.spublicKey = new DHPublicKeySpec(new BigInteger(this.serverPublicKey,16), this.P, this.G); //ok
generate proxy key pair
Code:
private void generateProxyKeyPair() throws NoSuchAlgorithmException, InvalidAlgorithmParameterException{
keyGen = KeyPairGenerator.getInstance("DH"); //ok
keyGen.initialize(dhparam, new SecureRandom()); //ok
do {
kpair = keyGen.generateKeyPair();
} while(((DHPublicKey)kpair.getPublic()).getY().toString(16).length() != this.serverPublicKey.length());
this.privateKey = (DHPrivateKey) kpair.getPrivate(); //ok
this.proxyPriKey = ((DHPrivateKey) kpair.getPrivate()).getX().toString(16).toUpperCase();
this.proxyPubKey = ((DHPublicKey) kpair.getPublic()).getY().toString(16).toUpperCase();
}
Add On:
didn't see your 2nd post.
Yes, after many tries i figured out the easiest way is to store keys both as strings and key objects. I needed them in both forms ... so ... that is just my implementation. You can come up with yours.
Strings can be converted to BigInteger. BigInteger can be converted to to DHPublicKeySpec and etc... DHPublicKeySpec and its private counterpart are used to generate shared key in keyfactory.
i stored my sharedkey in byte[]