Image Encryption (Python Lesson 53)

Advertisements

Hello everybody,

Michael here, and in today’s post (unofficially the summer of encryption series), we’ll learn how to encrypt images with Python!

During our encryption lessons, we’ve learned how to encrypt text and files with Python. We also learned some interesting mathematics behind RSA encryption too.

Anyway, let’s get started with our image encryption! Here’s the image we’ll be using:

Yes, it’s yours truly in front of the Deadpool & Wolverine movie poster, giving a thumbs up to indicate my approval (and I highly recommend seeing it). Anyway, onto the image encryption.

Encrypting the image

Before we encrypt the image, let’s first create a Fernet key. If you recall from my previous post, Fernet key encryption is a simple symmetric-key encryption method that utilizes the same key to encrypt and decrypt something (in this case, the image).

Encryption key generation!

Before we encrypt and decrypt the image, let’s generate a file for our image key! We’d generate the image encryption/decryption key in a similar manner to the way we generated the file encryption/decryption key (take a look at the code below to see how we generated the file for the image encryption/decryption key):

from cryptography.fernet import Fernet

imageKey = Fernet.generate_key()

with open(r'C:/Users/mof39/OneDrive/Documents/imageKey.key', 'wb') as keyFile:
    keyFile.write(imageKey)

After importing the Fernet module from cryptography.Fernet (install the cryptography package if you haven’t done so already), we create our imageKey then write it to the aptly-named imageKey file-with a .key extension-to a specific file path (you can use any location on your local drive that you can access).

What does the key look like? Let’s find out:

j8P7M-h1v6_KxFkNncCYIfRIQwWmxlLToatX3lg5Jcg=

This is the key we will need to both encrypt and decrypt the image.

It’s image encryption time!

Now, let’s actually encrypt the image. Here’s how to do so:

with open(r'C:/Users/mof39/OneDrive/Documents/imageKey.key', 'rb') as keyFile:
    encryptionKey = keyFile.read()
    
fernetEncryptionKey = Fernet(encryptionKey)

with open(r'C:/Users/mof39/Downloads/20240728_132146.jpg', 'rb') as image:
    originalImage = image.read()
    
encryptedImage = fernetEncryptionKey.encrypt(originalImage)

with open(r'C:/Users/mof39/OneDrive/Documents/encryptedImage.jpg', 'wb') as image:
    image.write(encryptedImage)

What exactly did we do here? Let me explain:

  • First we read the file containing the image encryption/decryption key into the IDE.
  • Next we turned the encryptionKey into a Fernet object.
  • We then read the image we’ll be using into the IDE and encrypted it using the Fernet object we created in the previous step.
  • Finally, we wrote the encrypted image to a specific location on the local drive.
  • Word of advice-I would use different names for the encrypted and decrypted images to avoid confusion.

Now, let’s see what our encrypted image looks like:

As you can see, we can’t view our encrypted image, which is a good thing since the whole point of encrypting the image is to keep it from being seen.

However, let’s see what happens when we open this image on Notepad:

As you can see, we get this beautiful-looking gibberish, which I would love for a would-be hacker to see if they tried to intercept the image transmission to my intended recipient.

Decrypting the image

Now that we have successfully encrypted the image, let’s now decrypt it. Here’s how we’ll accomplish that:

with open(r'C:/Users/mof39/OneDrive/Documents/imageKey.key', 'rb') as keyFile:
    imageKeyFile = keyFile.read()
    
fernetDecryptionKey = Fernet(imageKeyFile)

with open(r'C:/Users/mof39/OneDrive/Documents/encryptedImage.jpg', 'rb') as image:
    encryptedImage = image.read()
    
decryptedImage = fernetDecryptionKey.decrypt(encryptedImage)

with open(r'C:/Users/mof39/OneDrive/Documents/decryptedImage.jpg', 'wb') as image:
    image.write(decryptedImage)

What exactly did we do here to decrypt the image? Let’s explain:

  • First, we read the image key file (the one containing our encryption/decryption key) into the IDE.
  • Next, we created a fernetDecryptionKey object from the imageKeyFile-we’ll use this to decrypt the image.
  • We then read the encryptedImage onto the IDE and, using out fernetDecryptionKey, decrypted the image.
  • Last but not least, we wrote the decryptedImage onto the local drive with the name decryptedImage.
  • Yes I know fernetDecryptionKey and fernetEncryptionKey are the same thing. I just thought it would be easier to use different variable names since they are being used for different purposes (decryption and encryption respectively).

And now, the moment of truth…here’s our decrypted image:

Yup, there I am in all my Deadpool & Wolverine-loving decrypted glory!

Here’s the link to today’s script in GitHub-https://github.com/mfletcher2021/blogcode/blob/main/imageencryption.py

Thanks for reading,

Michael

Leave a ReplyCancel reply