Skip to main content

How to decrypt a file in Python

How to decrypt a file in Python.

Here is a detailed step-by-step tutorial on how to decrypt a file in Python:

Step 1: Import Required Libraries

First, we need to import the necessary libraries for file handling and encryption. In this tutorial, we will be using the cryptography library for encryption and decryption.

from cryptography.fernet import Fernet

Step 2: Generate or Retrieve the Encryption Key

To decrypt a file, we need the encryption key that was used to encrypt it. If you already have the encryption key, skip this step. Otherwise, you can generate a new encryption key using the following code:

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

This code generates a new encryption key and saves it to a file named encryption_key.key in binary format.

Step 3: Read the Encrypted File

Next, we need to read the encrypted file that we want to decrypt. Make sure the file exists in the specified location.

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

This code reads the content of the encrypted file and returns it as binary data.

Step 4: Decrypt the File

Now we can decrypt the file using the encryption key and the encrypted data.

def decrypt_file(key, encrypted_data):
fernet = Fernet(key)
decrypted_data = fernet.decrypt(encrypted_data)
return decrypted_data

This code initializes the Fernet class with the encryption key, then decrypts the encrypted data using the decrypt method.

Step 5: Save the Decrypted Data

Finally, we can save the decrypted data to a new file.

def save_decrypted_data(decrypted_data, output_filename):
with open(output_filename, "wb") as file:
file.write(decrypted_data)

This code writes the decrypted data to a new file specified by output_filename.

Complete Example

Here's a complete example that brings all the steps together:

from cryptography.fernet import Fernet

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

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

def decrypt_file(key, encrypted_data):
fernet = Fernet(key)
decrypted_data = fernet.decrypt(encrypted_data)
return decrypted_data

def save_decrypted_data(decrypted_data, output_filename):
with open(output_filename, "wb") as file:
file.write(decrypted_data)

# Generate or retrieve the encryption key
generate_key()

# Read the encrypted file
encrypted_data = read_file("encrypted_file.txt")

# Decrypt the file
with open("encryption_key.key", "rb") as key_file:
key = key_file.read()
decrypted_data = decrypt_file(key, encrypted_data)

# Save the decrypted data
save_decrypted_data(decrypted_data, "decrypted_file.txt")

Make sure to replace "encrypted_file.txt" with the path to your actual encrypted file, and "decrypted_file.txt" with the desired output file name.

That's it! You now have a decrypted file using the provided encryption key.