A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

© aisini 金牌黑马   /  2014-8-18 18:11  /  1251 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. public class LSBSteganography
  2. {
  3.     private String imgUrl;
  4.     private String password;
  5.     private byte[] infoText;
  6.     private byte[] imgByte;
  7.     private const int CB = 55;     //CheckBegin 隐写检测的起始下标
  8.     private const int LB = 103;    //LengthBegin 隐写文本长度的起始下标
  9.     private const int TB = 135;    //TextBegin 隐写正文的起始下标

  10.     public LSBSteganography(String imgUrl, String infoText, String password)
  11.     {
  12.         this.imgUrl = imgUrl;   //获取图片URL
  13.         this.password = (password + "tmppwd").Substring(0, 6);  //设置密码不足补齐6位,多的只取6位
  14.         this.infoText = Encoding.Default.GetBytes(infoText);    //获取隐写文本
  15.         FileStream tFS = new FileStream(imgUrl, FileMode.Open, FileAccess.ReadWrite);
  16.         this.imgByte = new byte[tFS.Length];
  17.         tFS.Read(imgByte, 0, Convert.ToInt32(tFS.Length));  //把图片信息存入imgByte
  18.         tFS.Close();
  19.     }

  20.     public String GetImgUrl()
  21.     {
  22.         return imgUrl;  //返回图片URL
  23.     }

  24.     public String GetInfoText()
  25.     {
  26.         return Encoding.Default.GetString(infoText);    //返回隐写信息 用户在提取信息后显示信息
  27.     }

  28.     public String LSBWrite()
  29.     {
  30.         try
  31.         {
  32.             //TB开始是真正开始隐写数据 一字节数据需要8字节图片信息来隐写
  33.             if (imgByte.Length < infoText.Length * 8 + TB) return "写入信息失败.\n图片过小无法存入全部信息";
  34.             this.SetCheckLSB(); //写入隐写标记 此处既是密码
  35.             this.SetLength();   //写入隐写长度
  36.             this.WriteInfo();   //写入文本信息
  37.             File.WriteAllBytes(imgUrl, imgByte);

  38.             return "写入信息成功.";
  39.         }
  40.         catch(Exception ex)
  41.         {
  42.             return "写入信息失败.\n" + ex.Message;
  43.         }
  44.     }

  45.     public String LSBRead()
  46.     {
  47.         try
  48.         {
  49.             //检查输入的密码是否正确
  50.             if (!this.CheckLSB()) return "读取信息失败.\n密码错误,或者图片不含隐藏信息.";
  51.             this.ReadInfo();    //读取信息到infoText
  52.             return "读取信息成功.";
  53.         }
  54.         catch(Exception ex)
  55.         {
  56.             return "读取信息失败.\n" + ex.Message;
  57.         }
  58.     }

  59.     public String LSBClear()
  60.     {
  61.         try
  62.         {
  63.             this.ClearInfo();   //擦除信息 此处方法为无脑全部置0
  64.             File.WriteAllBytes(imgUrl, imgByte);
  65.             return "擦除信息成功.";
  66.         }
  67.         catch (Exception ex)
  68.         {
  69.             return "擦除信息失败.\n" + ex.Message;
  70.         }
  71.     }

  72.     private void SetCheckLSB()  //隐写密码,用于检测是否有隐写信息
  73.     {
  74.         byte[] checkStr = Encoding.Default.GetBytes(password);
  75.         for (int i = 0; i < 6; i++) //6个字符
  76.         {
  77.             for (int j = 0; j < 8; j++) //每个字符8位
  78.             {
  79.                 imgByte[i * 8 + j + CB] &= ((1 << 8) - 2);
  80.                 if ((checkStr[i] & (1 << j)) != 0)
  81.                     imgByte[i * 8 + j + CB]++;
  82.             }
  83.         }
  84.     }

  85.     private bool CheckLSB() //读取隐写密码,于用户输入密码匹配
  86.     {
  87.         byte[] checkStr = new byte[6];
  88.         for (int i = 0; i < 6; i++) checkStr[i] = 0;

  89.         for (int i = 0; i < 6; i++) //6个字符
  90.         {
  91.             for (int j = 0; j < 8; j++) //每个字符8位
  92.             {
  93.                 if ((imgByte[i * 8 + j + CB] & 1) != 0)
  94.                     checkStr[i] |= Convert.ToByte(1 << j);
  95.             }
  96.         }

  97.         return Encoding.Default.GetString(checkStr) == password;
  98.     }

  99.     private int GetLength() //获取隐写文本长度
  100.     {
  101.         int length = 0;
  102.         for (int i = 0; i < 32; i++)    //一个int 32位
  103.         {
  104.             if ((imgByte[i + LB] & 1) != 0)
  105.                 length |= Convert.ToByte(1 << i);
  106.         }

  107.         return length;
  108.     }

  109.     private void SetLength() //写入隐写文本长度
  110.     {
  111.         int length = this.infoText.Length;
  112.         for (int i = 0; i < 32; i++)    //一个int 32位
  113.         {
  114.             imgByte[i + LB] &= ((1 << 8) - 2);  //末尾置0
  115.             if ((length & (1 << i)) != 0)
  116.                 imgByte[i + LB]++;  //如果不是0 改为1
  117.         }
  118.     }

  119.     private void WriteInfo()
  120.     {
  121.         for (int i = 0; i < infoText.Length; i++)
  122.         {
  123.             for (int j = 0; j < 8; j++) //每个字符8位
  124.             {
  125.                 imgByte[i * 8 + j + TB] &= ((1 << 8) - 2);  //末尾置0
  126.                 if ((infoText[i] & (1 << j)) != 0)
  127.                     imgByte[i * 8 + j + TB]++;  //如果不是0 改为1
  128.             }
  129.         }
  130.     }

  131.     private void ReadInfo()
  132.     {
  133.         int length = this.GetLength();
  134.         infoText = new byte[length];
  135.         for (int i = 0; i < length; i++)
  136.         {
  137.             for (int j = 0; j < 8; j++) //每个字符8位
  138.             {
  139.                 if ((imgByte[i * 8 + j + TB] & 1) != 0)
  140.                     infoText[i] |= Convert.ToByte(1 << j);
  141.             }
  142.         }
  143.     }

  144.     private void ClearInfo()
  145.     {
  146.         int length = imgByte.Length;
  147.         for (int i = CB; i < length; i++)
  148.         {
  149.             imgByte[i] &= ((1 << 8) - 2);  //末尾全部置0
  150.         }
  151.     }
  152. }
复制代码


0 个回复

您需要登录后才可以回帖 登录 | 加入黑马