Source code for this project can be found at: http://pastebin.com/GiSavbfz
Steganography is the act of hiding a message in such a way, that only the intended recipient is able to retrieve it, often in plain sight. Note, this is not the same as cryptography – cryptography is a completely different discipline. Sidenote, if you actually intend to send secret messages to someone else, please, please do not use steganography. Steganography is fun and all, but it’s not a reliable secure way to transmit secret information.
An image is composed of a bunch of RGB codes for every pixel. Each color code has a corresponding integer value associated with it (between 0 and 255), and integers are composed of bits. This means that every pixel has three 8-bit binary codes associated with it, giving the pixel its color. However, not all bits in the binary code are of equal importance. Altering the final two bits of say the red color code will hardly be noticeable for us humans. How can we use this to transmit secret information?
A message is, just like the color codes, composed of bits. And because altering the least significant bits in the color codes won’t make a noticeable difference, we can encode a message into the least significant bits of the color codes. Clever, eh?
In our implementation, we choose a 5-bit alphabet for encoding our messages. We won’t need anymore if we’re sending top-secret information to our pals – styling and unicode is not of importance. We’re choosing the letters A-Z (0-25), space, and basic punctuation (dots, commas, exclamation mark, question mark and apostrophe), for a total of 32 characters, which fits neatly inside 5 bits.
The human eye is best able to detect changes in contrast in the green channel, so we’ll take two bits from the red channel, two bits from the blue channel, and one bit from the green channel for each pixel. This means we can encode one character into one pixel on the image, which allows us to transmit pretty huge messages in just a single image.
The black bits our ours, the rest are reserved for the pixel. We went ahead and encoded a message onto on of these images of Barack Obama:
Can you tell which is the fake Barack Obama?
You can find a link to the source code at the top of this post. Here’s how you use it:
//To encode a message Encoder e = new Encoder("path-to-image.png"); e.encodeMessage("your-message-here"); e.saveImage("path-of-new-image.png"); //We advise against using lossy image formats, such as JPEG, as they'll ruin the hidden message. //To retrieve a message Encoder e = new Encoder("path-to-image.png"); String hiddenMessage = e.getMessage();