PDA

View Full Version : [JAVA] problema generazione chiave Diffie-Hellman


Miky Mouse
13-12-2008, 23:31
ho un client e un server che si scambiano (o dovrebbero scambiarsi) dati cifrati con DES... questo è quello che c'è prima, per generare una chiave.
solo che non funziona, anche se mi sono attenuto alla documentazione la doPhase solleva eccezione di InvalidKey dicendo che il parametro non è corretto.
ho ricontrollato ma non sembra un problema di trasmissione di dati, qualcuno potrebbe dirmi se il codice è corretto? (è preso in giro sul web e documentazioni varie)
il codice su client e server è lo stesso, questi si scambiano un oggetto serializzato con all'interno una variabile di tipo PublicKey (qui sotto ho semplificato il codice del send/receive tanto per far capire).
per ora mi basterebbe di stampare la chiave ottenuta...


private void DiffieHellman(){

String valuesInStr=genDHParams();
String[] values = valuesInStr.split(",");
BigInteger p = new BigInteger(values[0]);
BigInteger g = new BigInteger(values[1]);
int l = Integer.parseInt(values[2]);
try {
// Use the values to generate a key pair
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DH");
DHParameterSpec dhSpec = new DHParameterSpec(p, g, l);
keyGen.initialize(dhSpec);
KeyPair keypair = keyGen.generateKeyPair();

// Get the generated public and private keys
PrivateKey privateKey = keypair.getPrivate();
PublicKey publicKey = keypair.getPublic();

// Send the public key bytes to the other party...
stream.writeObject(new Pacchetto(publicKey));

// Retrieve the public key bytes of the other party
Pacchetto pacchetto=(PublicKey)stream.readObject();
publicKey=pacchetto.publicKey;

// Prepare to generate the secret key with the private key and public key of the other party
KeyAgreement ka = KeyAgreement.getInstance("DH");
ka.init(privateKey);
ka.doPhase(publicKey, true);
// Specify the type of key to generate;
// see e458 Listing All Available Symmetric Key Generators
String algorithm = "DES";

// Generate the secret key
SecretKey secretKey = ka.generateSecret(algorithm);

// Use the secret key to encrypt/decrypt data;
// see e462 Encrypting a String with DES
System.out.println( "chiave calcolata "+secretKey.toString());

}
catch (InvalidKeyException e) {System.out.println(e.toString());}
catch (InvalidAlgorithmParameterException e) {System.out.println(e.toString());
} catch (NoSuchAlgorithmException e) {System.out.println(e.toString());
} catch (IOException ex){ System.out.println(ex.toString());
} catch (ClassNotFoundException ex){System.out.println(ex.toString());
}
}
private static String genDHParams() {
try {
// Create the parameter generator for a 1024-bit DH key pair
AlgorithmParameterGenerator paramGen = AlgorithmParameterGenerator.getInstance("DH");
paramGen.init(1024);

// Generate the parameters
AlgorithmParameters params = paramGen.generateParameters();
DHParameterSpec dhSpec
= (DHParameterSpec)params.getParameterSpec(DHParameterSpec.class);

// Return the three values in a string
return ""+dhSpec.getP()+","+dhSpec.getG()+","+dhSpec.getL();
} catch (NoSuchAlgorithmException e) {
} catch (InvalidParameterSpecException e) {
}
return null;
}

Miky Mouse
15-12-2008, 12:36
up...

Miky Mouse
15-12-2008, 16:18
risolto... se a qualcuno interessa il problema è che i parametri g,p,l usati per generare la chiave devono essere uguali sul client e sul server... come facevo io invece venivano generati diversamente