Ver Mensaje Individual
  #2 (permalink)  
Antiguo 13/12/2014, 13:40
Avatar de NSD
NSD
Colaborador
 
Fecha de Ingreso: mayo-2012
Ubicación: Somewhere
Mensajes: 1.332
Antigüedad: 12 años
Puntos: 320
Respuesta: Steganografia de php a java

Lo que debes hacer que ambos scripts hagan lo mismo.

Al parecer recolectaste scripts de internet y pretendes que funcionen pero ambos son diferentes.

Te pongo los metodos encode de php y decode de java funcionando, te toca a ti escribir el encode en java y el decode en php, aunque simplemente tienes que transcribir y hacer cambios mínimos de sintaxis.

Código PHP:
Ver original
  1. <?php
  2.     class S12y
  3.     {
  4.         private $source = "";
  5.         private $target = "";
  6.         private $img = null;
  7.         private $size = [];
  8.  
  9.         public function __construct($source, $target = "")
  10.         {
  11.             $this->source = $source;
  12.             $this->target = $target;
  13.         $this->img = imagecreatefrompng($this->source);
  14.             $this->size = getimagesize($this->source);
  15.         }
  16.  
  17.         public function encode($message)
  18.         {
  19.             $message = str_split($message);
  20.             for($pxy=2; $pxy<$this->size[1]; $pxy+=2)
  21.             {
  22.                 for($pxx=1; $pxx<$this->size[0]; $pxx++)
  23.                 {                
  24.                     extract(imagecolorsforindex($this->img, imagecolorat($this->img, $pxx, $pxy-1)));
  25.                    
  26.                     if($message)
  27.                     {
  28.                         $ord = str_split(str_pad(ord(array_shift($message)), 3, "0", STR_PAD_LEFT));
  29.                        
  30.                         $red += $ord[0]; if($red > 255) $red -= 255;
  31.                         $green += $ord[1]; if($green > 255) $green -= 255;
  32.                         $blue += $ord[2]; if($blue > 255) $blue -= 255;
  33.                        
  34.                         imagesetpixel($this->img, $pxx, $pxy, imagecolorallocate($this->img, $red, $green, $blue));
  35.                     }
  36.                     else
  37.                     {
  38.                         imagesetpixel($this->img, $pxx, $pxy, imagecolorallocate($this->img, $red, $green, $blue));
  39.                         break 2;
  40.                     }
  41.                 }
  42.             }
  43.  
  44.             imagepng($this->img, ($this->target ? $this->target : $this->source));
  45.         }
  46.  
  47.         public function decode()
  48.         {
  49.             // TO-DO
  50.             $message = "";
  51.             return $message;
  52.         }
  53.     }
  54.  
  55.     $s12y = new S12y("imgen.png", "imagen-con-mensaje.png");
  56.     $s12y->encode("Hola!, mi nombre es NSD.");

Código Java:
Ver original
  1. package steganography;
  2.  
  3. import java.awt.Color;
  4. import java.awt.image.BufferedImage;
  5. import java.io.File;
  6. import java.io.IOException;
  7. import javax.imageio.ImageIO;
  8.  
  9. public class S12y {
  10.    
  11.     private BufferedImage img;
  12.     private String source = "";
  13.     private String target = "";
  14.     private int[] size;
  15.    
  16.     public S12y(String source)
  17.     {
  18.         this(source, "");
  19.     }
  20.    
  21.     public S12y(String source, String target)
  22.     {
  23.         this.source = source;
  24.         this.target = target;
  25.         try {
  26.             this.img = ImageIO.read(new File(this.source));
  27.         } catch (IOException e) {
  28.             e.printStackTrace();
  29.         }  
  30.         this.size = new int[2];
  31.         this.size[0] = this.img.getWidth();
  32.         this.size[1] = this.img.getHeight();
  33.     }
  34.    
  35.     public void encode()
  36.     {      
  37.         // TO-DO
  38.     }
  39.    
  40.     public String decode()
  41.     {
  42.         String message = "";
  43.        
  44.         outerloop:
  45.         for(int pxy=2; pxy<this.size[1]; pxy+=2)
  46.         {
  47.             for(int pxx=1; pxx<this.size[0]; pxx++)
  48.             {
  49.                 int old_rgb = this.img.getRGB(pxx, pxy-1);
  50.                 int new_rgb = this.img.getRGB(pxx, pxy);
  51.  
  52.                 if(old_rgb != new_rgb)
  53.                 {
  54.                     Color oldc = new Color(old_rgb);
  55.                     int old_red = oldc.getRed();
  56.                     int old_green = oldc.getGreen();
  57.                     int old_blue = oldc.getBlue();
  58.                     Color newc = new Color(new_rgb);
  59.                     int new_red = newc.getRed();
  60.                     int new_green = newc.getGreen();
  61.                     int new_blue = newc.getBlue();
  62.                                        
  63.                     String ord = (old_red <= new_red ? new_red - old_red : new_red + 255 - old_red)+"";
  64.                     ord += (old_green <= new_green ? new_green - old_green : new_green + 255 - old_green);
  65.                     ord += (old_blue <= new_blue ? new_blue - old_blue : new_blue + 255 - old_blue);
  66.                    
  67.                     message += Character.toString((char) Integer.parseInt(ord));
  68.                 }
  69.                 else
  70.                     break outerloop;
  71.             }
  72.         }
  73.  
  74.         return message;
  75.     }
  76.  
  77.     public static void main(String[] args) {
  78.         S12y img = new S12y("[path-to]/imagen-con-mensaje.png");
  79.  
  80.         System.out.println(img.decode());  
  81.     }
  82. }

La tecnica que se hace uso aca es muy simple, para cada letra se obtiene su valor ascii y luego los 3 digitos de dicho valor.
Luego, se recorre la imagen leyendo de a dos filas de pixeles, en la primera, se guarda el color de la segunda sumandole a cada canal de RGB un digito del valor ascii de la letra.

Cuando se acaba el mensaje, osea que se grabaron todas las letras, se guarda un pixel repetido en ambas lineas.

La funcion que lo decodifica hace lo mismo, va leyendo de a dos lineas y comparando los valores, rearma el codigo ascii y obtiene el caracter.

Una imagen puede contener ancho*alto/2 caracteres encubiertos.
__________________
Maratón de desafíos PHP Junio - Agosto 2015 en FDW | Reglamento - Desafios