Protecting sensitive files is important nowadays, particularly when handling sensitive data such as client details, financial statements, or individual documents. Python offers an easy but effective method of managing data protection via encryption and decryption. Knowing how to encrypt and decrypt files using Python assists programmers in protecting files prior to storage or sharing via the internet.
Suppose you are developing an application to store employee information or customer bills. In the absence of encryption, anyone who comes to access those files can misuse the information. By using Python encryption, you guarantee that even if the file becomes compromised, the information remains inaccessible to others.
Use Cases for File Encryption and Decryption
Apply Python file encryption and decryption to various real-world use cases:
- Secure storage: Encrypt data before storing it on a cloud server to keep it safe even if the storage gets compromised.
- Finance applications: Encrypt transaction logs or reports to protect sensitive figures from exposure.
- Medical data: Healthcare facilities can encrypt patient records before exchanging them between systems.
- Business communication: Encrypted confidential documents avoid data exposure in organizations.
Let’s assume you have file which needs to secure. But for our example, Let’s create a simple text file for encryption and decryption in python. Create sample.txt file into same directory as your script with “Codewolfy” as content.
Encrypt File in Python
You can easily encrypt a files in Python using the Fernet module from the cryptography library. Python has in-built libraries for securing data through encryption. It uses symmetric encryption, meaning the same key encrypts and decrypts the files.
Let’s take a simple example to encrypt our sample.txt file using python’s fernet library of cryptography module. Create new python file by enc.py and add below code into it:
from cryptography.fernet import Fernet
key = Fernet.generate_key()
fernet = Fernet(key)
with open('sample.txt', 'rb') as file:
original = file.read()
encrypted = fernet.encrypt(original)
with open('encrypted_sample.txt', 'wb') as encrypted_file:
encrypted_file.write(encrypted)
As per above example, we will import Fernet from cryptography module then generate encryption key. Once key is generated, it will be used to encrypt and decrypt file content.
Here, we are loading all content from file into original variable and then encrypting it. After encryption, Python creates a new file to store the encrypted content like below.
gAAAAABo_OVxeCgvlI9DGc_J12akoYbiFZ46NmBNsJdegMmbbM7hDhdzHKJ1_UyjR66FtmOhvwBwu1_ROtLfesG3Vm2RVfrHTg==
However, you can use same file to update it’s content too. Use the key to access the protected file, which you can decrypt only with the same key.
Decrypt File in Python
Decrypting a file in Python reverses the encryption process using the same Fernet key. In decrypt process, you don’t have to generate new key. Let’s modify our code to decrypt file in python:
from cryptography.fernet import Fernet
fernet = Fernet(key)
with open('encrypted_sample.txt', 'rb') as enc_file:
encrypted = enc_file.read()
decrypted = fernet.decrypt(encrypted)
with open('decrypted_sample.txt', 'wb') as dec_file:
dec_file.write(decrypted)
When the decryption key matches the encryption key, the file content is restored to its readable form. It ensures data privacy while maintaining easy access for authorized users.
Conclusion
Getting to encrypt and decrypt files in Python provides you with total command over data security. It is a critical skill for developers dealing with sensitive information or creating secure applications. If you are working on backups, communication devices, or cash applications, employing Python encryption secures your files and makes them trustworthy.
If you’re working with data in Python, you might also want to learn how to Save Data to CSV File in Python. This guide walks you through creating and storing data efficiently in CSV format, making data management easier for your projects.
