Pagina 1 di 2 1 2 ultimoultimo
Visualizzazione dei risultati da 1 a 10 su 12
  1. #1

    Decrittare un messaggio

    Ciao a tutti.
    Vi spiego il mio problema....
    ho fatto un programmino in Java che, tramite DES, critta una stringa inserita dall'utente e la salva in un file.

    Ora però vorrei fare in modo che il programma prenda il file, legga quello che c'è dentro, e in poche parole mi ridia la stringa di partenza.

    Riesco solo a fargli leggere il file....ma il processo di "decrittazione" non mi viene...
    potete darmi una mano?
    grazie
    www.balter.it

  2. #2
    Utente di HTML.it
    Registrato dal
    Feb 2007
    Messaggi
    4,157
    quando tu fai una cifratura (DES in questo caso) usi una chiave per cifrare il messaggio. DES è un algoritmo a chiave simmetrica, significa che la stessa chiave è usata sia per cifrare che per decifrare.
    Ora sei riuscito a cifrare il tuo messaggio?
    Allo stesso modo ricava un'istanza Cipher che inizializzi in DECRYPT, l'operazione che effettui in questo caso è speculare alla cifratura (soprattutto stessa chiave).
    Di esempi funzionanti ce ne sono a marea (specie se vedi anche il JCE java), quindi posta il tuo codice e vediamo

  3. #3
    intanto grazie per la celere risposta.....
    qui il codice....
    il file java che utilizzo per CRITTARE:

    codice:
    import java.io.InputStreamReader;
    import java.io.BufferedReader ;
    import java.io.IOException;
    import java.io.File;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.io.FileWriter;
    
    public class Critto {
    
      public static void main (String args[]) throws IOException {
    InputStreamReader reader = new InputStreamReader (System.in);
    System.out.print("Inserisci la chiave di cifratura: \n");
    BufferedReader myInput = new BufferedReader (reader);
    String str= new String();
    
    String str1= new String();
    str = myInput.readLine();
    System.out.print("Inserisci la parola o frase da criptare: \n");
    str1 = myInput.readLine();
    
        try {
    
            DesEncrypter encrypter = new DesEncrypter(str);
    
            String encrypted = encrypter.encrypt(str1);
            System.out.println("Encrypted: "+encrypted);
    
            String decrypted = encrypter.decrypt(encrypted);
            FileWriter fw = new FileWriter ("crittato.txt");
            fw.write (encrypted);
            fw.close ();
            System.out.println("Decrypted: "+decrypted);
        } catch (Exception e) {
        }
      }
    }

    quello per DECRITTARE:

    codice:
    import java.io.*;
    
    class Decritto {
    public static void main(String args[]) throws Exception {
    FileReader fr = new FileReader("crittato.txt");
    BufferedReader br = new BufferedReader(fr);
    String encrypted;
    while((encrypted = br.readLine()) != null) {
    System.out.println(encrypted);
    }
    fr.close();
    }
    }
    non so che righe di codice scrivere per far la decrittazione....

    grazie
    www.balter.it

  4. #4
    Utente di HTML.it
    Registrato dal
    Feb 2007
    Messaggi
    4,157
    DesEncrypter che classe è?
    Poi perché tenti l'apertura brutale del file? quanto ne sai di sicurezza e crittografia?
    Inizia a guardare qui

  5. #5
    questa è la classe DesEncrypter......
    perchè mi chiedi dell'apertura "brutale" del file?
    avresti ulteriori consigli?

    codice:
    import javax.crypto.*;
    import javax.crypto.spec.*;
    import java.io.*;
    
    public class DesEncrypter {
            Cipher ecipher;
            Cipher dcipher;
    
            // 8-byte Salt
            byte[] salt = {
                (byte)0xA9, (byte)0x9B, (byte)0xC8, (byte)0x32,
                (byte)0x56, (byte)0x35, (byte)0xE3, (byte)0x03
            };
    
            // Iteration count
            int iterationCount = 19;
    
            DesEncrypter(String passPhrase) {
                try {
                    // Create the key
                    PBEKeySpec keySpec = new PBEKeySpec(passPhrase.toCharArray(), salt, iterationCount);
                    SecretKey key = SecretKeyFactory.getInstance(
                        "PBEWithMD5AndDES").generateSecret(keySpec);
                    ecipher = Cipher.getInstance(key.getAlgorithm());
                    dcipher = Cipher.getInstance(key.getAlgorithm());
    
                    // Prepare the parameter to the ciphers
                    PBEParameterSpec paramSpec = new PBEParameterSpec(salt, iterationCount);
    
                    // Create the ciphers
                    ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
                    dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec);
                }
                catch (java.security.InvalidAlgorithmParameterException e) {
                }
                catch (java.security.spec.InvalidKeySpecException e) {
                }
                catch (javax.crypto.NoSuchPaddingException e) {
                }
                catch (java.security.NoSuchAlgorithmException e) {
                }
                catch (java.security.InvalidKeyException e) {
                }
            }
    
            public String encrypt(String str) {
                try {
                    // Encode the string into bytes using utf-8
                    byte[] utf8 = str.getBytes("UTF8");
    
                    // Encrypt
                    byte[] enc = ecipher.doFinal(utf8);
    
                    // Encode bytes to base64 to get a string
                    return new sun.misc.BASE64Encoder().encode(enc);
                }
                catch (javax.crypto.BadPaddingException e) {
                }
                catch (IllegalBlockSizeException e) {
                }
                catch (UnsupportedEncodingException e) {
                }
                catch (java.io.IOException e) {
                }
                return null;
            }
    
            public String decrypt(String str) {
                try {
                    // Decode base64 to get bytes
                    byte[] dec = new sun.misc.BASE64Decoder().decodeBuffer(str);
    
                    // Decrypt
                    byte[] utf8 = dcipher.doFinal(dec);
    
                    // Decode using utf-8
                    return new String(utf8, "UTF8");
                }
                catch (javax.crypto.BadPaddingException e) {
                }
                catch (IllegalBlockSizeException e) {
                }
                catch (UnsupportedEncodingException e) {
                }
                catch (java.io.IOException e) {
                }
                return null;
            }
        }
    grazie
    www.balter.it

  6. #6
    Utente di HTML.it
    Registrato dal
    Feb 2007
    Messaggi
    4,157
    perché in genere io non applico cosi la crittografia.
    Ora perché non metti uno stacktrace? Non si sa mai cosa va male, anziché lasciare i catch vuoti metti un trace

    Come fai la cifratura fai la decifratura, in modo simmetrico, te l'ho già detto.
    Ma devi conoscere la password ed è lì il problema

    codice:
    import java.io.*;
    
    class Decritto {
    	public static void main(String args[]) throws Exception {
    		FileReader fr = new FileReader("crittato.txt");
    		BufferedReader br = new BufferedReader(fr);
    		String encrypted;
    		//QUALE E' LA PASS??? COME FAI A CONOSCERLA??
    		//DesEncrypter encrypter = new DesEncrypter(str);
    		DesEncrypter encrypter = new DesEncrypter("prova");
    
    		while ((encrypted = br.readLine()) != null) {
    			System.out.println(encrypted);
    
    			System.out.println("Encrypted: " + encrypted);
    
    			String decrypted = encrypter.decrypt(encrypted);
    
    			System.out.println("Decrypted: " + decrypted);
    		}
    		fr.close();
    	}
    }

  7. #7
    intanto grazie.....
    allora....l'ultimo problema ora è vedere se il messaggio che "decritto" è stato o meno modificato......devo fare dei controlli di sicurezza...
    sai darmi una mano anche in questo?
    grazie
    www.balter.it

  8. #8
    Utente di HTML.it
    Registrato dal
    Feb 2007
    Messaggi
    4,157
    solo con DES?

  9. #9
    no no...con quello che le API di java offrono....
    www.balter.it

  10. #10
    Utente di HTML.it
    Registrato dal
    Feb 2007
    Messaggi
    4,157
    ripeto la domanda: quanto ne sai di crittografia?
    Il link che ti ho dato poco fa è una guida alle API di sicurezza, ma se non hai chiari alcuni concetti base è alquanto inutile continuare anche a parlarne

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.