In this tutorial we want to learn about File Encryption in Python, one of the best usage of Python programming language is in file encryption. so first of all let’s talk about encryption, so encryption is the process of encoding a message or information, after encryption only authorized parties can access it. in this article we want to talk about Python file encryption and how it can be implemented.
Why Encrypt Files?
Let’s talk that why we need to encrypt a file, so as you know that data breaches and hacks are becoming more common, and if we have sensitive information, then that is at risk. encryption is a way of protecting data from unauthorized users, by encrypting the data, we make the data unreadable to unauthorized parties. we can say that encrypting files is particularly important when the information contained in the file is sensitive, such as financial information or personal data.
Now let’s talk about Python File Encryption, Python provides different libraries for file encryption, including cryptography library and hashlib library.
Python Cryptography Library
Cryptography library provides functions for implementing symmetric and asymmetric encryption.
How to Encrypt a File in Python?
This is an example of using symmetric encryption, in this example we are using Fernet class from the cryptography library to generate a key and encrypt a file. we open the file in binary mode, read the file into memory and encrypted that with Fernet cipher, and then write the information to a new file.
1 2 3 4 5 6 7 8 9 10 11 12 |
from cryptography.fernet import Fernet key = Fernet.generate_key() cipher = Fernet(key) with open('file_to_encrypt.txt', 'rb') as file: original = file.read() encrypted = cipher.encrypt(original) with open('encrypted_file.txt', 'wb') as encrypted_file: encrypted_file.write(encrypted) |
Now i have created a text file and inside that i have added some texts, and after this will be the new encrypted file.
How to Decrypt a File in Python?
For decrypting the encrypted file using cryptography. you need to follow a process similar to the encryption, but in reverse. You will use the same key that was used for encryption to decrypt the data. Here is how you can do it:
- Load the key that was used for encryption.
- Read the encrypted file.
- Use the Fernet instance initialized with the key to decrypt the data.
- Save the decrypted data back to a file or process it as needed.
This is the complete code, make sure to change your encryption key
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
from cryptography.fernet import Fernet # The key you printed during encryption key = b'DTsIkXadjf2FUIO0qYrvYCmr8D_9P-AeXKxPCBJiPa0=' # Instantiate a Fernet instance with the key cipher = Fernet(key) # Read the encrypted file with open('encrypted_file.txt', 'rb') as encrypted_file: encrypted_data = encrypted_file.read() # Decrypt the data try: decrypted = cipher.decrypt(encrypted_data) # Write the decrypted data back to a file with open('decrypted_file.txt', 'wb') as decrypted_file: decrypted_file.write(decrypted) print("Decryption successful. Data written to 'decrypted_file.txt'.") except Exception as e: print(f"An error occurred during decryption: {e}") |
Also there is another library for Python file encryption that you can use.
Python File Encryption with Hashlib Library
hashlib library provides functions for hashing data.
This is an example of using hashlib library to hash a file, in this example we are using hashlib library to create a SHA-256 hash of a file. after that the file is opened in binary mode, we are reading the file into memory, hashed using SHA-256 algorithm and after that write to a new file.
1 2 3 4 5 6 7 8 9 |
import hashlib with open('file_to_hash.txt', 'rb') as file: original = file.read() hash_object = hashlib.sha256(original) with open('hashed_file.txt', 'wb') as hashed_file: hashed_file.write(hash_object.digest()) |
Subscribe and Get Free Video Courses & Articles in your Email