Shh! Your secret is safe — A simple guide to Steganography in Python

Suman Adhikari
Analytics Vidhya
Published in
6 min readMay 25, 2020
Image Credits: Google Images

Ever tried to speak to your friend in a secret code? We all have tried, right?

And it’s not only us. When Julius Caesar sent messages to his generals, he didn’t trust his messengers. So he replaced every A in his messages with a D, every B with an E, and so on through the alphabet. Only someone who knew the “shift by 3” rule could understand his messages. Pretty clever, right?

This method of converting original data to “secret” data is known as encryption. And, the converted “secret data” itself is known as encrypted data.

While it’s a clever technique for exchanging secret messages, there is still a possibility that someone could figure out the “secret rule”(in this case, shift by 3 rule) of unscrambling and obtain the original message.

But, if we hide the encrypted data itself, other people might never notice that there had been an exchange of secrets between two parties. So, is there any way to hide the encrypted data itself?

Exactly this is the kind of thing that we are going to talk about in this article.

From top secret government agencies to terrorist organizations, almost everyone uses steganography in order to exchange highly confidential data, so that, it seems like exchange of secret data never took place.

So, What is Steganography?

Image Steganography is the technique of hiding data(including text, image, audio, video) inside an image in such a way that a human being generally won’t be able to distinguish between the manipulated image and the original image. This adds an extra layer of security to the data being transmitted.

In this article, we will build an image steganography software from scratch using Python. This software will be able to hide password protected secret messages inside images in such a way that a normal person won’t be able to visually distinguish between the original image and the modified image.

Before writing the code, let us first understand the underlying concept of this technique.

There are many ways to do this. But, we will use the LSB(Least Significant Bit) steganography method for implementing this software.

Let us first understand how images are represented in computers.

Each image is represented as a matrix of pixels in computers. Pixels are the smallest elements of an image. Each pixel represents an individual point in the image consisting of an individual color. And thus, a collection of all the pixels(hence, color points) builds up the whole image. If you zoom too deep into an image, you might be able to see individual pixels.

Image source: www.ultimate-photo-tips.com (© Julie Waterhouse Photography)

Interesting, right? But, how are pixels themselves represented in computers?

As we have discussed earlier, pixels are nothing but color points. Any color in this world can be created using three primary colors- Red, Green and Blue (RGB). So, each pixel is represented as a tuple of the intensity values (ranging from 0 to 255) of these three primary colors. This representation is known as RGB representation. The first element of RGB tuple represents the intensity of color red, the second element represents intensity of green and the third element represents the intensity of blue. For example, rgb(255,0,0) represents pure red color, rgb(0,0,255) represents pure blue while rgb(255,255,0) and rgb(160,32,240) represents yellow and purple respectively.

As we know these color intensity values are nothing but integers ranging from 0 to 255, these integers are again converted into binary format in order to be stored in computer’s memory/storage devices.

This diagram shows how each pixel of an image can be described by three 8-bit binary numbers. (Image Source: DataGenetics)

Now we know how images are stored in computers. Let’s use this knowledge to hide secret data inside images. To do this, we will implement the following process:

  1. An image will be taken as input from user, inside which we want to hide the secret data. We will call this image as “cover image”.
  2. Plain text data will be taken as input from user.
  3. The plain text will be converted into an encrypted text using a password (which will be taken from user).
  4. The encrypted text will be converted into raw binary data using ASCII table.
  5. Each bit of this binary data will be embedded in the Least Significant Bit(LSB) of RGB values of each pixel in the cover image.
  6. The changes in color value are so small that a normal human eye won’t be able to detect these changes.
  7. This modified image will contain the secret data, which can only be read if someone has the right password.

In a nutshell, the following image describes how LSB steganography works (and what we are trying to do in this article):

Now that we have a clear understanding of the whole process, let’s bring this idea into action. We are going to implement this in Python 3 and we will use OpenCV library for image processing.

Let us first import all required libraries.

Now we will define the encode() function for performing the encoding process and decode() function for retrieving data from an encoded image. So, let us first define this encode() function.

The first thing we would like to do inside encode() function is to encrypt the secret data into encrypted text. For doing this, we will define a function called encrypt_decrypt(), which will take a string and password as input and then it will encrypt/decrypt data based on the mode of input. The default mode is set for encryption(mode=’enc’).

The next thing that we need to do is to convert this encrypted text into binary form. In order to do this, we will define a str2bin() function, which will be able to convert any given text into binary data.

Now, let’s implement the encode function.

You might have seen that we are throwing some custom exceptions in this encode() function, we need to define these custom exceptions like this-

Our next task is to define the decode()function for retrieving encoded data from an image by providing the correct password.

But, before implementing decode() function, we will define the bin2str()function, which will be able to convert binary data into text strings.

Now it’s time to define the decode() function.

Now, if you wish, you can create a menu driven program which will use these functions in order to perform steganography over an image.

Note: The output images must be saved in PNG format(with .png extension) otherwise, JPEG compression might compress the image while writing pixels values. This might lead to failure of the steganography process because compression might modify pixel values.

I created a GUI implementation of the above program using the Qt GUI framework for Python. The GUI version of this steganography program looks like this:

Let’s see the results using this software.

Here is the original image that I am going to use as cover image:

Now I will encode the message “The quick brown fox jumps over the lazy dog” into this image using “hello_world” as password. After encoding the message, here is the resulting image:

Are you able to visually distinguish between these images? No, right? That’s the magic of steganography !

You can find all the source codes of this program including the GUI version in the Github repo of LSB Steganography Software.

You can download a pre-compiled exe file of this software for Windows from this software’s release page on Github.

Contributions and suggestions for improvements are most welcome on the Github repo.

--

--