Encrypt and Decrypt Files using Python - GeeksforGeeks

13 Jan, 2021
Encryption is the act of decoding the message so that intended users can only 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.
The cryptography library can be installed using the below command:
pip install cryptography
In the cryptography library, there is a cryptography algorithm called fernet. We will use the fernet module to encrypt the file.
Python3
# import required modulefrom cryptography.fernet import Fernet
Generating the key and saving it:
Python3
# key generationkey = Fernet.generate_key()  # string the key in a filewith 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=
Now we have an encrypted key and file to be encrypted. Now write code to encrypt this file:
Open the file that contains the key.Initialize the Fernet object and store it in the fernet variable.Read the original file.Encrypt the file and store it into an object.Then write the encrypted data into the same file nba.csv.
Open the file that contains the key.
Initialize the Fernet object and store it in the fernet variable.
Read the original file.
Encrypt the file and store it into an object.
Then write the encrypted data into the same file nba.csv.
Python3
# opening the keywith open('filekey.key', 'rb') as filekey:    key = filekey.read()  # using the generated keyfernet = Fernet(key)  # opening the original file to encryptwith open('nba.csv', 'rb') as file:    original = file.read()      # encrypting the fileencrypted = fernet.encrypt(original)  # opening the file in write mode and # writing the encrypted datawith 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:
We have to use the same key to decrypt the file:
Initialize the Fernet object and store it in the fernet variable.Read the encrypted file.Decrypt the file and store it into an object.Then write the decrypted data into the same file nba.csv.
Initialize the Fernet object and store it in the fernet variable.
Read the encrypted file.
Decrypt the file and store it into an object.
Then write the decrypted data into the same file nba.csv.
Python3
# using the keyfernet = Fernet(key)  # opening the encrypted filewith open('nba.csv', 'rb') as enc_file:    encrypted = enc_file.read()  # decrypting the filedecrypted = fernet.decrypt(encrypted)  # opening the file in write mode and# writing the decrypted datawith 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:
Picked
python-utility
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Defaultdict in Python
How to Install PIP on Windows ?
Deque in Python
Bar Plot in Matplotlib
Check if element exists in list in Python
Python math function | sqrt()
Python | Output Formatting
Python - Pandas dataframe.append()
How to drop one or multiple columns in Pandas Dataframe
Python Classes and Objects