Advertisement
Python253

file_encrypt_decrypt

Apr 7th, 2024
692
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.81 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # Filename: file_encrypt_decrypt.py
  4. # Version: 1.0.0
  5. # Author: Jeoi Reqi
  6.  
  7. """
  8. This script provides functions for encrypting and decrypting files using OpenSSL's AES-256-CBC encryption algorithm
  9. with salt and PBKDF2 key derivation. It also includes checks to create the necessary files if they do not exist.
  10. Additionally, it prompts the user to select a file for encryption or decryption using a file dialog.
  11. """
  12.  
  13. import subprocess
  14. import os
  15. from tkinter import filedialog, Tk
  16.  
  17. def encrypt_file(input_file, output_file):
  18.     """
  19.    Encrypts a file using AES-256-CBC encryption with salt and PBKDF2 key derivation.
  20.    
  21.    Parameters:
  22.        input_file (str): The path to the input file to be encrypted.
  23.        output_file (str): The path to save the encrypted output file.
  24.    
  25.    Returns:
  26.        None
  27.    """
  28.     try:
  29.         subprocess.run(['openssl', 'enc', '-aes-256-cbc', '-salt', '-pbkdf2', '-in', input_file, '-out', output_file, '-k', 'password'], check=True)
  30.         print("File encrypted successfully.")
  31.     except subprocess.CalledProcessError:
  32.         print("An error occurred during encryption.")
  33.  
  34. def decrypt_file(input_file, output_file):
  35.     """
  36.    Decrypts a file encrypted with AES-256-CBC encryption using salt and PBKDF2 key derivation.
  37.    
  38.    Parameters:
  39.        input_file (str): The path to the encrypted input file to be decrypted.
  40.        output_file (str): The path to save the decrypted output file.
  41.    
  42.    Returns:
  43.        None
  44.    """
  45.     try:
  46.         subprocess.run(['openssl', 'enc', '-d', '-aes-256-cbc', '-pbkdf2', '-in', input_file, '-out', output_file, '-k', 'password'], check=True)
  47.         print("File decrypted successfully.")
  48.     except subprocess.CalledProcessError:
  49.         print("An error occurred during decryption.")
  50.  
  51. def select_file():
  52.     """
  53.    Opens a file dialog to select a file for encryption or decryption.
  54.    
  55.    Returns:
  56.        str: The path to the selected file.
  57.    """
  58.     root = Tk()
  59.     root.withdraw()  # Hide the main window
  60.     file_path = filedialog.askopenfilename()  # Open file dialog
  61.     return file_path
  62.  
  63. # Example usage:
  64. # Ask user if they want to encrypt or decrypt
  65. operation = input("Enter 'E' to encrypt or 'D' to decrypt: ").upper()
  66.  
  67. if operation == 'E':
  68.     # Encrypting file
  69.     input_file = select_file()  # Select file using file dialog
  70.     if input_file:
  71.         output_file = input("Enter the filename for the encrypted file (without extension): ") + ".enc"
  72.         encrypt_file(input_file, output_file)
  73. else:
  74.     # Decrypting file
  75.     input_file = select_file()  # Select file using file dialog
  76.     if input_file:
  77.         output_file = input("Enter the filename for the decrypted file (without extension): ")
  78.         decrypt_file(input_file, output_file)
  79.  
  80.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement