Published in · 3 min read · Feb 17, 2024
--
Securing information is very important today. Without encryption, an enemy or unwanted person could read our data easily. After that, they could use it for bad things. In this article, I want to share a step-by-step of encryption on an image using Python.
Step 1: Install the cryptography library
Ensure you have the cryptography library installed. If not, you can install it using:
pip install cryptography
Step 2: Generate a Key
A key is fundamental to encryption. Run the following Python script to generate a key and save it to a file named key.key
:
from cryptography.fernet import Fernetkey = Fernet.generate_key()
with open('key.key', 'wb') as f:
f.write(key)
print("Key has been generated and saved.")
Step 3: Encrypt the Image
Now, you can use the generated key to encrypt an image. Save the following script in a file, for example, app.py
:
from cryptography.fernet import Fernet# Load the key from the file
with open('key.key', 'rb') as f:
key = f.read()
# Create a Fernet object with the key
fernet = Fernet(key)
# Read the image to be encrypted
with open('img.jpg', 'rb') as f:
photo = f.read()
# Encrypt the image
locked_photo = fernet.encrypt(photo)
# Write the encrypted image to a new file
with open('locked_img.jpg', 'wb') as locked_photo_file:
locked_photo_file.write(locked_photo)
print("Image has been encrypted and saved.")
Step 4: Let’s Start
Let’s documenting the encryption process.
Okay, we did it; we created the key to encrypt the image. What it’s use for? It is also used for the decryption process. Let’s decrypt it.
Step 1: Retrieve the Key
Before decrypting, ensure you have the key. You can generate it if you still need to, as shown in the previous steps. If you transfer the encrypted image to your friend, you should send this key. Without the key, your friend will never know what the image was.
Step 2: Decrypt the Image
Now, create a decryption script (e.g., reapps.py
) using the following code:
from cryptography.fernet import Fernet# Load the key from the file
with open('key.key', 'rb') as f:
key = f.read()
# Create a Fernet object with the key
fernet = Fernet(key)
# Read the encrypted image
with open('locked_img.jpg', 'rb') as f:
locked_photo = f.read()
# Decrypt the image
unlocked_photo = fernet.decrypt(locked_photo)
# Write the decrypted image to a new file
with open('unlocked_img.jpg', 'wb') as unlocked_photo_file:
unlocked_photo_file.write(unlocked_photo)
print("Image has been decrypted and saved.")
Step 3: Let’s Start
Let’s make a documenting the decryption process.
You can encrypt and decrypt images using Python and the cryptography library with these simple steps. Remember to keep your key secure, as it is crucial for decryption. This approach provides a foundational understanding of image encryption and serves as a starting point for more advanced encryption techniques. Thanks for reading.
Thank you for being a part of the In Plain English community! Before you go:
- Be sure to clap and follow the writer ️👏️️
- Follow us: X | LinkedIn | YouTube | Discord | Newsletter
- Visit our other platforms: Stackademic | CoFeed | Venture | Cubed
- More content at PlainEnglish.io