隐藏JPG图像中的消息

时间:2013-05-20 06:07:04

标签: java image jpeg steganography

我试图隐藏一个图像,它与.bmp& .png但是当我将图像写为JPG并尝试检索隐藏的消息时,它无法正常工作。 我的程序,首先以格式(bmpgifjpgpng读取图像)写入消息以隐藏并保存它,以便我们可以再次阅读并提取消息。 当我使用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

2 个答案:

答案 0 :(得分:3)

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

我不喜欢DCT而且我没有深入研究过,但here是2007年的一篇论文,声称jpeg无损隐写术。请注意,该算法比空间域中的偶然LSB替换要复杂得多。隐藏频域中的数据也意味着更低的隐藏容量,我不知道这是否会为您服务。如果您感兴趣并且无法访问该论文,我们可以私下对其进行排序。

答案 1 :(得分:2)

您可能需要更改JPEG以获得100%的编码质量 - 这将显着增加字节大小(很多)。

有关如何使用可控压缩/质量编码为JPG的信息,请参阅this thread。左侧的滑块用于控制级别。

相关问题