Hiding secrets in image
Today we gonna learn about image stenography in python, whereby we will be hiding text messages in images in a way that that is almost impossible to spot out by looking.

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 to 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 decrypt and get the hidden text instantly without any key
Pros :
The advantage of this is simple since you’re not going to deal 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 tell you it consist 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)
Example of 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 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 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 secret message.
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'
Congratulations, you have just learned how to perform image stenography in python and learned ways you could hide secret messages in the image.
Now don’t be shy, knowledge is good when shared now feel free to share it with your fellow friends on other dev communities, to share on twitter press Tweet now
In case of any comment, suggestion, or difficulties drop it in the comment box below, and I will get back to you ASAP.
Don’t forget to subscribe to this blog post to be notified when a new cool tutorial is out
One thought on “Image stenography in Python using bit-manipulation.”