Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

java.security.InvalidKeyException: Incompatible parameters - help me!

0 views
Skip to first unread message

Junky

unread,
Apr 2, 2003, 8:50:48 PM4/2/03
to
I'm writing some test code that simply performs a diffie hellman key
exchange between two parties. The code is based on sample code I have
found online.

It is all in one method, and instead of two parties communicating
their public keys over a network of some kind, i simply get the public
key from one, and pass it to the other. But this is just for test
purposes. In my application of course, the key exchange will be done
with each party performing their respective operations on their own
side and somehow communicating their keys over the network.

Anyway, I'm already having this problem I can't figure out. When I
call the doPhase(...) method of my KeyAgreement objects (to combine
the other parties key with the private key) I get this:

java.security.InvalidKeyException: Incompatible parameters

Below is my code. Any help will be very much appreciated. I'm having
trouble getting the help I need with this one thus far.


import java.security.*;
import java.security.spec.*;
import javax.crypto.*;
import javax.crypto.spec.*;


public class test{

public static void main(String[] args){

try {
// Create the parameter generator for a 1024-bit DH key
pair
AlgorithmParameterGenerator paramGen1 =
AlgorithmParameterGenerator.getInstance("DH");
paramGen1.init(1024);
AlgorithmParameterGenerator paramGen2 =
AlgorithmParameterGenerator.getInstance("DH");
paramGen2.init(1024);

// Generate the parameters
AlgorithmParameters params1 =
paramGen1.generateParameters();
DHParameterSpec dhSpec1
= (DHParameterSpec)params1.getParameterSpec(DHParameterSpec.class);
AlgorithmParameters params2 =
paramGen2.generateParameters();
DHParameterSpec dhSpec2
= (DHParameterSpec)params2.getParameterSpec(DHParameterSpec.class);

// Use the values to generate a key pair
KeyPairGenerator keyGen1 =
KeyPairGenerator.getInstance("DH");
keyGen1.initialize(dhSpec1);
KeyPair keypair1 = keyGen1.generateKeyPair();
KeyPairGenerator keyGen2 =
KeyPairGenerator.getInstance("DH");
keyGen2.initialize(dhSpec2);
KeyPair keypair2 = keyGen2.generateKeyPair();

// Get the generated public and private keys
PrivateKey privateKey1 = keypair1.getPrivate();
PublicKey publicKey1 = keypair1.getPublic();
PrivateKey privateKey2 = keypair2.getPrivate();
PublicKey publicKey2 = keypair2.getPublic();

// Send the public key bytes to each other
byte[] publicKeyBytes2 = publicKey1.getEncoded();
byte[] publicKeyBytes1 = publicKey2.getEncoded();

// Convert the public key bytes into a PublicKey object
X509EncodedKeySpec x509KeySpec1 = new
X509EncodedKeySpec(publicKeyBytes1);
KeyFactory keyFact1 = KeyFactory.getInstance("DH");
publicKey1 = keyFact1.generatePublic(x509KeySpec1);
X509EncodedKeySpec x509KeySpec2 = new
X509EncodedKeySpec(publicKeyBytes2);
KeyFactory keyFact2 = KeyFactory.getInstance("DH");
publicKey2 = keyFact2.generatePublic(x509KeySpec2);

// Prepare to generate the secret key with the private key
and public key of the other party
KeyAgreement ka1 = KeyAgreement.getInstance("DH");
ka1.init(privateKey1);
ka1.doPhase(publicKey1, true);
KeyAgreement ka2 = KeyAgreement.getInstance("DH");
ka2.init(privateKey2);
ka2.doPhase(publicKey2, true);

// Generate the secret key
SecretKey secretKey1 = ka1.generateSecret("DES");
SecretKey secretKey2 = ka2.generateSecret("DES");

} catch (Exception e) {
e.printStackTrace();
}
}
}

0 new messages