27/04/07 14:41
anerol82
Sto scrivendo un'applicazione client server dove un client invia il suo nome, il server lo riceve poi estrae una informazione legata al client utilizza una password la invia al client il client la decifra e poi dovrebbe visualizzare la stringa corretta...ho usato PBE...ma mi da l'errore " PARAMETRO NON CORRETTO" lato client...quindi suppongo che lato server la cifratura avvenga correttamente...qualcuno può aiutarmi?? Devo usare qualche codifica??
La password che uso sia per decifrare che codificare è una stringa del tipo "240982"
mentre la stringa da codificare è del tipo "negativo"(in realtà sarebbe una stringa "12.4" quale potrebbe essere l'errore? La classe dove ho implementato le operazioni è questa:
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.*;
import java.security.spec.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import java.util.*;
public class Cryptex{
private static final int ITERATIONS = 1000;
public static String encrypt(char[] password, String plaintext) {
Random random = new Random();
byte[] salt = new byte[8];
random.nextBytes(salt);
PBEKeySpec keySpec = new PBEKeySpec(password);
try{
SecretKeyFactory keyFactory =SecretKeyFactory.getInstance("PBEWithMD5AndDES"
SecretKey key = keyFactory.generateSecret(keySpec);
PBEParameterSpec paramSpec =new PBEParameterSpec(salt, ITERATIONS);
Cipher cipher = Cipher.getInstance("PBEWithMD5AndDES"
cipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
byte[] ciphertext = cipher.doFinal(plaintext.getBytes("UTF8");
return new String(salt) + new String(ciphertext);
}
catch (NoSuchAlgorithmException e1) {
System.out.println("Algoritmo non supportato"
}
catch (NoSuchPaddingException e2) {
System.out.println("Padding non supportato"
}
catch (InvalidKeySpecException e3) {
System.out.println("Specificazione della chiave non valida"
}
catch (InvalidKeyException e4) {
System.out.println("Chiave non valida"
}
catch (InvalidAlgorithmParameterException e5) {
System.out.println("Parametro non valido"
}
catch (IllegalBlockSizeException e6) {
System.out.println("Dimensione blocco illegale"
}
catch (BadPaddingException e7) {
System.out.println("Bad Padding"
}
catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "Error";
}
public static String decrypt(char[] password, String input) {
try{
byte[] salt = input.substring(0,8).getBytes("UTF8"
byte[] ciphertext = input.substring( 8 ).getBytes("UTF8"
PBEKeySpec keySpec = new PBEKeySpec(password);
SecretKeyFactory keyFactory =
SecretKeyFactory.getInstance("PBEWithMD5AndDES"
SecretKey key = keyFactory.generateSecret(keySpec);
PBEParameterSpec paramSpec =
new PBEParameterSpec(salt, ITERATIONS);
Cipher cipher = Cipher.getInstance("PBEWithMD5AndDES"
cipher.init(Cipher.DECRYPT_MODE, key, paramSpec);
byte[] plaintextArray = cipher.doFinal(ciphertext);
return new String(plaintextArray );
} catch (NoSuchAlgorithmException e1) {
System.out.println("Algoritmo non supportato"
}
catch (NoSuchPaddingException e2) {
System.out.println("Padding non supportato"
}
catch (InvalidKeySpecException e3) {
System.out.println("Specificazione della chiave non valida"
}
catch (InvalidKeyException e4) {
System.out.println("Chiave non valida"
}
catch (InvalidAlgorithmParameterException e5) {
System.out.println("Parametro non valido"
}
catch (IllegalBlockSizeException e6) {
System.out.println("Dimensione blocco illegale"
}
catch (BadPaddingException e7) {
System.out.println("Bad Padding"
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "Decifratura Fallita";
}
}
mentre quando utilizzo lato client il metodo per la decifratura uso:
....
// ricezione esito
String esito,risultato;
String pwd= null;
System.out.println("INSERIRE PASSWORD = "+"/n"
try{
while ( (pwd=stdIn.readLine()) != null){
try{
esito = inSock.readUTF();
risultato=Cryptex.decrypt(pwd.toCharArray(),esito);
System.out.println("Esito esame: " + risultato);
// chiudo la socket in downstream
socket.shutdownInput();
System.out.println("Terminata la chiusura della socket: " + socket);
}
....
...
Qual è l'errore???? grazie in anticipo
La password che uso sia per decifrare che codificare è una stringa del tipo "240982"
mentre la stringa da codificare è del tipo "negativo"(in realtà sarebbe una stringa "12.4" quale potrebbe essere l'errore? La classe dove ho implementato le operazioni è questa:
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.*;
import java.security.spec.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import java.util.*;
public class Cryptex{
private static final int ITERATIONS = 1000;
public static String encrypt(char[] password, String plaintext) {
Random random = new Random();
byte[] salt = new byte[8];
random.nextBytes(salt);
PBEKeySpec keySpec = new PBEKeySpec(password);
try{
SecretKeyFactory keyFactory =SecretKeyFactory.getInstance("PBEWithMD5AndDES"
SecretKey key = keyFactory.generateSecret(keySpec);
PBEParameterSpec paramSpec =new PBEParameterSpec(salt, ITERATIONS);
Cipher cipher = Cipher.getInstance("PBEWithMD5AndDES"
cipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
byte[] ciphertext = cipher.doFinal(plaintext.getBytes("UTF8");
return new String(salt) + new String(ciphertext);
}
catch (NoSuchAlgorithmException e1) {
System.out.println("Algoritmo non supportato"
}
catch (NoSuchPaddingException e2) {
System.out.println("Padding non supportato"
}
catch (InvalidKeySpecException e3) {
System.out.println("Specificazione della chiave non valida"
}
catch (InvalidKeyException e4) {
System.out.println("Chiave non valida"
}
catch (InvalidAlgorithmParameterException e5) {
System.out.println("Parametro non valido"
}
catch (IllegalBlockSizeException e6) {
System.out.println("Dimensione blocco illegale"
}
catch (BadPaddingException e7) {
System.out.println("Bad Padding"
}
catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "Error";
}
public static String decrypt(char[] password, String input) {
try{
byte[] salt = input.substring(0,8).getBytes("UTF8"
byte[] ciphertext = input.substring( 8 ).getBytes("UTF8"
PBEKeySpec keySpec = new PBEKeySpec(password);
SecretKeyFactory keyFactory =
SecretKeyFactory.getInstance("PBEWithMD5AndDES"
SecretKey key = keyFactory.generateSecret(keySpec);
PBEParameterSpec paramSpec =
new PBEParameterSpec(salt, ITERATIONS);
Cipher cipher = Cipher.getInstance("PBEWithMD5AndDES"
cipher.init(Cipher.DECRYPT_MODE, key, paramSpec);
byte[] plaintextArray = cipher.doFinal(ciphertext);
return new String(plaintextArray );
} catch (NoSuchAlgorithmException e1) {
System.out.println("Algoritmo non supportato"
}
catch (NoSuchPaddingException e2) {
System.out.println("Padding non supportato"
}
catch (InvalidKeySpecException e3) {
System.out.println("Specificazione della chiave non valida"
}
catch (InvalidKeyException e4) {
System.out.println("Chiave non valida"
}
catch (InvalidAlgorithmParameterException e5) {
System.out.println("Parametro non valido"
}
catch (IllegalBlockSizeException e6) {
System.out.println("Dimensione blocco illegale"
}
catch (BadPaddingException e7) {
System.out.println("Bad Padding"
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "Decifratura Fallita";
}
}
mentre quando utilizzo lato client il metodo per la decifratura uso:
....
// ricezione esito
String esito,risultato;
String pwd= null;
System.out.println("INSERIRE PASSWORD = "+"/n"
try{
while ( (pwd=stdIn.readLine()) != null){
try{
esito = inSock.readUTF();
risultato=Cryptex.decrypt(pwd.toCharArray(),esito);
System.out.println("Esito esame: " + risultato);
// chiudo la socket in downstream
socket.shutdownInput();
System.out.println("Terminata la chiusura della socket: " + socket);
}
....
...
Qual è l'errore???? grazie in anticipo
aaa