Hiding Text In Images


Steganography is the concealing a message within another message. For example, a secret message can be embedded within a document such that, taking the first letter of each word, you can reform the hidden message.

The following two rectangles appear to be very similar.

Message Hidden In Image

The rectangle on the left is a plain old rectangle, the rectangle on the right has the contents of Martin Luther King's famous 'I Have A Dream' speech encoded within the pixel data. If you use some graphics editing software and use the eye-dropper tool, you'll notice that there are tiny changes in the RGB values from one pixel to the next.

In this case, the text has not been encrypted in any way, the ASCII codes of each character, including punctuation and white space are embedded within the pixel data of the image. Steganography is not about ciphers, it's about codes. By codes, we mean ways of representing information. In order to store the text data, we need a way of encoding the ASCII values of our message that can be written to and extracted from the image.

The Approach

In a bitmap, the colour of each pixel is stored. We form colours by mixing red, green and blue. A byte is used to store a value for each colour channel. If we take 3 adjacent pixels, there are 9 bytes in total used to store the colour information. The rightmost bit of each of those bytes is the units bit, if we change its value, we only change the colour channel information by 1. Using the 9 rightmost bits, we have enough place values to store a binary number, the ASCII code of a character.

LOAD plain bitmap
   READ next character from plain text
   READ next 3 pixel colours from plain bitmap
   WRITE ASCII of current character into pixel data
   WRITE new pixel data to cipher bitmap
UNTIL message complete or end of image
OUTPUT cipher bitmap

You can pack quite a lot of extra information into an image with minimal impact on its quality, particularly on screen. There are nearly 10 000 characters used here to encode the same speech within the image of the lazy kitten.

Message Hidden In Image


How you go about this depends on the features of the language you choose to use. Any language where you can draw images and set the colour of pixels one-by-one would work.

If you are unsure about how to implement the encoding system described above, start with something a little simpler. Take some text as input. Pad out the text with random characters so that its length is a multiple of 3. Form the pixel colours for the image by taking the RGB values of each pixel from the ASCII codes of groups of 3 characters from the input string. The outcome will look a little murky but will have encoded some text.

You can improve on this by looking at the range of ASCII values you are getting in the input string. A little operation on the numbers and you can get a more pleasant range of colours in the output.

If you can do encoding and decoding with an image like this, you can modify an existing image the same way.