How to Create a Simple File Encryptor/Decryptor

85 views

How to Create a Simple File Encryptor/Decryptor

How to Create a Simple File Encryptor/Decryptor

I want to create a simple file encryptor and decryptor using Python. How can I do this?

 

solveurit24@gmail.com Changed status to publish February 16, 2025
0

You can use the os and base64 modules for a basic encryption/decryption mechanism.

Code Example:

import os
import base64

def encrypt_file(file_name):
    with open(file_name, 'rb') as file:
        data = file.read()
        encrypted = base64.b64encode(data)
    with open(file_name + '_encrypted', 'wb') as file:
        file.write(encrypted)

def decrypt_file(file_name):
    with open(file_name, 'rb') as file:
        data = file.read()
        decrypted = base64.b64decode(data)
    with open(file_name.replace('_encrypted', '_decrypted'), 'wb') as file:
        file.write(decrypted)

# Example usage
encrypt_file("example.txt")
decrypt_file("example.txt_encrypted")


This code provides basic file encryption and decryption using Base64 encoding.

solveurit24@gmail.com Changed status to publish February 16, 2025
0
You are viewing 1 out of 1 answers, click here to view all answers.