Tuesday, May 20, 2014

Steganography in Java

Steganography is a process of hiding text message on an image. This is achieved by putting text's bits to image's bit. It will produce a new image that contains hidden bits inside the image. There are a lot of approaches to do steganography but for now we will do it as simple as possible. In this approach, and many other approaches, we will use bit operations (AND, OR, XOR, RIGHT SHIFT, LEFT SHIFT, etc).


First, we define the input and output image file name, and also the text message to hide. In addition to the text message itself, we also need to persist the text message length so that we are able to fetch the hidden text message later. Integer has length of 32 bits which is equivalent to 4 bytes. That's why we have variable INT_LENGTH there.


Then we load the input image and put it into a BufferedImage object.


Next we create a new BufferedImage object by specifying its width and height. We get the width and height from input image. We also set its image type to be TYPE_3BYTE_BGR which means that we want to create an image with 8-bit RGB color Blue, Green, and Red stored in 3 bytes.


We can then obtain the Graphics2D object from the new BufferedImage. Then we copy the input BufferedImage data to our new BufferedImage for image processing later.


Since now our new BufferedImage contains data from input image, we can obtain its pixel bytes using WritableRaster and DataBufferByte classes as shown above.


Then we get text message bytes and check its length. If the text message length plus four bytes (remember that we will also persist the length, which is an integer of four bytes) doesn't fit the input image length, then an exception is thrown.


Next we persist the text message length to the image. Casting an integer to byte makes the first 3 bytes truncated. That is why we have to shift it to right. To get the left most byte, we need to right shift it 24 bits. To get the second left most byte, we need to right shift it 16 bits. To get the third left most byte, we need to right shift it 8 bits. Lastly, getting the right most byte we only need to cast it to byte.


We then replace the first four pixels in the image with our previous text message length bytes.


Next is the main part of Steganography, hiding the text message in the image. We do this by replacing pixels starting from position fourth until the text message length. We replace the original input image byte pixel with text message byte.


Next step is writing the new BufferedImage to an output file. This is the last step to produce the output image.

After successfully hiding the text message in an image, we will now try to read the hidden text message.


Our first step is to read the output image and put its data into a BufferedImage object.


Next step is to get its pixel bytes which we can do by the help of WritableRaster and DataBufferByte classes as shown above.


After getting the pixel bytes, now we can extract the data. The first four bytes have information about the text message length. We can extract the text message length by left shifting and combining them using OR. Below is the illustration of the process.



Next we create an array of byte that will hold our extracted text message data.


Next is the process of extracting the text message. Starting from position fourth until the text message length is where the text message bytes hidden. We put the data into our previously created array.


Converting the array of byte to string gives us the original text message. Below is the input and output image. There are some noise on upper left corner of the output image after we hide the text message inside. The more advanced Steganography technique can be applied to produce smoother output.



0 comments:

 

©2009 Stay the Same | by TNB