首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >图像加密

图像加密
EN

Stack Overflow用户
提问于 2015-11-07 12:39:02
回答 1查看 686关注 0票数 0

我正在进行图像隐写,如果我输入大于3个字符的消息来加密,则有一个例外,即量化表0x01未定义,消息小于3个字符,我得到了一个加密的图像,因为我需要.I,因为我认为这是由于.Help格式(我认为在图像字节数组中注入比特时,hv破坏了图像的属性和属性).Help me,我确信它与元数据有关,但对它不太了解。

我正在添加代码我正在做的

代码语言:javascript
复制
 Creating_image()
 {
  File f=new File(file.getParent()+"/encrypt.jpg");
    if(file==null)
    {
        JOptionPane.showMessageDialog(rootPane, "file null ho gyi encrypt mein");
    }
      try{

          FileInputStream imageInFile = new FileInputStream(file);
        byte imageData[] = new byte[(int) file.length()];
        imageInFile.read(imageData);


        // Converting Image byte array into Base64 String
        String imageDataString =  Base64.encode(imageData);

        // Converting a Base64 String into Image byte array
        pixels = Base64.decode(imageDataString);

        // Write a image byte array into file system

        imageInFile.close();

        }
        catch(Exception as)
       {
   JOptionPane.showMessageDialog(rootPane,"Please first select an Image");
       }
   String msg=jTextArea1.getText(); 
   byte[] bmsg=msg.getBytes();
   String as=Base64.encode(bmsg);
  bmsg=Base64.decode(as);

    int len=msg.length();
   byte[] blen=inttobyte(len);
 String sd=Base64.encode(blen);
 blen=Base64.decode(sd);
 pixels=encode(pixels,blen,32);
 pixels=encode(pixels,bmsg,64);

 try{



         // Converting Image byte array into Base64 String
       String imageDataString =  Base64.encode(pixels);

        // Converting a Base64 String into Image byte array
        pixels = Base64.decode(imageDataString);
   InputStream baisData = new ByteArrayInputStream(pixels,0,pixels.length);
        image= ImageIO.read(baisData);
 if(image == null)
 {
     System.out.println("imag is empty");
 }
 ImageIO.write(image, "jpg", f);

 }
 catch(Exception s)
 {
    System.out.println(s.getMessage());
    }
 }

这就是编码fxn的样子。

代码语言:javascript
复制
byte[] encode(byte [] old,byte[] add,int offset)
{ 
try{    if(add.length+offset>old.length)
{
    JOptionPane.showMessageDialog(rootPane, "File too short");
}
}
catch(Exception d)
{
  JOptionPane.showMessageDialog(rootPane, d.getLocalizedMessage()); 
}
byte no;
    for(int i=0;i<add.length;i++)
    {
        no=add[i];
        for(int bit=7;bit>=0;bit--,++offset)
        {
           int  b=(no>>bit)&1;
            old[offset]=(byte)((old[offset]&0xfe)|b);
        }
    } 
    return old;
 }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-11-07 18:59:59

您是正确的,因为您已经扰乱了文件结构。JPEG格式包含高度压缩的数据,其字节中没有一个直接表示任何像素值。事实上,JPEG甚至不存储像素值,而是存储像素块的DCT系数。

读取文件的原始字节的方法只适用于像BMP这样的格式,其中像素直接存储在文件中。但是,您仍然必须跳过头几个字节(头),其中包含图像的宽度和高度、彩色平面的数量和每个像素的位数等信息。

如果您想通过修改最不重要的像素位来嵌入您的消息,您必须使用load the actual pixels in a byte array。然后,您可以使用encode()方法修改像素。要将数据保存到文件中,请使用convert the byte array to a BuffferedImage object并使用ImageIO.write()。但是,您必须使用不涉及有损压缩的格式,因为这会扭曲像素值,从而破坏您的消息。无损压缩(或未压缩)文件格式包括BMP和PNG,而JPEG是有损的。

如果你仍然想做JPEG隐写,这个过程会更复杂一些,但是this answer基本上涵盖了你需要做的事情。简单地说,您想借用jpeg编码器的源代码,因为编写它非常复杂,需要对整个格式进行复杂的理解。编码器将将像素转换为一组不同的数字(有损步骤),并将它们简洁地存储到文件中。然后,您的隐写算法应该在这两个步骤之间注入,在这个步骤中,您可以修改这些数字,然后将它们保存到文件中。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33582859

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档