I’m a Mechatronics engineer | Pro Python Developer | AI Enthusiast
Today, we are gonna learn how to apply coding skills to cryptography, by performing image-based stenography which hiding involves secret messages in an image.
Stenography has been used for quite a while. Since World War II, 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.
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.
The advantage of this is simple since you’re not going to deal any memorized key issues.
Anyone can decrypt as long as he uses the same library you used during encryption.
Encrypting text into an image
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 are 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 which can tell you it consists of a hidden msg.
Decrypting text from an image
Make sure the image with hidden text is in your project folder.
>>> from stegano import lsb >>>lsb.reveal(path_to_an_image)
>>> 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.
It’s much secure since only those with a secret key can decrypt it.
Once the encryption key is lost, it complicates the decryption process.
>>>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)
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
>>> from steganocryptopy.steganography import Steganography >>> Steganography.decrypt(path_to_key, path_to_image)
>>> from steganocryptopy.steganography import Steganography >>> Steganography.decrypt("key", "Secret_img.png") 'Life is short you need Pythonn'
Congratulations, you have just learned how to hide secret messages in the image, Now don’t be shy to share messages with your friends.
In case of any comment, suggestion, create a thread in community and I will get back to you as fast as possible.
Don’t forget to subscribe to this blog post to be notified when a new cool tutorial is out
Previously published at https://kalebujordan.com/image-stenography-in-python/