Step by Step of Image Encryption Using Python (2024)

Step by Step of Image Encryption Using Python (3)

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 Fernet

key = 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.

Step by Step of Image Encryption Using Python (4)

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.

Step by Step of Image Encryption Using Python (5)

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:

Step by Step of Image Encryption Using Python (2024)
Top Articles
Best Crypto Wallets of December 2023 | The Motley Fool
What is FIDO 2? | Yubico
Woodward Avenue (M-1) - Automotive Heritage Trail - National Scenic Byway Foundation
Warren Ohio Craigslist
Danatar Gym
The Definitive Great Buildings Guide - Forge Of Empires Tips
Limp Home Mode Maximum Derate
Call Follower Osrs
Encore Atlanta Cheer Competition
Optimal Perks Rs3
Slapstick Sound Effect Crossword
Stream UFC Videos on Watch ESPN - ESPN
Does Publix Have Sephora Gift Cards
Brenna Percy Reddit
1Win - инновационное онлайн-казино и букмекерская контора
Discover Westchester's Top Towns — And What Makes Them So Unique
Oc Craiglsit
I Wanna Dance with Somebody : séances à Paris et en Île-de-France - L'Officiel des spectacles
I Touch and Day Spa II
7 Fly Traps For Effective Pest Control
Simpsons Tapped Out Road To Riches
Andhrajyothy Sunday Magazine
Metro Pcs.near Me
Little Caesars 92Nd And Pecos
Glover Park Community Garden
Hood County Buy Sell And Trade
Gma' Deals & Steals Today
Where to eat: the 50 best restaurants in Freiburg im Breisgau
Taktube Irani
Ucm Black Board
Have you seen this child? Caroline Victoria Teague
R3Vlimited Forum
Composite Function Calculator + Online Solver With Free Steps
Whas Golf Card
Audi Q3 | 2023 - 2024 | De Waal Autogroep
Joe's Truck Accessories Summerville South Carolina
Consume Oakbrook Terrace Menu
Covalen hiring Ai Annotator - Dutch , Finnish, Japanese , Polish , Swedish in Dublin, County Dublin, Ireland | LinkedIn
Buhsd Studentvue
5 Tips To Throw A Fun Halloween Party For Adults
062203010
Homeloanserv Account Login
Natasha Tosini Bikini
What Is The Optavia Diet—And How Does It Work?
The Horn Of Plenty Figgerits
Sacramentocraiglist
Walmart Front Door Wreaths
Evil Dead Rise - Everything You Need To Know
Hkx File Compatibility Check Skyrim/Sse
Hcs Smartfind
8663831604
OSF OnCall Urgent Care treats minor illnesses and injuries
Latest Posts
Article information

Author: Dan Stracke

Last Updated:

Views: 6222

Rating: 4.2 / 5 (43 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Dan Stracke

Birthday: 1992-08-25

Address: 2253 Brown Springs, East Alla, OH 38634-0309

Phone: +398735162064

Job: Investor Government Associate

Hobby: Shopping, LARPing, Scrapbooking, Surfing, Slacklining, Dance, Glassblowing

Introduction: My name is Dan Stracke, I am a homely, gleaming, glamorous, inquisitive, homely, gorgeous, light person who loves writing and wants to share my knowledge and understanding with you.