PHP架构程序员架构相关

LSB算法 - 隐藏数据到图片中

2017-12-19  本文已影响125人  大道至简_Andy

LSB算法概述

应用场景

代码在GitHub上

https://github.com/jingxuan1990/ImageLSB

代码分析

隐藏文本到图片

// data数据; imagePath: 图片路径,格式为png或jpg
public static boolean toImg(String data, String imagePath) {
    File imageFile = new File(imagePath);
    InputStream inputStream = null;
    try {
      inputStream = new FileInputStream(imageFile);
      
      // image转成buffer image对象
      BufferedImage image = Utils.streamToImage(inputStream);
      
      // 获取image的长度,也就是像素点的多少
      final int imageLength = image.getHeight() * image.getWidth();
      // 隐藏内容偏移的位置
      final int startingOffset = Utils.calculateStartingOffset(null, imageLength);
    
      Steganography steganography = new Steganography();
      // 开始隐藏
      steganography.encode(image, data, startingOffset);
      return ImageIO.write(image, ImageUtils.getFileExt(imageFile), imageFile);
    } catch (FileNotFoundException e) {
      throw new RuntimeException("Couldn't find file " + imagePath, e);
    } catch (IOException | SteganographyException e) {
      throw new RuntimeException(e);
    } finally {
      if (inputStream != null) {
        try {
          inputStream.close();
        } catch (IOException ignored) {
        }
      }
    }
}

Steganography类的encode方式是核心方法。

public BufferedImage encode(final BufferedImage bufferedImage, final String message, final int offset) throws SteganographyException {
    // 调用addText方法
    return this.addText(bufferedImage, message, offset);
  }
private BufferedImage addText(BufferedImage bufferedImage, final String text, final int offset)
      throws SteganographyException {
    // 获取隐藏内容的字节数组
    final byte msg[] = text.getBytes();
    // 计算length的字节数组(4个Byte表示)
    final byte len[] = bitConversion(msg.length);
    try {
      // 隐藏文本的核心方法
      // 1. 先隐藏文本的长度
      bufferedImage = this.encodeText(bufferedImage, len, offset);
      // 2. 隐藏文本的实际内容
      bufferedImage = this.encodeText(bufferedImage, msg, offset + Constants.HIDDEN_MESSAGE_BIT_LENGTH);
    } catch (Exception e) {
      throw new SteganographyException(MessageConstants.ENCODING_ERROR_GENERAL, e);
    }

    return bufferedImage;
  }
private BufferedImage encodeText(final BufferedImage bufferedImage, final byte[] addition,final int offset) throws SteganographyException {
    // 获取图片的宽和高
    final int height = bufferedImage.getHeight();
    final int width = bufferedImage.getWidth();
    
    // 初始化迭代的变量
    int i = offset / height;
    int j = offset % height;
    
    // 判断隐藏内容和图片可以隐藏内容的大小
    if ((width * height) >= (addition.length * 8 + offset)) {
    
      // 遍历隐藏内容的字节数组, additon[]就是要隐藏的内容的字节
      for (final byte add : addition) {
      
        // 遍历当前byte的每一比特位
        for (int bit = 7; bit >= 0; --bit) {
          // 获得像素点(i,j)的R/G/B的值(0-255, 8比特位)
          final int imageValue = bufferedImage.getRGB(i, j);
    
          // 获取add的字节的最低位
          /** 如:
          *  add    1111 1111
          *  bit=7  0000 0001 (无符号左右7位)
          *    1    0000 0001
          *    &    0000 0001
          *
          *  add    1111 1111
          *  bit=6  0000 0011 (无符号左右6位)
          *    1    0000 0001
          *    &    0000 0001
          * 
          *  整个循环从左到右取add的每一位
          */
          int b = (add >>> bit) & 1; // 与1&取最低位,并保证最低位为0或1
          
          // imageValue & 0xFFFFFFFE -> 确保imageValue的最后一位为0
          // 然后或|运算,用b代替imageValue最后一位。
          final int imageNewValue = ((imageValue & 0xFFFFFFFE) | b);
    
          // 把替换后的imageValue重新设置到原来的点
          bufferedImage.setRGB(i, j, imageNewValue);
    
          // 确保高度(j)不越界
          if (j < (height - 1)) {
            ++j;
          } else {
            // 高度(y坐标)遍历完后,移动x(j)坐标。
            ++i;
            j = 0;
          }
        }
    
      }
    } else {
      throw new SteganographyException(MessageConstants.ENCODING_ERROR_BIG_MESSAGE);
    }
return bufferedImage;
}

//备注: 图片是由左下角开始遍历的

提取图片中的文本

public static String fromImg(String imagePath) {
    File imageFile = new File(imagePath);
    InputStream inputStream = null;
    
    try {
      inputStream = new FileInputStream(imageFile);
      BufferedImage image = Utils.streamToImage(inputStream);
    
      final int imageLength = image.getWidth() * image.getHeight();
      final int startingOffset = Utils.calculateStartingOffset(null, imageLength);
    
      Steganography steganography = new Steganography();
      // 提取方法
      return steganography.decode(image, startingOffset);
    } catch (FileNotFoundException e) {
      throw new RuntimeException("Couldn't find file " + imagePath, e);
    } catch (IOException | SteganographyException e) {
      throw new RuntimeException(e);
    } finally {
      if (inputStream != null) {
        try {
          inputStream.close();
        } catch (IOException ignored) {
        }
      }
    }
}
public String decode(final BufferedImage bufferedImage, final int startingOffset)throws SteganographyException {
    byte[] decode;
    try {
      // 调用decodeText解密
      decode = this.decodeText(bufferedImage, startingOffset);
      return new String(decode);
    } catch (Exception e) {
      throw new SteganographyException(MessageConstants.DECODING_ERROR_GENERAL);
    }
}
  private byte[] decodeText(final BufferedImage bufferedImage, final int startingOffset) {
    // 初始化变量
    final int height = bufferedImage.getHeight();
    final int offset = startingOffset + Constants.HIDDEN_MESSAGE_BIT_LENGTH;
    int length = 0;

    // 提取文本内容的长度,32bit,固定32个比特
    for (int i = startingOffset; i < offset; ++i) {
      final int h = i / height;
      final int w = i % height;

      final int imageValue = bufferedImage.getRGB(h, w);
      // 从bit中还原int(文本的字节数组长度)
      // 1. (imageValue & 1)取imageValue的最低一比特位
      // 2. length << 1 左移
      // 3. (length << 1) | (imageValue & 1) -> 把取出的每个bit通过|操作,累加到length
      length = (length << 1) | (imageValue & 1);
    }

    // 初始化字节数组,存放提取结果
    byte[] result = new byte[length];

    // 初始化迭代变量
    int i = offset / height;
    int j = offset % height;

    // 遍历数据的所有字节
    for (int letter = 0; letter < length; ++letter) {
      // 遍历隐藏数据的每一位,取出放到当前byte中
      for (int bit = 7; bit >= 0; --bit) {
      
        // 获取像素点(i,j)的R/G/B的值(0~255, 8比特位)
        final int imageValue = bufferedImage.getRGB(i, j);

        // (imageValue & 1) -> 取出imageValue的最低位
        // (result[letter] << 1) -> 左移一位
        // (result[letter] << 1) | (imageValue & 1) -> 取出imagevalue的最低位,放到byte的最低位上
        // 循环8次,还原成一个字节Byte
        result[letter] = (byte) ((result[letter] << 1) | (imageValue & 1));

        if (j < (height - 1)) {
          ++j;
        } else {
          ++i;
          j = 0;
        }
      }
    }

    return result;
  }

上一篇下一篇

猜你喜欢

热点阅读