Learn how to hide secret messages in Images

Hi Guys,

Today we gonna learn how to apply coding skills to cryptography, by performing image-based stenography which involving secret messages in the image.

Stenography has been used for quite a while, since World War II time, it was heavily used for communication among allies so as to prevent the info being captured by enemies

I will guide you on how to do using two different techniques, One involving a secret key and one that doesn’t.

Requirements

Installation

pip install steganocryptopy 
pip install stegano

Image stenography without Key

Here will be hiding our hiding text within an image without any encryption key, therefore the recipient of the image is able to decrypts and get the hidden text instantly without any key

Pros :
The advantage of this is simple since you’re not going to deal with any memorized key issues

Cons :
Anyone can decrypt as long as he uses the same library you used during encryption.

Encrypting text into an image Syntax

from stegano import lsb
secret = lsb.hide(path_to_img, secret_msg) secret.save(ecrypted_img_name)

You’re supposed to have a sample image on your project folder to hide the message, we gonna provide a path to our image to our encryption library

Example of Usage

>>> from stegano import lsb 
>>> secret = lsb.hide("sample.png", "Python is dangerous be careful")
>>> secret.save("sample_secret.png")

Now if you look at the project folder you will realize there is a new image with the name sample_secret.png, there nothing by looking that tells you it consists of a hidden msg.

Decrypting text from an image

Make sure the image with hidden text is in your project folder

Syntax

>>> from stegano import lsb 
>>>lsb.reveal(path_to_an_image)

Usage

>>> from stegano import lsb 
>>>lsb.reveal('sample_secret.png')
'Python is dangerous be careful'

Well that’s it, now let’s dive into hiding into an image with a secret key

Image stenography with Secret Key

Here we will be hiding our secret text in the message together with an encryption key required to decode it, therefore only the one with the secret key is able to decode it.

Pros:
It’s much secure since only those with a secret key can decrypt it

Cons:
Once the encryption key is lost, it complicates the decryption process

Syntax

>>>from steganocryptopy.steganography import Steganography 
>>> Steganography.generate_key(path_to_key)
>>> encrypted = Steganography.encrypt(path_to_key, path_to_img, path_to_secretfile)
>>> encrypted.save(encrypted_imgname)

Usage

Let’s assume I have a file containing my secret key named key, a file consisting of our hidden message named classified.us, and our raw image named sample.png

>>>from steganocryptopy.steganography import Steganography 
>>> Steganography.generate_key("key")
>>> encrypted = Steganography.encrypt("key","sample.png", "classified.us")
>>> encrypted.save("Secret.png")

Now once you run the above code you should see a new image on your project folder named Secret.png consisting of secret message.

The decryption of the Message

To decrypt the image, you will need a file containing a key and your encrypted image

Syntax

>>> from steganocryptopy.steganography import Steganography 
>>> Steganography.decrypt(path_to_key, path_to_image)

Usage

>>> from steganocryptopy.steganography import Steganography 
>>> Steganography.decrypt("key", "Secret_img.png")
'Life is short you need Python\n'

I also write in-depth articles about Python on my Personal Blog

You can also connect with on Twitter

Originally published at https://kalebujordan.com on September 30, 2020.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Kalebu Jordan

Kalebu Jordan

128 Followers

Mechatronics Engineer by Professional || Self taught Python Developer || Passionate about open source and bringing impact to education sector