Image Based Steganography using Python

What is Steganography?

Steganography is the process of hiding a secret message within a larger one in such a way that the presence or contents of the hidden message are unknown. The goal of steganography is to keep communication between two parties hidden. Unlike cryptography, which allows us to hide the content of a secret message, Steganography hides the fact that a message has been sent. Despite the fact that Steganography and Cryptography are not the same, there are some parallels between the two. Steganography is classified as a type of cryptography by some writers since hidden communication appears to be a secret message.

Steganography is the process of hiding confidential data within an ordinary file during transmission. Secret data and ordinary files can both take the form of a text message, image, audio clip, or video file.
Image steganography is the practice of hiding sensitive data within an image or video file.

A simple example of hiding a text message within an image is provided below. The two main steps are as follows:

Encryption is the process of storing a text message within an image.

Decryption is the process of extracting the message hidden within an image.

Before going to the coding part we should first install the pillow and stepic libraries into our system

Installation:

pip install Pillow
pip install stepic

Output:

Collecting stepic
Downloading stepic-0.5.0.tar.gz (219 kB)
|████████████████████████████████| 219 kB 4.1 MB/s 
Requirement already satisfied: pillow in /usr/local/lib/python3.7/
dist-packages
 (from stepic) (7.1.2)
Building wheels for collected packages: stepic
Building wheel for stepic (setup.py) ... done
Created wheel for stepic: filename=stepic-0.5.0-py3-none-any.whl size=12428
 sha256=bcf777c1de2853e6fd38c63e0ba14b7d95c6803aebac77f72f18752979b216a2
Stored in directory: /root/.cache/pip/wheels/47/8d/57/322e721ace59cf72d3282
42bbc636e6affc672344cf5480f33
Successfully built stepic
Installing collected packages: stepic
Successfully installed stepic-0.5.0

pillow module in Python:

Pillow is a Python Imaging Library (PIL) that allows you to open, manipulate, and save images in a variety of formats.

stepic module in Python:
stepic is a Python package for hiding data within an image by slightly altering the image’s colors. These changes are normally unnoticeable to the naked eye, but they are detectable by machines.

Image-Based Steganography using Python

1)Encryption

Approach:

  • Import Image from PIL module using the import keyword
  • Import stepic module using the import keyword
  • Open the image using the open() function of the Image module by passing the image filename/path as an argument to it in which the secret message will be stored.
  • Give some random secret message and store it in a variable
  • Convert the above given secret message into UTF-8 format using the encode() function and store it in the same variable
  • Pass the given image and secret message into the encode() function of the stepic module which gives a new image within which the secret message is hidden.
  • Save the above encoded/encrypted image with some random name and extension using the save() function
  • Print some random text for acknowledgment.
  • The Exit of the Program.

Below is the implementation:

# Import Image from PIL module using the import keyword
from PIL import Image
# Import stepic module using the import keyword
import stepic

# Open the image using the open() function of the Image module 
# by passing the image filename/path as an argument to it
# in which the secret message will be stored.
gvn_image = Image.open("sampleimage.jpg")
# Give some random secret message and store it in a variable
secret_msg = "Welcome to Python-programs"
# Convert the above given secret message into UTF-8 format using the encode() function
# and store it in the same variable
secret_msg = secret_msg.encode()
# Pass the given image and secret message into the encode() function of the stepic module
# which gives a new image within which the secret message is hidden.
encodedimage = stepic.encode(gvn_image, secret_msg)
# Save the above encoded/encrypted image with some random name and extension using the 
# save() function
encodedimage.save("EncryptedImage.png")
# Print some random text for acknowledgment
print("Encryption has done successfully")

Output:

Encryption has done successfully

Comparing Original and Encrypted Images

As seen in the preceding image, the original image and the image acquired after encryption appears identical. The secret text message hiding within the latter image is not apparent to us, nor does this image appear altered in terms of pixel intensities to the human eye.

2)Decryption 

Approach:

  • Import Image from PIL module using the import keyword
  • Import stepic module using the import keyword
  • Open the image from which you want to extract the secret message(encrypted image) using the open() function by passing filename/path as an argument to it.
  • Store it in a variable
  • Pass the above encrypted image to the decode() function of the stepic module to give the encrypted secret message in the string format.
  • Store it in another variable
  • Print some random text for acknowledgment
  •  Print the decrypted/decoded message.
  • The Exit of the Program.

Below is the implementation:

# Import Image from PIL module using the import keyword
from PIL import Image
# Import stepic module using the import keyword
import stepic

# Open the image from which you want to extract the secret message(encrypted image) using the
# open() function by passing filename/path as an argument to it.
# Store it in a variable
encryptd_image = Image.open("EncryptedImage.png")

# Pass the above encrypted image to the decode() function of the stepic module 
# to give the encrypted secret message in the string format.
# Store it in another variable
decryptedmessage = stepic.decode(encryptd_image)
# Print some random text for acknowledgment
print("Decryption has done successfully")
# Print the decrypted/decoded message
print("The decrypted message = ", decryptedmessage)

Output:

Decryption has done successfully
The decrypted message = Welcome to Python-programs