07.07.2023 Views

Implementing-cryptography-using-python

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

190 Chapter 6 ■ Using Cryptography with Images

We now examine how the module works when you do not supply the

correct key. Change the key to anything else and then pass the new key into

the CryptoSteganography class:

>>> key = "AnotherKey"

>>> crypto_steganography = CryptoSteganography(key)

You can now use the retrieve() method to attempt to extract the message.

This time, when you print the retrieved value, you will get the value None,

indicating that the key is incorrect:

>>> secret = crypto_steganography.retrieve(modfile)

>>> print(secret)

None

Now that you have seen the components, let's put them all together and show

how hiding information using steganography changes the original file:

from cryptosteganography import CryptoSteganography

key = "1111222233334444!"

crypto_steganography = CryptoSteganography(key)

print()

print('The program is looking for an image named ch6_secret_image.png\n')

origfile = "chapter6\steg\ch6_secret_image.png"

print('The image with the hidden message will be called steg_ch6_secret_

image.png\n')

modfile = "chapter6\steg\steg_ch6_secret_image.png"

secretMsg = ""

message1 = "Sympathy for the favorite nation, facilitating the illusion

of an imaginary common "

message2 = "interest in cases where no real common interest exists, and

infusing into one the "

message3 = "enmities of the other, betrays the former into a

participation in the quarrels and "

message4 = "wars of the latter without adequate inducement or

justification."

secretMsg = secretMsg.join([message1, message2, message3, message4])

crypto_steganography.hide(origfile, modfile, secretMsg)

secret = crypto_steganography.retrieve(modfile)

print("The secret that is hidden in the file is:\n")

print(secret)

print()

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!