Oppure

Loading
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
aaa
29/04/07 9:51
netarrow
Devi convertire a base64 per evitare di perdere bit per strada ;-)

Ah, usando base64 8 bytes diventano 12 caratteri

Questo è il mio output:

J+yyP0CmYGs=ahYDYJ2DI66ngmGuCPfmnLR7apaf/xnpxNP8r+8lk+RnmCPCTCE/Oeu4qLnrpG/P
quelsiasi testo quelsiasi testo qualsiasi testo

Versione corretta:

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.*;

import sun.misc.*;

public class Cryptex {


private static final int ITERATIONS = 1000;

public static void main(String args[]) {
String test = encrypt("password".toCharArray(), "quelsiasi testo quelsiasi testo qualsiasi testo");
System.out.println(test);
System.out.println(decrypt("password".toCharArray(), test));
}

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{
BASE64Encoder encoder = new BASE64Encoder();
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 encoder.encode(salt) + encoder.encode(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{
BASE64Decoder decoder = new BASE64Decoder();
String salt64 = input.substring(0, 12);
String ciphertext64 = input.substring( 12 );
byte[] salt = decoder.decodeBuffer(salt64);
byte[] ciphertext = decoder.decodeBuffer(ciphertext64);
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) {
e.printStackTrace();
}

return "Decifratura Fallita";
}

}
/*
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);
*/
Ultima modifica effettuata da netarrow 29/04/07 10:11
aaa
29/04/07 16:09
Shutdown
Bravo Matteo,
sempre pronto per quanto
riguarda questi campi.
aaa