In this article we are going to built a simple tool for implementing steganography by hiding a text message within a bitmap image.
to be able to do so, we have to have a closer look at the bitmap file format structure.
In brief bitmap files can be broken into two sub-blocks which are the Bitmap header and the Bitmap data.the Bitmap header is used to identify the file as a valid Bitmap file.
the length is the header block is usually 54 bytes ( to get more Info. about the header , you can visit this page. )
After the Bitmap header comes the Bitmap data block which is the part we are concerned with in the rest of this article.
The Bitmap data block includes the actual image stored as pixels. In 32pbpARGB format ( which is the most common format ) each pixel is stored as 4 bytes ( byte for Blue ,Byte for Green , Byte for Red, and a Byte for the Alpha component of the Pixel color ).
Each component value of a pixel is stored as 8 bits ( which gives range between 0 and 255 ).
If the value of a component is changed say from 60 t0 61 the change will be really hard to be noticed by the human eye. and this is the trick we will be using.
Now, let's put hands down and start to work ;-)
To start manipulating the Image data block we need to convert the Bitmap image to and from a byte array.
so we are going to write a class called "BitmapHelper" which includes to static Methods:
- getBytes(Bitmap) : which is used to convert a bitmap image into a byte array
- getBitmap(Byte[]): which is used to convert a byte array into a Bitmap Image.
Now we can have the Bitmap block data as byte array ready for manipulation .class BitmapHelper
{
public static byte []
getBytes(Bitmap imgSource)
{
MemoryStream ms = new
MemoryStream();
imgSource.Save(ms, ImageFormat.Bmp);
return
ms.ToArray();
}
public static Bitmap getBitmap(byte []
arrData){
MemoryStream ms = new MemoryStream(arrData);
Bitmap bmp = new
Bitmap(ms);
return bmp;
}
}
Now we're going to write a Library called SteganoLib which has two methods:
- HideText : which is used to hide text in a given bitmap and return the resultant bitmap
- ExtractText: which tries to extract a hidden text from the Given Bitmap otherwise it will throw an Exception.
Let's start with the HideText method:
a Text is hidden by making use of the LSB of the R,G, B components of each pixel.
If we wanna for example store the letter A which is represented as ( 01000001 ) and we had the following values of two successive pixels in the image .
01001001 -------> 01001001
011111111 -------> 011111110
10001000 --------> 10001000
10101101 ---------> 10101100
01011101 ---------> 01011100
00101101 --------> 00101100
11001011 --------> 11001010
01101010 -------> 01101011
01011111 --------> 01011110
It can easily be noticed that the changed bits ( red bits ) represent the value of letter 'A'.
you can download and try our tool from here.