LSB Steganography algorithm
In this tutorial, I are going to implement one of the simplest steganography algorithm called LSB (Least significant Bit) using python programming language. But before that, since this is my first post about steganography, I am going to explain some important terms in this field. As soon as we get familiar with these terms, I’ll continue to explain a bit about algorithm and python code which can be downloaded from here.
what is steganography? According to Wikipedia, steganography is the practice of concealing a file, message, image, or video within another file, message, image, or video. The advantage of steganography over cryptography alone is that the intended secret message does not attract attention to itself as an object of scrutiny. Plainly visible encrypted messages—no matter how unbreakable—arouse interest, and may in themselves be incriminating in countries where encryption is illegal. Thus, whereas cryptography is the practice of protecting the contents of a message alone, steganography is concerned with concealing the fact that a secret message is being sent, as well as concealing the contents of the message.
Some important definition in steganography
Cover fileIt’s a medium in which hide our information. It could be an image, audio, video or any kind of file even a text file. Different steganography techniques use different medium as cover. Here we are using image files as cover.
Stego-fileIt’s cover file that contains a secret message inside it. This is the output of a steganography algorithm.
Secret messageThe important data or information you want to conceal. It could be a text or a recorded message or whatever you want.
KeyTo protect the secret message from detection, sometimes we encrypt the secret message using know encryption algorithms or just XOR our message with a sequence of zeroes and ones.
There are two important approaches in steganography. Spatial domain steganography and transform domain steganography. The latter algorithms are more robust to changes like compression, converting, resizing while the former has more capacity and fast execution. A reasonable steganography algorithm must have high capacity, high stego-image quality and high robustness. But there is always a tradeoff between capacity, robustness and quality. This means that if you sacrifices the quality, you can increase the capacity. Also increasing the robustness of the algorithm means decreasing the capacity and vice versa. Now that we know what is steganography, it’s time to explain LSB algorithm.
Least Significant Bit(LSB) algorithm
LSB algorithm is one of the most popular and spatial domain technique that used in steganography. The idea behind this algorithm is very simple: hide bits of secret message in the lowest significant bit of each pixel (for grayscale image) or each component of a pixel (for 24bit color image). It uses the fact that changing the lowest bit of a pixel doesn’t affect the subjective quality of the image and these kind of changes are not perceivable by human vision.
But how this algorithm works? Suppose that our secret message is a binary sequence which means sequence of zeroes and ones like this:
It doesn’t matter what the message is. We can convert it to a binary sequence no matter what it’s type is. As you can see, the first bit of our secret message is 1 which is an odd number. If the intensity of the chosen pixel is an odd number, we do not need to change anything but if it is an even number, then all we have to do is to subtract one or add one to that value.
For example let’s say our pixel value is 234. Since the secret bit is 1 and pixel value is 234, we have to change it to 233 or 235. To restore the secret message at receiver, we look at the value of each pixel. If we have an odd number, it means that the secret bit is 1 else it is 0.
How to determine when to add and when to subtract the pixel value? I came up with this idea that to preserve the mean value of the original cover image, we have to keep tracking of these addition and subtraction. Lets say we have a variable named mean_history which it’s initial value is 0.everytime we add one to a pixel, we are going to increment mean_history and when we subtract one from mean_history. All we have to do is to try to keep mean_history to zero. In this way, the stego image has almost the same value as cover image. Images below show the result of the algorithm and the output of the algorithm is :
Input image size : 512 x 512 We are going to hide 262144 bits of data First 20 bytes of random secret message is: 6V]jMbx8Gz*0M2YilnNq Starting to hide 262144 bits of data... Done hiding 262144 bits of data in 0.2919160032611191 second(s) We have changed 130524 (~ 50 %) pixels(lsb) in image Recovering data.... Done recovering 262144 bits of data in 0.15040115665038045 second(s) Recover process had error? False Mean of original image : 124.061351776 Mean of stego image : 124.061351776 PSNR value of stego image : 51.145479462335935
You can download the python code of this algorithm from here.