2015-11-07 58 views
0

我正在做圖像隱寫,如果我輸入消息大於3個字符加密有一個例外,量化表0x01沒有定義,並且是消息小於3字符我得到了一個加密的圖像,因爲我需要.I認爲這是由於JPEG格式(我認爲當在圖像字節數組中注入比特時,我摧毀了圖像的屬性和屬性)。幫助我確信它與元數據有關但是對此不太瞭解。圖像加密

我加入的代碼我在做什麼

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樣子

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; 
} 
+0

告訴我,如果我正確地理解這一點,因爲你的方法轉換爲base-64,並回到我困惑。你只是從圖像和你的消息的字節數組中獲得一個原始二進制的字節數組,並將它們傳遞給你的'encode'方法? – Reti43

回答

2

你是正確的,你已經干擾了文件結構。 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編碼器的源代碼,因爲寫一個是非常複雜的,需要對整個格式有複雜的理解。編碼器會將像素轉換爲一堆不同的數字(有損步驟)並將它們緊湊地存儲到一個文件中。然後應該在這兩個步驟之間注入您的隱寫算法,您可以在將這些數字保存到文件之前修改這些數字。