Encrypt and Decrypt Files using Python
Last Updated :
20 Jun, 2025
Encryption is the process of converting readable data into an unreadable format to protect its contents. This is useful when storing or sharing sensitive information. In Python, we can encrypt and decrypt files using the cryptography library’s Fernet module, which uses symmetric encryption. This means the same key is used to both encrypt and decrypt the data. Lets look at a simple example to better understand this:
Python
# Encrypt a simple message
from cryptography.fernet import Fernet
key = Fernet.generate_key()
f = Fernet(key)
token = f.encrypt(b"secret")
print(f.decrypt(token))
Output:
b'secret'
We are going to use the nba.csv file to perform all operations from now on, so click on the link to download the file if you want to.
Installation:
The cryptography library can be installed using the below command:
pip install cryptography
1. Generate and Save an Encryption Key
We first generate a secure key and save it in a .key file. This key will later be used for both encryption and decryption.
Python
from cryptography.fernet import Fernet
# Generate a key
key = Fernet.generate_key()
# Save the key into a file
with open('filekey.key', 'wb') as f:
f.write(key)
This will create a file filekey.key
with a key like:
J64ZHFpCWFlS9zT7y5zxuQN1Gb09y7cucne_EhuWyDM=
2. Encrypt a File
In this step, we encrypt the contents of a file using the key we generated earlier. The file content is read in binary format, encrypted using the Fernet object, and then the encrypted version is written back to the same file or a new one.
Python
from cryptography.fernet import Fernet
# Load the key from the .key file
with open('filekey.key', 'rb') as f:
key = f.read()
# Create a Fernet object using the key
fernet = Fernet(key)
# Open the file to be encrypted in binary read mode
with open('nba.csv', 'rb') as f:
original = f.read()
# Encrypt the file content
encrypted = fernet.encrypt(original)
# Overwrite the original file with the encrypted data
with open('nba.csv', 'wb') as f:
f.write(encrypted)
The nba.csv file before executing the above program:

The nba.csv file after executing the above program:

Explanation:
- filekey.key stores the symmetric key securely.
- Fernet(key) creates an encryption tool that can encrypt and decrypt using this key.
- f.read() reads the file content as bytes.
- fernet.encrypt(original) returns the encrypted data in bytes.
- f.write(encrypted) replaces the original file with encrypted content.
3. Decrypt the File
This step restores the encrypted file back to its original state. Using the same key, the encrypted data is decrypted and written back to the file.
Python
from cryptography.fernet import Fernet
# Load the key again
with open('filekey.key', 'rb') as f:
key = f.read()
# Create a Fernet object
fernet = Fernet(key)
# Read the encrypted data from the file
with open('nba.csv', 'rb') as f:
encrypted = f.read()
# Decrypt the encrypted data
decrypted = fernet.decrypt(encrypted)
# Write the decrypted data back to the file
with open('nba.csv', 'wb') as f:
f.write(decrypted)
The nba.csv file before executing the above program:

The nba.csv file after executing the above program:

Explanation:
- The same key is loaded from filekey.key.
- fernet.decrypt(encrypted) decrypts the content and returns the original data.
- Writing back with 'wb' restores the file to its original readable state.
Similar Reads
Encrypt and Decrypt Image using Python In this article, we will encrypt/decrypt an image using simple mathematical logic. It requires two things, data, and key, and when XOR operation is applied on both the operands i.e data and key, the data gets encrypted but when the same process is done again with the same key-value data gets decrypt
5 min read
Encrypt and Decrypt PDF using PyPDF2 PDF (Portable Document Format) is one of the most used file formats for storing and sending documents. They are commonly used for many purposes such as eBooks, Resumes, Scanned documents, etc. But as we share pdf to many people, there is a possibility of its data getting leaked or stolen. So, it's n
4 min read
How to Encrypt and Decrypt Strings in Python? In this article, we will learn about Encryption, Decryption and implement them with Python. Encryption:Encryption is the process of encoding the data. i.e converting plain text into ciphertext. This conversion is done with a key called an encryption key.Decryption:Decryption is the process of decodi
5 min read
Create an empty file using Python File handling is a very important concept for any programmer. It can be used for creating, deleting, and moving files, or to store application data, user configurations, videos, images, etc. Python too supports file handling and allows users to handle files i.e., to read and write files, along with
3 min read
Encrypt and Decrypt Using Rijndael Key in C# To keep data secure and protected it is necessary to keep the data encrypted. As we know that in C# and in other languages too there are many ways for encrypting data. The Data Encryption Standard method used for encryption was not promising good security that led to the invention of a highly secure
7 min read
Create a Credential file using Python A credential file is nothing but just a configuration file with a tad bit of encryption and an unseen security structure in the backend. There might be a situation where you might come across these kinds of files while using some kind of cloud platforms. All you do to login to the instance or give t
5 min read