java - 在 JPG 图像中隐藏消息

标签 java image jpeg steganography

我试图在图像中隐藏 a 它与 .bmp 和 .png 一起工作正常,但是当我将图像写为 JPG 并尝试检索隐藏的消息时它不起作用。 我的程序,首先读取格式为 (bmp, gif, jpg, png) 的图像写入消息以隐藏并保存它,以便我们可以再次阅读它并提取消息。 当我用 bmppng 保存它时它工作正常但是当用 jpg 保存并尝试提取消息时它不起作用。

 ImageIO.write(bimg, "png", outputfile);//working
 ImageIO.write(bimg, "png", outputfile);//not working 

我应该怎么做才能使其适用于 JPEG?

注意:我将每个像素读取为具有 ARGB 值的 4 位整数,并更改 R、G、B 的 LSB 以隐藏消息。

    public void stegnography(BufferedImage bimg,String msg,String filename)
    {

      int w=bimg.getWidth();
      int h=bimg.getHeight();
     //*************************************** 
     // String msg="Hide this message:)";
      System.out.println("message="+msg+" length="+msg.length());
    //*************************************** 

      if(msg.length()>255 )
      {
         jLabel3.setText("MESSAGE IS LARGE THAN 255 CHARACTERS");            
      }
      else if( msg.length()*11 >w*h)
      {
         jLabel3.setText("Image is too small");    
      }
      else{

  //-------------------------------------------
           byte[] msgbytes= msg.getBytes();

        int msglendecode= (bimg.getRGB(0,0)>>8)<<8;

         msglendecode |= msg.length();
        bimg.setRGB(0, 0,msglendecode );//hidig msg length at first position

         //System.out.println("\npixel at position (0,0) ");
         // bitpattern(bimg.getRGB(0,0) );

         for(int i=1,msgpos=0,row=0,j=0;   row<h   ;row++  )
      {
          for(int col=0;col<w && j<msgbytes.length ;col++,i++ )
          {      

               if (i%11==0) {

                    int rgb = bimg.getRGB(col,row);


                    int a=((rgb >> 24) & 0xff);

                    int r = (((rgb >> 16) & 0xff)>>3)<<3;
                    r=r|(msgbytes[msgpos]>>5);

                    int g = (((rgb >> 8) & 0xff)>>3)<<3;
                    g=g|((msgbytes[msgpos]>>2)& 7);

                     int b = ((rgb & 0xff)>>2)<<2;
                    b=b|(msgbytes[msgpos]&0x3);


                    rgb=0;
                    rgb=(rgb|(a<<24));
                    rgb=(rgb|(r<<16));
                    rgb=(rgb|(g<<8));

                    rgb=(rgb|b);

                    bimg.setRGB(col,row,rgb);

                    msgpos++;
                    j++;

                  //bitpattern(bimg.getRGB(col,row));

              }


          }//for 2
      }//for 1


      ImageIcon image = new ImageIcon(bimg);
           jLabel3.setIcon(image);  

     try {

  //  File outputfile = new File("c:/Users/yathestha/Documents/"+filename);
     File outputfile = new File("c:/Users/yathestha/Documents/outpng.png");
    ImageIO.write(bimg, "png", outputfile);
} catch (IOException e) {
         System.out.println("error in saving image ");
}

  //-------------------------------------------------
      }//else
 // decoding part----------------------------------------------------------------------   

    }
///////////////////////////////////////////////////////////////////////
private void decodestegnography(BufferedImage bimg) {

     System.out.println("in decode");

   int w=bimg.getWidth(),h=bimg.getHeight();
    bitpattern(bimg.getRGB(0, 0));
    int msglength=(bimg.getRGB(0, 0)&0xff);
    bitpattern(msglength);
    System.out.println("Message Length="+msglength);

    jTextField1.setText("");
      for(int row=0,j=0,i=1;   row<h   ;row++  )
  { 
      for(int col=0;col<w && j<msglength ;col++ ,i++)
      {

          if (i%11==0) {
             int result=bimg.getRGB(col,row);


              int charatpos = (((result >> 16) & 0x7) << 5);

              charatpos |=  (((result >> 8) & 0x7) << 2);

              charatpos |=  ((result & 0x3));

              jTextField1.setText(jTextField1.getText()+ (char)charatpos);

             j++;
          }
      }
  } 

     System.out.println("decoding done");
}//function

最佳答案

对于 jpeg 隐写术,要么将结果保存为无损 jpeg,要么简单地使用不同的隐写方法。我唯一知道的是摆弄离散余弦变换系数(DCT)。但是,您需要注意舍入错误,因此对您的 secret 的检索将是有损的。

我不喜欢 DCT,也没有深入研究它,但是 here是 2007 年的一篇论文,声称 jpeg 无损隐写术。请注意,该算法比您在空间域中随意替换 LSB 复杂得多。在频域中隐藏数据也意味着较低的隐藏能力,我不知道这是否对您有用。如果您有兴趣但无法访问该论文,我们可以私下解决这个问题。

关于java - 在 JPG 图像中隐藏消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16643495/

相关文章:

java - Android:将 AppWidgetId 传递给服务

image - 如何在 MATLAB 中从视频中选择特定帧?

html - 无法更改容器时图像高度到完整的 div 高度

java - 我无法获得 ImageReaderByFormatName ("jpeg-lossless")

image - 如何在 jpg 或 gif 照片中隐藏信息?

java - 使用嵌套在 JAVA android 中格式化 HTTP Post 请求参数

java - 我可以在 Java EE 项目中使用 Dagger 2 吗?

java - 如何检查标签是否不为空?

image - 在 HTML/CSS 中将图像转换为灰度

c++ - 如何知道 jpeg 图像的压缩率?