I am trying to implement a steganography. I am reading an image "a.jpeg" and inserting a byte in it by changing its consecutive 7 bytes at the least significant bit starting from offset 50. This is done successfully as when I print the bytes the last bits are changed accordingly. Then I saved it as "ao.jpeg". But when I am reading the byte values from 50, they are not the same as the once i saved. here's my code
public static void main(String[] args) throws IOException {
BufferedImage inputImage = ImageIO.read(new File("a.jpeg"));
int offset=50;
byte data = 7;
byte[] image = get_byte_data(inputImage);//function converts bufferedimage to byte array
//add data at end of each byte starting from offset
System.out.println("bytes altered are :");
for(int bit=7; bit>=0; --bit, ++offset)//for each bit of data
{
int b = (data >>> bit) & 1;
image[offset] = (byte)((image[offset] & 0xFE) | b );
String s1 = String.format("%8s", Integer.toBinaryString(image[offset] & 0xFF)).replace(' ', '0');
System.out.println(s1);
}
//write changed image to ao.jpeg
BufferedImage outputImage = ImageIO.read(new ByteArrayInputStream(image));
File outputfile = new File("ao.jpeg");
ImageIO.write(outputImage,"jpeg",outputfile);
//read data from ao.jpeg
System.out.println("bytes from encoded image are :");
byte result=0;
offset=50;
BufferedImage oImage = ImageIO.read(new File("aoc.jpeg"));
byte[] image1 = get_byte_data(oImage);//function converts bufferedimage to byte array
for(int i=0; i<8; i++, ++offset)
{
result = (byte)((result << 1) | (image1[offset] & 1));
String s1 = String.format("%8s", Integer.toBinaryString(image1[offset] & 0xFF)).replace(' ', '0');
System.out.println(s1);
}
System.out.println("recovered data is :");
System.out.print(result);
}
output sample : data inserted is 7. If you notice the least significant bit of each byte it is forming 7. But when i read it again it is random bytes.
bytes altered are :
00010100
00011100
00011010
00011110
00011110
00011101
00011011
00011101
bytes from encoded image are :
00011110
00011101
00011010
00011100
00011100
00100000
00100100
00101110
recovered data is :
64
As suggested by Konstantin V. Salikhov I tried different file format (gif) and it worked. But is there any way i can use "jpeg"?