How to do it...

Execute the following steps to hide a secret message inside the Lena image using LSB data hiding and use (non-blind) steganalysis to detect the secret message:

  1. Read the (cover) image and hide a long message (by concatenating a message 10 times) inside the image. Save the stego image:
cover = Image.open('images/lena.png')
stego = lsb.hide("images/lena.png", 10*"Python Image Processing
Cookbook - LSB data hiding with Stegano")
stego.save("images/lena-secret.png")
  1. Print the message hidden inside the stego image:
print(lsb.reveal("images/lena-secret.png"))
  1. Do parity steganalysis and extract the parity encoded cover and stego images. Then, use statistical steganalysis to retrieve the common cover and stego images using the following code snippet:
parity_encoded_cover = parity.steganalyse(cover)
parity_encoded_stego = parity.steganalyse(stego)
_, cover_common = statistics.steganalyse(cover)
_, stego_common = statistics.steganalyse(stego)

If you plot all of the images along with the difference between the stego and cover image and the difference between the parity encoded stego and cover images, you will get a figure like the following screenshot:

As you can see from the preceding screenshot, by seeing the difference in the image, between the stego and the cover image, the existence of the secret message can't be detected. The parity-encoded stego and cover image are not the same and the difference reveals that there is some secret message embedded inside the stego image.

  1. Define a plot_freq() function to plot the histograms of the common pixel frequencies where the cover and stego images differ (obtained earlier with statistical steganalysis):

plot_freq(cover_common, stego_common)

You will get the output shown in the following screenshot, which clearly shows that the stego and the cover image were different:

  1. Next, use the LSB technique with sets based on generators (Sieve of Eratosthenes) to hide the secret message:

cover = Image.open("images/lena.png").convert('RGB')
secret_message = "Python Image Processing Cookbook - LSB data
hiding with Stegano lsbset!"
n = 1000
stego = lsbset.hide("images/lena.png",
secret_message,
generators.eratosthenes(),
shift = n).convert('RGB')
stego.save("images/stego.png")

If you plot the difference between the stego and cover image versus the difference between the parity-encoded stego and cover image, you will get the following screenshot:

  1. Now, try to retrieve the hidden message using the same generator (it will succeed) and a different one (it will fail):
try:
message = lsbset.reveal("images/stego.png",
generators.fibonacci())
except:
print('Could not decode with the generator provided!')
# Could not decode with the generator provided!

message = lsbset.reveal("images/stego.png",
generators.eratosthenes())
message
# 'Python Image Processing Cookbook - LSB data hiding with Stegano lsbset!'
  1. Finally, for JPEG and TIFF images, you can hide a message in the exifHeader (and reveal it) using the following code:
secret = exifHeader.hide("images/butterfly.jpg", 
"images/stego.png", secret_message=5*"Python
Image Processing Cookbook - LSB data hiding with Stegano")
print(exifHeader.reveal("images/stego.png"))
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset