Building a File Encryption and Decryption Tool with Python



Learn how to create a file encryption and decryption tool using Python. Encrypt your sensitive files using a chosen algorithm and secret key. Step-by-step tutorial with code examples using the cryptography library. 

Introduction:

In this tutorial, we will create a file encryption and decryption tool using Python. The tool will allow users to encrypt their sensitive files using a chosen encryption algorithm and a secret key. We will utilize Python's cryptography library to implement the encryption and decryption functionalities.


Prerequisites:

1. Basic understanding of Python programming.

2. Familiarity with encryption concepts and algorithms.


Step 1: Setting Up the Environment

Create a new directory for your project and navigate to it in a terminal or command prompt. Create a virtual environment:


```

$ python -m venv encryption-env

```


Activate the virtual environment:


- On Windows:

```

$ encryption-env\Scripts\activate

```


- On macOS/Linux:

```

$ source encryption-env/bin/activate

```


Step 2: Installing Dependencies

Inside the activated virtual environment, install the necessary libraries:


```

$ pip install cryptography

```


Step 3: Writing the Code

Create a new Python file in your project directory, e.g., `file_encryption.py`. Open the file in a text editor or IDE and follow along with the code below:


```python

from cryptography.fernet import Fernet


def generate_key():

    key = Fernet.generate_key()

    with open('secret.key', 'wb') as key_file:

        key_file.write(key)


def load_key():

    with open('secret.key', 'rb') as key_file:

        key = key_file.read()

    return key


def encrypt_file(key, file_path):

    cipher = Fernet(key)

    with open(file_path, 'rb') as file:

        file_data = file.read()

    encrypted_data = cipher.encrypt(file_data)

    with open(file_path, 'wb') as file:

        file.write(encrypted_data)


def decrypt_file(key, file_path):

    cipher = Fernet(key)

    with open(file_path, 'rb') as file:

        encrypted_data = file.read()

    decrypted_data = cipher.decrypt(encrypted_data)

    with open(file_path, 'wb') as file:

        file.write(decrypted_data)


# Generate a new key (run only once)

# generate_key()


# Load the key

key = load_key()


# Encrypt a file

file_path = 'example.txt'

encrypt_file(key, file_path)


# Decrypt the file

decrypt_file(key, file_path)

```


Step 4: Understanding the Code

- We import the `Fernet` class from the `cryptography.fernet` module, which provides encryption and decryption functionalities.

- The `generate_key()` function generates a new encryption key and stores it in a file called `secret.key`.

- The `load_key()` function reads the encryption key from the `secret.key` file.

- The `encrypt_file()` function encrypts a file using the provided encryption key.

- The `decrypt_file()` function decrypts a file using the encryption key.

- In the example usage section, we generate a new key (commented out after the first run), load the key, encrypt an example file, and then decrypt it.


Step 5: Running the File Encryption Tool

Save the `file_encryption.py` file and execute it from the command line:


```

$ python file_encryption.py

```


The example file will be encrypted and then decrypted, demonstrating the functionality of the tool.


Conclusion:

In this tutorial, we built a file encryption and decryption tool using Python's cryptography library. You can use this tool to encrypt and decrypt your sensitive files, providing an additional layer of security. Feel free to explore further by implementing different encryption algorithms or integrating the tool into a user interface for a more user-friendly experience.




Support My Work with a Cup of Chai !


If you are located in India, I kindly request your support through a small contribution.

Please note that the UPI payment method is only available within India.

Chai

Accepted Payment Methods: Google Pay, PhonePe, PayTM, Amazonpay  UPI 

UPI ID

haneen@postbank

 

If you are not located in India, you can still show your appreciation by sending a thank you or an Amazon gift card to the following email address:

websitehaneen@gmail.com

 

Wishing you a wonderful day!


*

Post a Comment (0)
Previous Post Next Post