Learn Image stenography with Python

Learn Image stenography with Python

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 the 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

  • Stegano
  • Steganocryptopy

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 decrypt 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 it by looking which 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

Encrypting text into image

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

Lets now see decrypt the image using our key

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

Congratulations, you have just learned how to hide secret messages in the image, Now don\’t be shy share with your fellow friends.

The original Article can be found on kalebujordan.com