encrypt & decrypt file in python

How to Encrypt & Decrypt Files in Python

Python supports data encryption & decryption to help you secure your data against thefts and frauds. In this article, we will learn how to encrypt & decrypt files in python. Python supports numerous types of encryption/decryption algorithm and offers several modules & packages for it. For our purpose, we will use a symmetric encryption that uses the same key to encrypt & decrypt files. We will use fernet module of cryptography package for this purpose.


How to Encrypt & Decrypt Files in Python

Here are the steps to encrypt & decrypt files in python.


1. Install Cryptography Package

Open terminal and run following command to install cryptography package.

$ pip install cryptography


2. Create Python Script

Create an empty python script encrypt.py with the following command.

$ sudo vi encrypt.py

Add the following lines to it, to import Fernet module.

# import required module
from cryptography.fernet import Fernet

Next, add the following lines to it to generate encryption key.

# key generation
key = Fernet.generate_key()

# string the key in a file
with open('filekey.key', 'wb') as filekey:
filekey.write(key)

The above code will generate a file named filekey.key with one line of string that is your key.


3. Encrypt File

Once you have generated the encryption key, you can follow these steps to encrypt your file.

  • Open file containing key
  • Initialize Fernet object and store it in fernet variable
  • Read file to be encrypted
  • Encrypt file and store into an object
  • Write encrypted data back into original file.

For our example, we will encrypt file projects.csv.

# opening the key
with open('filekey.key', 'rb') as filekey:
    key = filekey.read()
  
# using the generated key
fernet = Fernet(key)
  
# opening the original file to encrypt
with open('nba.csv', 'rb') as file:
    original = file.read()
      
# encrypting the file
encrypted = fernet.encrypt(original)
  
# opening the file in write mode and 
# writing the encrypted data
with open('projects.csv', 'wb') as encrypted_file:
    encrypted_file.write(encrypted)

After the encryption takes place, all data in projects.csv will be replaced by their equivalent encrypted strings.

Here is the complete encryption code encrypt.py for your reference.

#!/usr/bin/env python

# import required module
from cryptography.fernet import Fernet

# key generation
key = Fernet.generate_key()

# string the key in a file
with open('filekey.key', 'wb') as filekey:
filekey.write(key)

# opening the key
with open('filekey.key', 'rb') as filekey:
    key = filekey.read()
  
# using the generated key
fernet = Fernet(key)
  
# opening the original file to encrypt
with open('projects.csv', 'rb') as file:
    original = file.read()
      
# encrypting the file
encrypted = fernet.encrypt(original)
  
# opening the file in write mode and 
# writing the encrypted data
with open('projects.csv', 'wb') as encrypted_file:
    encrypted_file.write(encrypted)

Run the python code with the following command.

$ python encrypt.py


4. Decrypt File

If you want to decrypt file, create another python script

$ sudo vi decrypt.py

Next, you can similarly follow these steps.

  • Initialize Fernet object
  • Open encrypted file
  • Decrypt file and store it in an object
  • Write back decrypted content to original file

Add the following lines to your python script.

#!/usr/bin/env python

# import required module
from cryptography.fernet import Fernet

# opening the key
with open('filekey.key', 'rb') as filekey:
    key = filekey.read()

# using the key
fernet = Fernet(key)
  
# opening the encrypted file
with open('projects.csv', 'rb') as enc_file:
    encrypted = enc_file.read()
  
# decrypting the file
decrypted = fernet.decrypt(encrypted)
  
# opening the file in write mode and
# writing the decrypted data
with open('projects.csv', 'wb') as dec_file:
    dec_file.write(decrypted)

Save and close the file. Run the code with the following command to decrypt file.

$ python decrypt.py

If you open your file projects.csv after you run the decryption script, you will find the original unencrypted data in your file.

In this article, we have learnt how to encrypt and decrypt files in python. Please note, you can use the above code for any file type, not just .csv files. In fact, you can also customize the above code to encrypt strings, instead of files. You just need to pass strings (instead of files) in encrypt() and decrypt() functions above.

You can use the above code as standalone scripts or embed it within your application, or website, as per your requirement.

Also read:

How to Recursively Download Files Using Wget
Bash Script to Run Commands on Remote Server
How to Add Text to Image in Python
How to Find & Copy Files in Linux
How to Rename Downloaded File in Wget

One thought on “How to Encrypt & Decrypt Files in Python

  1. Great work!! I was looking for a short and understandable methode to encrrypt and decrypt files.. I had found several pages but yours is simple and good. Keep up the good work!
    Thank you

Leave a Reply

Your email address will not be published. Required fields are marked *