Ver Mensaje Individual
  #1 (permalink)  
Antiguo 12/12/2014, 12:52
marcusaurelio
 
Fecha de Ingreso: enero-2007
Mensajes: 285
Antigüedad: 17 años, 3 meses
Puntos: 21
Steganografia de php a java

como va? estoy trabajando en dos scripts, uno en java y otro en php. el de php debe escribir una clave, mediante steganografria en una imagen . y el java debe leerla. asi de sencillo.. pero no lo es tanto (al menos para mi). si bien entiendo los conceptos de esta practica, no es algo que maneje.. por lo tanto procedi a buscar en google y encontre mil cosas.. que funcionan en php o en java pero ninguna puede generar una imagen que pueda ser leida por el otro lenguaje.

de todas decidi quedarme con estas

JAVA
Código:
public class Steganography {

    public Steganography() {
    }


    public boolean encode(String path, String original, String ext1, String stegan, String message) {
        String file_name = image_path(path, original, ext1);
        BufferedImage image_orig = getImage(file_name);

       
        BufferedImage image = user_space(image_orig);
        image = add_text(image, message);

        return (setImage(image, new File(image_path(path, stegan, "png")), "png"));
    }


    public String decode(String path, String name) {
        byte[] decode;
        try {
            //user space is necessary for decrypting
            BufferedImage image = user_space(getImage(image_path(path, name, "png")));
            decode = decode_text(get_byte_data(image));
            return (new String(decode));
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null,
                    "There is no hidden message in this image!", "Error",
                    JOptionPane.ERROR_MESSAGE);
            return "";
        }
    }


    private String image_path(String path, String name, String ext) {
        return path + "/" + name + "." + ext;
    }


    private BufferedImage getImage(String f) {
        BufferedImage image = null;
        File file = new File(f);

        try {
            image = ImageIO.read(file);
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null,
                    "Image could not be read!", "Error", JOptionPane.ERROR_MESSAGE);
        }
        return image;
    }


    private boolean setImage(BufferedImage image, File file, String ext) {
        try {
            file.delete(); //delete resources used by the File
            ImageIO.write(image, ext, file);
            return true;
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null,
                    "File could not be saved!", "Error", JOptionPane.ERROR_MESSAGE);
            return false;
        }
    }


    private BufferedImage add_text(BufferedImage image, String text) {
        //convert all items to byte arrays: image, message, message length
        byte img[] = get_byte_data(image);
        byte msg[] = text.getBytes();
        byte len[] = bit_conversion(msg.length);
        try {
            encode_text(img, len, 0); //0 first positiong
            encode_text(img, msg, 32); //4 bytes of space for length: 4bytes*8bit = 32 bits
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null,
                    "Target File cannot hold message!", "Error", JOptionPane.ERROR_MESSAGE);
        }
        return image;
    }

  
    private BufferedImage user_space(BufferedImage image) {
      
        BufferedImage new_img = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_3BYTE_BGR);
        Graphics2D graphics = new_img.createGraphics();
        graphics.drawRenderedImage(image, null);
        graphics.dispose(); //release all allocated memory for this image
        return new_img;
    }


    private byte[] get_byte_data(BufferedImage image) {
        WritableRaster raster = image.getRaster();
        DataBufferByte buffer = (DataBufferByte) raster.getDataBuffer();
        return buffer.getData();
    }


    private byte[] bit_conversion(int i) {
        //only using 4 bytes
        byte byte3 = (byte) ((i & 0xFF000000) >>> 24); //0
        byte byte2 = (byte) ((i & 0x00FF0000) >>> 16); //0
        byte byte1 = (byte) ((i & 0x0000FF00) >>> 8); //0
        byte byte0 = (byte) ((i & 0x000000FF));
        //{0,0,0,byte0} is equivalent, since all shifts >=8 will be 0
        return (new byte[]{byte3, byte2, byte1, byte0});
    }


    private byte[] encode_text(byte[] image, byte[] addition, int offset) {
        //check that the data + offset will fit in the image
        if (addition.length + offset > image.length) {
            throw new IllegalArgumentException("File not long enough!");
        }
        //loop through each addition byte
        for (int i = 0; i < addition.length; ++i) {
            //loop through the 8 bits of each byte
            int add = addition[i];
            for (int bit = 7; bit >= 0; --bit, ++offset) //ensure the new offset value carries on through both loops
            {
               
                int b = (add >>> bit) & 1;
               
                image[offset] = (byte) ((image[offset] & 0xFE) | b);
            }
        }
        return image;
    }


    private byte[] decode_text(byte[] image) {
        int length = 0;
        int offset = 32;
        
        for (int i = 0; i < 32; ++i) //i=24 will also work, as only the 4th byte contains real data
        {
            length = (length << 1) | (image[i] & 1);
        }

        byte[] result = new byte[length];

        
     
        for (int b = 0; b < result.length; ++b) {
         
            for (int i = 0; i < 8; ++i, ++offset) {
              
                result[b] = (byte) ((result[b] << 1) | (image[offset] & 1));

            }
        }
        return result;
    }
    
    
       public static void main(String[] args) {
       Steganography st = new Steganography();
        st.encode("C:\\java\\netbeans\\steganografia\\","kk","png","aaa","la policia esta loca loca");
        
        String salida=st.decode("C:\\java\\netbeans\\steganografia\\", "aaa");
        System.out.println(salida);
    }
}
PHP
Código PHP:
<?php
function encode(){
$img=imagecreatefromjpeg("kk.jpg");
$message='Hello World'.chr(0);
$x=imagesx($img);
$y=imagesy($img);
$px=0;
$py=23;
$h=23;
for(
$i=0;$i<strlen($message);$i++) {
    
$px+=$h;
    if(
$px>$x) {
        
$px=$px%$x;
        
$py+=$h;
    }
    
$rgb=imagecolorat($img,$px,$py);
    
$R=($rgb >> 16) & 0xFF;
    
$G=($rgb >> 8) & 0xFF;
    
$B=$rgb 0xFF;

    
$m=ord($message{$i});
    
$R=($R&0xf8)| ($m&0x07);
    
$G=($B&0xf8)|(($m>>3)&0x07);
    
$B=($B&0xf8)| (($m>>6)&0x03);

    
$t=imagecolorallocate($img,$R,$G,$B);
    
imagesetpixel($img ,$px,$py,$t);
}
imagepng($img,'deadsea2.png');
imagedestroy($img);
}

function 
decode(){
$img imagecreatefrompng("deadsea2.png");
$message='';
$x=imagesx($img);
$y=imagesy($img);
$px=0;
$py=23;
$h 23;
while( 
TRUE ){
    
$px += $h;
    if( 
$px $x ) {
        
$px $px%$x;
        
$py += $h;
    }
    
$rgb imagecolorat($img$px$py);
    
$R = ($rgb >> 16) & 0x7;
    
$G = ($rgb >> 8) & 0x7;
    
$B $rgb 0x3;
    
$m $R + ($G<<3) + ($B<<6);
    
$g=dechex($G<<3);
    
$b=dechex($B<<6);

    if( 
$m==) break;
    
$message .= chr($m);
}
return 
$message;
}
encode();
echo 
decode();
?>
Estos son los codigos tal cual los baje. he modificado mucho los dos.. pero sin resultados porsitivos. por lo cual decidi postear estos. como dije antes no entiendo mucho de esto pero sospecho que la cosa debe andar por la escritura en 0xFE y 0xFF .. lo cual probe pero nada, tambien probe cabiar los parametros $px y $py de 32 a 23 para ponerlos en los dos lenguajes.. tambien veo que puede hacer alguna cosa en que uno trabaja con imagenes 24 bites y el otro con 16.. tambien trate de acomodarlo.. pero no tuve suerte..

lo que pido no es que me lo hagan.. (para que nadie se enoje..) pero si alguien pudiera guiarme para encontrar el kid de la cuestion .. estaria muy agradecido.