Open In App

Encrypt and Decrypt Files using Python

Improve
Improve
Like Article
Like
Save
Share
Report

Encryption is the act of encoding a message so that only the intended users can see it. We encrypt data because we don’t want anyone to see or access it.

We will use the cryptography library to encrypt a file. The cryptography library uses a symmetric algorithm to encrypt the file. In the symmetric algorithm, we use the same key to encrypt and decrypt the file. The fernet module of the cryptography package has inbuilt functions for the generation of the key, encryption of plain text into cipher text, and decryption of cipher text into plain text using the encrypt() and decrypt() methods respectively. The fernet module guarantees that data encrypted using it cannot be further manipulated or read without the key. 

We are going to use the nba.csv file to perform all operations.

Installation:

The cryptography library can be installed using the below command:

pip install cryptography

Generate Key to encrypt the file

In the cryptography library, there is a cryptography algorithm called fernet. We will use the fernet module to encrypt the file.

Python3




# import required module
from cryptography.fernet import Fernet


Generating the key and saving it:

Python3




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


Above code will generate a file with the name filekey.key. The file will contain one line, which is a string acting as a key i.e. J64ZHFpCWFlS9zT7y5zxuQN1Gb09y7cucne_EhuWyDM=

Encrypt the file using the key generated

Now we have an encrypted key and file to be encrypted. Now write code to encrypt this file:

  1. Open the file that contains the key.
  2. Initialize the Fernet object and store it in the fernet variable.
  3. Read the original file.
  4. Encrypt the file and store it into an object.
  5. Then write the encrypted data into the same file nba.csv.

Python3




# 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('nba.csv', 'wb') as encrypted_file:
    encrypted_file.write(encrypted)


The nba.csv file before executing the above program:

The nba.csv file after executing the above program:

Decrypt the encrypted file

We have to use the same key to decrypt the file:

  1. Initialize the Fernet object and store it in the fernet variable.
  2. Read the encrypted file.
  3. Decrypt the file and store it into an object.
  4. Then write the decrypted data into the same file nba.csv.

Python3




# using the key
fernet = Fernet(key)
 
# opening the encrypted file
with open('nba.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('nba.csv', 'wb') as dec_file:
    dec_file.write(decrypted)


The nba.csv file before executing the above program:

The nba.csv file after executing the above program:



Last Updated : 03 Jun, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads