信息安全之隐写图片

  

隐写图片

我们用记事本打开或者用notepad++打开一张jpg图片,然后在最后写下一段文字,然后发送给别人,别人也用记事本拉到最后 打开才能看到。

这是因为在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 }

测试运行

 

 

posted @ 2016-04-18 20:50  CodeNoob  阅读(1122)  评论(0编辑  收藏  举报