Open In App

Encrypt and Decrypt Files using Python

Last Updated : 20 Jun, 2025
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report

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.

Article Tags :
Practice Tags :

Similar Reads