• Hiding data in an image: image Steganography using Python
  • Rupali Roy
  • The Nuggets translation Project
  • Permanent link to this article: github.com/xitu/gold-m…
  • Translator: Jessica
  • Proofreader: Jiang doesn’t know, Baddyo

Hiding data in images: Using Python to implement image steganography

Use Python to implement image steganography

Today, the world is experiencing an unprecedented explosion of data. The incredible amount of data we generate every day. Forbes article ** “How much data Do we Create every day?” ** points out that at our current rate, we’re creating about 250 gigabytes of data per day, but that pace will accelerate with the growth of the Internet of Things (IoT). In the last two years alone, we’ve generated 90 percent of the world’s data. The data explosion phenomenon reflected in this article needs our attention!

The data. Essentially, the modern computer world revolves around this word. But what is it about it that fascinates us so? In today’s world, many businesses are beginning to realize the power of data, as it can potentially predict customer trends, increase sales and scale companies to greater heights. With the rapid progress of technology and continuous innovation in the way data is used, ensuring data security has become our top priority. Data sharing is increasing because thousands of messages and data are transferred from one place to another on the Internet every day. The protection of data is of Paramount concern to the sender and it is very important to encrypt our messages in an encryption that only the recipient can understand.

In this article, we’ll learn what least-significant steganography is and how to implement it in Python.

What is steganography?

Steganography is the process of hiding secret information in a larger message so that others cannot know the existence of the hidden message and its contents. The purpose of steganography is to ensure confidential communication between two parties. Unlike cryptography, which hides the contents of secret messages, steganography conceals the fact that messages are communicated. Although there are differences between steganography and cryptography, there are many similarities between the two, and some authors would classify steganography as a form of cryptography because covert communications are also confidential messages.

What are the advantages of using steganography over cryptography?

Until now, cryptography has worked to protect the confidentiality between sender and receiver. However, in addition to cryptography, steganography is now increasingly being used to add more layers of protection to data that needs to be hidden. The advantage of using steganography over cryptography alone is that intentionally encrypted messages do not attract attention as monitored objects. Clearly visible encrypted messages, no matter how undecipherable, attract attention. And in countries where encryption is illegal, it may itself be a crime. [1]

The classification of steganography

Steganography can now be performed on a variety of transmission media such as image, video, text or audio.

Basic steganography classification model

As shown in the figure above, both the raw image file (X) and the confidential message (M) are passed into the steganography encoder as input parameters. Steganography encoder functions f(X,M,K) write confidential messages to cover image files by using techniques such as least significant bit encoding. The resulting steganography image looks very similar to the cover image file and is indistinguishable to the naked eye. That completes the coding. To extract the confidential message, enter the previously generated steganography image into the steganography decoder. [3]

This article will use Python to implement image steganography. It teaches you to use the Python language to hide text messages using a technique called “Least Significant Bit (LSB).”

Least significant bit steganography

A digital image can be described as a finite set of numeric values called pixels. A pixel is the smallest indivisible unit in an image, and its value represents the brightness of a given color at any particular point. Thus, we can think of an image as a matrix (or two-dimensional array) of pixels with a fixed number of rows and columns.

The least significant bit (LSB) is a technique in which the last bit of each pixel is modified and replaced with the data bit of a confidential message.

It is clear from the figure above that if we modify the most significant bit (MSB), it will have a greater effect on the final value, but if we modify the least significant bit (LSB), the effect on the final value will be minimal, so we use the least significant bit steganography.

How does the least significant bit work?

Each pixel contains three values, red, green, and blue, which range from 0 to 255, in other words, they are an 8-bit binary number [4]. To illustrate how it works, let’s say you want to hide the message “hi” in a 4×4 image with the following pixel values:

[(225, 12, 99), (155, 2, 50), (99, 51, 15), (15, 55, 22), (155, 61, 87), (63, 30, 17) (1, 55, 19), (99, 81, 66), (219, 77, 91), (69, 39, 50), (18, 200, 33), (25, 54, 190)]

Using ASCII tables, we can first convert confidential messages to decimal values and then to binary: 0110100 0110101. Now, we iterate over the pixel values one by one, and after converting them to binary, we replace each least significant bit with that information bit in turn. (For example, if 225 is 11100001, we replace the last bit, the rightmost (1) and the first (0) of the secret message, and so on). Such an operation would only change the pixel value by +1 or -1, so it would be invisible to the naked eye. The pixel values obtained after performing least-significant steganography are as follows:

[(224, 13, 99), (154, 3, 50), (98, 50, 15), (15, 54, 23), (154, 61, 87), (63, 30, 17) (1, 55, 19), (99, 81, 66), (219, 77, 91),(69, 39, 50),(18, 200, 33),(25, 54, 190)]

Use Python to hide text in the image

In this section, you’ll use Python code to step through the process of hiding and displaying text. First, open a Google Collab Notebook and follow these steps:

Before you start writing code, you can use the Upload option in the left menu bar to upload the image (PNG file) you want to use for steganography.

Step 1: Import all the necessary Python libraries.

Step 2: Define a function that can convert any type of data to binary. We will use this function to convert confidential message data and pixel values to binary during the encoding and decoding phases.

Step 3: Write a function that hides confidential messages in the image by changing the least significant bits.

Step 4: Define a function that decodes hidden information from the hidden image.

Step 5: Define a function that takes the input image name and confidential message as the user’s input.

Step 6: Create a function that asks the user to enter the name of the image to be decoded, and then calls the showData() function to return the decoded message.

Step 7: Main function

Output/Results:

Encrypted message:

Decoded message:

If you’re interested in the code, you can check out my Jupyter Notebook code on Github.

References:

  1. Towardsdatascience.com/steganograp…
  2. www.edureka.co/blog/stegan…
  3. www.forbes.com/sites/berna…
  4. www.ukessays.com/essays/comp…
  5. www.thepythoncode.com/article/hid…
  6. www.youtube.com/watch?v=xep…

If you find any mistakes in your translation or other areas that need to be improved, you are welcome to the Nuggets Translation Program to revise and PR your translation, and you can also get the corresponding reward points. The permanent link to this article at the beginning of this article is the MarkDown link to this article on GitHub.


The Nuggets Translation Project is a community that translates quality Internet technical articles from English sharing articles on nuggets. The content covers Android, iOS, front-end, back-end, blockchain, products, design, artificial intelligence and other fields. If you want to see more high-quality translation, please continue to pay attention to the Translation plan of Digging Gold, the official Weibo, Zhihu column.