信息安全之隱寫圖片

  

隱寫圖片

咱們用記事本打開或者用notepad++打開一張jpg圖片,而後在最後寫下一段文字,而後發送給別人,別人也用記事本拉到最後 打開才能看到。java

這是由於在jpg中,是有結束符的,16進制是FF D9,利用UE編輯器 能夠看到正常的jpg結尾都是FF D9的,圖片查看器會忽視jpg結束符以後的內容,因此咱們附加的內容,天然也就不會影響到圖像的正常顯示。數組

 

 

咱們找一張圖片(這表情就是原圖)在裏面寫入「這裏是密文」,而後保存編輯器

 

最後咱們寫一個程序來給出圖片地址檢查出添加的密文測試

 1 package gh;
 2 
 3 import java.io.BufferedInputStream;
 4 import java.io.ByteArrayOutputStream;
 5 import java.io.File;
 6 import java.io.FileInputStream;
 7 import java.io.FileWriter;
 8 import java.io.IOException;
 9 import java.io.PrintWriter;
10 /**
11  * 檢查隱寫圖片
12  * @author ganhang
13  *
14  */
15 
16 public class Steganographic {
17 
18     public static void main(String[] args) throws Exception {
19         try {
20             StringBuffer sb = new StringBuffer();
21             FileInputStream fis = new FileInputStream("1.jpg");
22             BufferedInputStream bis = new BufferedInputStream(fis);
23             ByteArrayOutputStream bos = new ByteArrayOutputStream();
24             byte[] buff = new byte[102400];
25             int len = 0;
26             while ((len = fis.read(buff)) != -1) {
27                 bos.write(buff, 0, len);
28             }
29             // 獲得圖片的字節數組
30             byte[] result = bos.toByteArray();
31             //System.out.println("++++" + byte2HexStr(result));
32             // 字節數組轉成十六進制
33             String str = byte2HexStr(result);
34             /*
35              * 找出FFD9的位置
36              */
37             char []c=str.toCharArray();
38             int j=0;
39             for(int i=0;i<c.length;i++){
40                 if(c[i]=='F'&&c[i+1]=='F'&&c[i+2]=='D'&&c[i+3]=='9'){
41                     j=i+4;
42                     break;
43                 }
44             }
45             //截取FFD9後面的十六進制,而後轉中文
46             str=str.substring(j);
47             System.out.println(hexToStringGBK(str));
48         } catch (IOException e) {
49             e.printStackTrace();
50         }
51     }
52 
53     /*
54      * 實現字節數組向十六進制的轉換方法一
55      */
56     public static String byte2HexStr(byte[] b) {
57         String hs = "";
58         String stmp = "";
59         for (int n = 0; n < b.length; n++) {
60             stmp = (Integer.toHexString(b[n] & 0XFF));
61             if (stmp.length() == 1)
62                 hs = hs + "0" + stmp;
63             else
64                 hs = hs + stmp;
65         }
66         return hs.toUpperCase();
67     }
68 
69     public static String hexToStringGBK(String s) {
70         byte[] baKeyword = new byte[s.length() / 2];
71         for (int i = 0; i < baKeyword.length; i++) {
72             try {
73                 baKeyword[i] = (byte) (0xff & Integer.parseInt(
74                         s.substring(i * 2, i * 2 + 2), 16));
75             } catch (Exception e) {
76                 e.printStackTrace();
77                 return "";
78             }
79         }
80         try {
81             s = new String(baKeyword, "GBK");// UTF-16le:Not
82         } catch (Exception e1) {
83             e1.printStackTrace();
84             return "";
85         }
86         return s;
87     }
88 }

測試運行spa

 

相關文章
相關標籤/搜索