Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- # Filename: file_encrypt_decrypt.py
- # Version: 1.0.0
- # Author: Jeoi Reqi
- """
- This script provides functions for encrypting and decrypting files using OpenSSL's AES-256-CBC encryption algorithm
- with salt and PBKDF2 key derivation. It also includes checks to create the necessary files if they do not exist.
- Additionally, it prompts the user to select a file for encryption or decryption using a file dialog.
- """
- import subprocess
- import os
- from tkinter import filedialog, Tk
- def encrypt_file(input_file, output_file):
- """
- Encrypts a file using AES-256-CBC encryption with salt and PBKDF2 key derivation.
- Parameters:
- input_file (str): The path to the input file to be encrypted.
- output_file (str): The path to save the encrypted output file.
- Returns:
- None
- """
- try:
- subprocess.run(['openssl', 'enc', '-aes-256-cbc', '-salt', '-pbkdf2', '-in', input_file, '-out', output_file, '-k', 'password'], check=True)
- print("File encrypted successfully.")
- except subprocess.CalledProcessError:
- print("An error occurred during encryption.")
- def decrypt_file(input_file, output_file):
- """
- Decrypts a file encrypted with AES-256-CBC encryption using salt and PBKDF2 key derivation.
- Parameters:
- input_file (str): The path to the encrypted input file to be decrypted.
- output_file (str): The path to save the decrypted output file.
- Returns:
- None
- """
- try:
- subprocess.run(['openssl', 'enc', '-d', '-aes-256-cbc', '-pbkdf2', '-in', input_file, '-out', output_file, '-k', 'password'], check=True)
- print("File decrypted successfully.")
- except subprocess.CalledProcessError:
- print("An error occurred during decryption.")
- def select_file():
- """
- Opens a file dialog to select a file for encryption or decryption.
- Returns:
- str: The path to the selected file.
- """
- root = Tk()
- root.withdraw() # Hide the main window
- file_path = filedialog.askopenfilename() # Open file dialog
- return file_path
- # Example usage:
- # Ask user if they want to encrypt or decrypt
- operation = input("Enter 'E' to encrypt or 'D' to decrypt: ").upper()
- if operation == 'E':
- # Encrypting file
- input_file = select_file() # Select file using file dialog
- if input_file:
- output_file = input("Enter the filename for the encrypted file (without extension): ") + ".enc"
- encrypt_file(input_file, output_file)
- else:
- # Decrypting file
- input_file = select_file() # Select file using file dialog
- if input_file:
- output_file = input("Enter the filename for the decrypted file (without extension): ")
- decrypt_file(input_file, output_file)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement