图像隐写术显示不同的输出

时间:2014-04-11 04:42:19

标签: java java-ee servlets steganography

我在java中使用图像速记制作一个Web应用程序。但是当我在桌面应用程序中使用编码和解码相同的算法时,我陷入了困境。我得到了不同的结果(正确)。但是当我在Web应用程序中使用相同的算法时,结果是错误的

对文本进行编码如下:

private static BufferedImage add_text(BufferedImage image, String text)
{
    //convert all items to byte arrays: image, message, message length
    byte img[]  = get_byte_data(image);

    byte msg[] = text.getBytes();
    byte len[]   = bit_conversion(msg.length);
    try
    {
        encode_text(img, len,  0); //0 first positiong
        encode_text(img, msg, 32); //4 bytes of space for length: 4bytes*8bit = 32 bits
    }
    catch(Exception e)
    {
        JOptionPane.showMessageDialog(null,"Target File cannot hold message!", "Error",JOptionPane.ERROR_MESSAGE);
    }
    return image;
}

它使用三个功能。

get_byte_data()如下:

private static byte[] get_byte_data(BufferedImage image)
{
    WritableRaster raster   = image.getRaster();
    DataBufferByte buffer = (DataBufferByte)raster.getDataBuffer();
    return buffer.getData();
}

使用的第二个函数是bit_Conversion.It如下:

private static byte[] bit_conversion(int i)
{
    byte byte3 = (byte)((i & 0xFF000000) >>> 24); //0
    byte byte2 = (byte)((i & 0x00FF0000) >>> 16); //0
    byte byte1 = (byte)((i & 0x0000FF00) >>> 8 ); //0
    byte byte0 = (byte)((i & 0x000000FF)       );
    return(new byte[]{byte3,byte2,byte1,byte0});
}

第3个,最后一个是encode_text,用于对图像中的文本进行编码

private static byte[] encode_text(byte[] image, byte[] addition, int offset)
{
    //check that the data + offset will fit in the image
    if(addition.length + offset > image.length)
    {
        throw new IllegalArgumentException("File not long enough!");
    }
    //loop through each addition byte
    for(int i=0; i<addition.length; ++i)
    {
        //loop through the 8 bits of each byte
        int add = addition[i];
        for(int bit=7; bit>=0; --bit, ++offset) //ensure the new offset value carries on through both loops
        {
            //assign an integer to b, shifted by bit spaces AND 1
            //a single bit of the current byte
            int b = (add >>> bit) & 1;
            //assign the bit by taking: [(previous byte value) AND 0xfe] OR bit to add
            //changes the last bit of the byte in the image to be the bit of addition
            image[offset] = (byte)((image[offset] & 0xFE) | b );
        }
    }
    return image;
}

解码:

public static String decode(String path, String name)
{
    byte[] decode;
    try
    {
        //user space is necessary for decrypting
        BufferedImage image  = user_space(getImage(image_path(path,name,"png")));
        decode = decode_text(get_byte_data(image));
        return(new String(decode));
    }
    catch(Exception e)
    {
        JOptionPane.showMessageDialog(null, 
            "There is no hidden message in this image!","Error",
            JOptionPane.ERROR_MESSAGE);
        return "";
    }
}

解码_text函数:

private static byte[] decode_text(byte[] image)
{
    int length = 0;
    int offset  = 32;
    //loop through 32 bytes of data to determine text length
    for(int i=0; i<32; ++i) //i=24 will also work, as only the 4th byte contains real data
    {
        length = (length << 1) | (image[i] & 1);
    }

    byte[] result = new byte[length];

    //loop through each byte of text
    for(int b=0; b<result.length; ++b )
    {
        //loop through each bit within a byte of text
        for(int i=0; i<8; ++i, ++offset)
        {
            //assign bit: [(new byte value) << 1] OR [(text byte) AND 1]
            result[b] = (byte)((result[b] << 1) | (image[offset] & 1));
        }
    }
    return result;
}

不同结果的原因是什么?请帮忙。也告诉相同的解决方案

0 个答案:

没有答案