Steganography ✍🏻➡🖼

Vedansh Vijaywargiya
7 min readSep 25, 2020

--

Encrypt data in your file using lsb (least significant bit)

Data Hiding

Steganography is the practice of encoding a file, message, image, or a video within another file, message, image, or video. It has existed for a long time, and nowadays, digital steganography is used to hide data inside images. We can hide all kinds of data by using different digital steganographic methods.

In this article, we would be using Least Significant Bit(LSB) transformation to hide messages.

Least Significant Bit (LSB)

Decimal to Binary
Binary to Decimal Conversion

In the above gif we can see the binary to decimal conversion of 4-bit binary digits from 0 to 31. This gives us a clear idea of how are binary digits converted into decimal numbers. Now we will see, what do we mean by the term Least Significant Bit.

8-bit binary digit
Image Source - Wikipedia

The above picture is a representation of 149 as an 8-bit binary digit. The rightmost highlighted bit is known as the Least Significant Bit. If we change it to 0, we will get 10010100 which is decimal equivalent of 148. If we change any other bit in the binary number above, the change would be much greater. So in this example, the rightmost bit is the least significant. That is changing it would result in the least change to the original number.

Using LSB Transformation to Hide Data inside an Image🖼.

A digital image is a representation of pixel values, and every pixel value will have numbers containing information regarding the pixel. A digital color image will have red, green, and blue channels and eight bits to represent each channel, so every channel can take a value from 0–255, and this value represents the intensity of the pixel.

(R,G,B) = (0,0,0)

This is the representation of the color Black and the representation for color white is given by:

(R,G,B) = (255,255,255)
Pixel Representation

Lets’s say we want to hide a character A in an array of pixels.This is how we would do it:

(R, G, B)= (11101010 11101001 11001010),(10111001 11001011 11101000),(11001001 00100100 11101001)

This is the pixel array, and we want to hide “A” in it. We know that the ASCII value for “A” is 65. If we convert 65 to binary we would get 01000001. So if we use LSB transformation, we can change the LSB of all the numbers in our pixel array and get the following result:

(R,G, B)= (11101010 11101001 11001010),(10111000,11001010,11101000),(11001000 00100101 11101001)

If we look closely we can see that we have changed the original value of the LSB to a new value which corresponds the digits of the binary of 65 i.e. 01000001. Now that we have understood the key logic behind data hiding in images, let’s see how to implement it in python.

Image Steganography using Python🐍

Image Used for Hiding

This is the image we are going to use for data hiding.

So, first of all let’s install all the dependencies and then we will look at the code step by step.

Installing the dependencies

We are going to use stegano module in python🐍 for this. We can easily install it by running in the command prompt(cmd):

pip install stegano

Now we will open a python file and type the following code to hide a message let’s say “Hello” inside the image:

from stegano import lsb
secret=lsb.hide("./HelloThere.jpg","Hello")

This will transform the LSB of our image and hide our message in it. But the image still needs to be saved so to that we would use the following code:

secret.save("./Encoded_Image.jpg")

This will create another image file of name “Encoded_Image.jpg” which would be very similar to the original image but it would contain the an embedded message in it.

To decode the hidden message in the image we would use the following code:

from stegano import lsb
lsb.reveal('./Encoded_Image.jpg')
Output:- Hello
Hide and Decode messages inside Images

Image Quality Comparison🧐

Original Image(left) vs Encoded Image(right)

Now we have seen LSB transformation to hide data inside an image, lets’s see how can we hide the data inside a video.

Video Steganography in Python🐍

We know that video is a collection of frames, where each frame is an image. So if we extract all the frames from a video then we can store data using LSB steganography and then stitch those encoded frames back together into a video with a secret message.

I would be using this video to spread awareness while learning😄.

So as to perform video steganography we would divide our code into the following sub-parts.

1. Installing Dependencies

We are going to use the opencv and numpy modules in python for this. We can easily install it by running in the command prompt(cmd):

pip install opencv-python
pip install numpy

We would also be using ffmpeg to extract audio file from the video so that we could use it to at the time of stitching:

pip install ffmpeg-python

2. Installing and Importing required modules

In this we are going to import the following modules:

  • stegano
  • os
  • time
  • cv2
  • numpy
  • math
  • shutil
  • subprocess
from stegano import lsb
from os.path import isfile,join
import time
import cv2
import numpy as np
import math
import os
import shutil
from subprocess import call,STDOUT

3. Inputting the video📹

First we would decide the inputs. That is first the user needs to specify whether they want to encode or decode a video. Then they would input the video file with extension which we would read using OpenCV.

Inputting Choices

4. Defining frame extraction function🎞

Now that we have the video, our first step should be defining a frame extraction function to extract frames in the form of images from the video.

5. Defining the String Split function🪓

Now that we have all the extracted frames, we can divide the strings into small chunks and hide each chunk of the message inside a frame.

6. Extracting the audio using “ffmpeg”🔊

Now we would be focusing on extracting the audio from the video which would be stitched together withe frames afterwards. We can use FFmpeg to stitch together all our frames with a hidden message to form a video and then lay out the audio

call(["ffmpeg", "-i",f_name, "-q:a", "0", "-map", "a", "temp/audio.mp3", "-y"],stdout=open(os.devnull, "w"), stderr=STDOUT)

This code will extract the audio from the given video file and save it as “audio.mp3” in the temp folder. After we encode the frames with our text, we can then use this audio file to give our encoded video file the proper audio.

Note: If the above code is giving an error then try using the following code:

call(["ffmpeg", "-i",f_name, "-q:a", "0", "-map", "a", "temp/audio.mp3", "-y"],stdout=open(os.devnull, "w"), shell=True, stderr=STDOUT)

Mostly the above code should work but if it’s still giving an error then you can refer to my GitHub Repo by clicking here.

7. Encoding the text 👨🏻‍💻

Now that we have the split string and the extracted frames, it’s time we start encoding the split strings into the extracted frames.

8. Stitching together the frames🖼 and the audio🔊

We can use ffmpeg to stitch together all our frames with a hidden message to form a video and then lay out the audio:

call(["ffmpeg", "-i", "temp/%d.png" , "-vcodec", "png", "temp/Embedded_Video.mp4", "-y"],stdout=open(os.devnull, "w"), stderr=STDOUT)

Running the code above creates the video with our secret message hidden in it. We can run the code below to stitch the audio in our video:

call(["ffmpeg", "-i", "temp/Embedded_Video.mp4", "-i", "temp/audio.mp3", "-codec", "copy", "Embedded_Video.mp4", "-y"],stdout=open(os.devnull, "w"), stderr=STDOUT)

And now we have successfully encoded our video📹 with a secret message‍✍🏻.

9. Decoding🕵🏻 the message

Now we will perform the same steps as we did during encryption but in the opposite order. So first we extract all the frames from the video and then we extract the information from the LSB and then we would join the information to decode the message.

10. Video Quality Comparison 🧐

  • To access the Original video click here
  • To access the Encoded video click here

Conclusion🚩

Implementing steganography and cryptography becomes really simple using the modern packages in python. But one problem still remains that is the new video that is created has a huge size as compared to the original video.

⚒️I would be working on this issue but if you have figured out a way then feel free to contribute to my Github Repo.

--

--