Oppure

Loading
14/01/11 8:02
albe82
Ciao a tutti...
è la prima volta che scrivo in questo forum.
Ho una richiesta di aiuto da fare....
Devo fare un programmino che mi nasconde una frase (che inserisco "al volo";) dentro un'immagine........e fin qui tutto bene....
questo il mio file:


package crypto;
import java.awt.image.*;
import java.io.*;
import javax.imageio.ImageIO;

public class Encrypt {
    public static void main(String[] args) throws IOException {
        InputStreamReader reader = new InputStreamReader (System.in);
System.out.print("Inserisci il testo da nascondere nell'immagine: \n");
BufferedReader myInput = new BufferedReader (reader);
String msg= new String();
msg = myInput.readLine();

        InputStreamReader sreader = new InputStreamReader (System.in);
System.out.print("Inserisci il testo da nascondere nell'immagine: \n");
BufferedReader pathmyInput = new BufferedReader (sreader);
String path= new String();
path = pathmyInput.readLine();

File imgFile = new File(path);
        BufferedImage source = null;
        try {
            source = ImageIO.read(imgFile);
        } catch (IOException ex) {
        }

        BufferedImage dest = new BufferedImage(source.getWidth(),source.getHeight(),BufferedImage.TYPE_INT_ARGB);

        int tmp, pixel, index;
        for(int i=0; i<source.getWidth(); i++) {
            for(int j=0; j<source.getHeight(); j++) {
                pixel = source.getRGB(i,j);
                index = (i*source.getHeight())+j;
                if(index<msg.length()) {
                    tmp = msg.charAt(index);
                    int chan_1 = tmp>>6;
                    int chan_2 = (tmp^(chan_1<<6))>>4;
                    int chan_3 = (tmp^(chan_1<<6)^(chan_2<<4))>>2;
                    int chan_4 = (tmp^(chan_1<<6)^(chan_2<<4)^(chan_3<<2));
                    pixel = pixel^(chan_1<<24)^(chan_2<<16)^(chan_3<<8)^(chan_4);
                }
                dest.setRGB(i,j,pixel);
            }
        }

        //Salvo l'immagine
        String ext="png";
        imgFile = new File("C:/Users/Alberto/Desktop/Image."+ext);
        try {
            ImageIO.write(dest,ext,imgFile);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}




alla fine di questo processo ottengo un'immagine di peso superiore alla prima, che contiene il messaggio nascosto.

Ora però voglio prendere l'immagine appena creata, decrittarla, e stampare la frase...

package crypto;
import java.awt.image.*;
import java.io.*;
import javax.imageio.ImageIO;

public class Decrypt {

    public static void main(String[] args) throws IOException {

        BufferedImage imgMod = null;
                  File imgFile = new File("C:/Users/Alberto/Desktop/Image.png");
        BufferedImage source = null;
        try {
 source = ImageIO.read(imgFile);
        } catch (IOException ex) {
        }
        try {
           imgMod = ImageIO.read(imgFile);
        } catch (IOException ex) {
        }
        for(int i=0; i<imgMod.getWidth(); i++) {
            for(int j=0; j<imgMod.getHeight(); j++) {
                int tmp2 = imgMod.getRGB(i,j)^source.getRGB(i,j);
                int chan_1 = tmp2>>24;
                int chan_2 = (tmp2^(chan_1<<24))>>16;
                int chan_3 = (tmp2^(chan_1<<24)^(chan_2<<16))>>8;
                int chan_4 = (tmp2^(chan_1<<24)^(chan_2<<16)^(chan_3<<8));
                char c = (char)((chan_1<<6)^(chan_2<<4)^(chan_3<<2)^(chan_4));
                if(c!=0) {
                  System.out.print(c);
                }
            }
        }        }        }


il problema è che, quando il file viene compilato, non mi stampa a video NIENTE.
Vuoto.
sapreste dirmi dov'è il problema?

Inoltre vorrei aggiungere dei controlli per la sicurezza....
mi sapreste aiutare?
grazie
aaa