Sai A Sai A
Updated date Feb 07, 2024
In this blog, we will learn how to encrypt and decrypt files using Python. Follow step-by-step instructions and explanations to protect your sensitive information from unauthorized access.

Introduction:

In today's world, securing sensitive information is more important than ever. Whether it is personal documents, financial records, or confidential communications, keeping our files safe from prying eyes is important. One effective way to achieve this is through file encryption, a process that converts plaintext into ciphertext, making it unreadable without the proper decryption key. In this blog, we will explore how to create a simple file encryption program in Python.

Method 1: Generating Encryption Keys

The first step in our file encryption process is generating encryption keys. Encryption keys are essentially strings of characters used to encrypt and decrypt data. For our program, we'll use the cryptography library in Python, which provides robust encryption algorithms.

from cryptography.fernet import Fernet

def generate_key():
    key = Fernet.generate_key()
    with open("encryption_key.txt", "wb") as key_file:
        key_file.write(key)

generate_key()

Output:

This program will generate an encryption key and save it to a file named "encryption_key.txt". The key will look like a random string of characters and will be used for both encryption and decryption.

In this code snippet, we import the Fernet class from the cryptography.fernet module. We then define a function generate_key() that generates a random encryption key using the Fernet.generate_key() method. Finally, we write the generated key to a file named "encryption_key.txt" in binary mode ("wb").

Method 2: Encrypting a File

Once we have our encryption key, we can proceed to encrypt a file. For this demonstration, let's encrypt a text file named "sensitive_data.txt".

def encrypt_file(filename, key):
    with open(filename, "rb") as file:
        data = file.read()

    fernet = Fernet(key)
    encrypted_data = fernet.encrypt(data)

    with open(filename + ".encrypted", "wb") as encrypted_file:
        encrypted_file.write(encrypted_data)

encrypt_file("sensitive_data.txt", key)

Output:

This program will encrypt the contents of "sensitive_data.txt" using the encryption key generated earlier. The encrypted data will be saved to a new file named "sensitive_data.txt.encrypted".

In this code snippet, we define a function encrypt_file() that takes the filename and encryption key as arguments. We open the specified file in binary mode ("rb") and read its contents into the data variable. Then, we create an instance of the Fernet class with the provided encryption key. We use this instance to encrypt the data using the encrypt() method, resulting in encrypted_data. Finally, we write the encrypted data to a new file with a ".encrypted" extension.

Method 3: Decrypting a File

Decrypting a file is the reverse process of encryption. We will use the same encryption key to decrypt the file back to its original form.

def decrypt_file(filename, key):
    with open(filename, "rb") as file:
        encrypted_data = file.read()

    fernet = Fernet(key)
    decrypted_data = fernet.decrypt(encrypted_data)

    with open(filename[:-10], "wb") as decrypted_file:  # Remove ".encrypted" extension
        decrypted_file.write(decrypted_data)

decrypt_file("sensitive_data.txt.encrypted", key)

Output:

This program will decrypt the contents of "sensitive_data.txt.encrypted" using the encryption key generated earlier. The decrypted data will be saved to a new file named "sensitive_data.txt".

In this code snippet, we define a function decrypt_file() that takes the filename and encryption key as arguments. We open the encrypted file in binary mode ("rb") and read its contents into the encrypted_data variable. Then, we create an instance of the Fernet class with the provided encryption key. We use this instance to decrypt the data using the decrypt() method, resulting in decrypted_data. Finally, we write the decrypted data to a new file with the ".encrypted" extension removed.

Conclusion:

File encryption is a powerful technique for safeguarding sensitive information from unauthorized access. With the Python program provided in this blog, you can easily encrypt and decrypt your files using strong encryption algorithms. By following the step-by-step instructions and understanding the underlying concepts, you can enhance the security of your digital assets and protect your privacy online.

Comments (0)

There are no comments. Be the first to comment!!!