Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- # Filename: openssl_toolkit.py
- # Version: 1.0.0
- # Author: Jeoi Reqi
- """
- This script provides basic functionality for working with OpenSSL, including checking if OpenSSL is installed, installing it if necessary,
- encrypting and decrypting files using AES-256-CBC encryption, generating RSA private keys and self-signed certificates, and generating
- sample text files for testing purposes. Additionally, it allows viewing the content of an encrypted file without decrypting it, and prompts
- the user to select a file for encryption or decryption using a file dialog.
- """
- import subprocess
- import shutil
- import sys
- from tkinter import filedialog, Tk
- def check_openssl():
- """
- Check if OpenSSL is installed on the system. If not, prompt the user to install it.
- """
- try:
- subprocess.run(['openssl', 'version'], check=True, timeout=10)
- except FileNotFoundError:
- print("\nOpenSSL is not installed on your system.\n")
- choice = input("\nDo you want to install OpenSSL?\nOPTIONS:\n1: YES\2: NO\nMake Your Selection (1 or 2): ")
- if choice.lower() == '1':
- install_openssl()
- else:
- print("\nExiting Without Saving...\n")
- sys.exit(1)
- except subprocess.TimeoutExpired:
- print("\nOpenSSL command timed out. Make sure OpenSSL is installed and try again.\n")
- sys.exit(1)
- except subprocess.CalledProcessError:
- print("\nAn error occurred while checking OpenSSL version.\n")
- sys.exit(1)
- def install_openssl():
- """
- Install OpenSSL using Chocolatey on Windows or manually on other platforms.
- """
- if shutil.which('choco'):
- try:
- subprocess.run(['choco', 'install', 'openssl.light'], timeout=300, check=True)
- print("\nOpenSSL installed successfully using Chocolatey.\n")
- except subprocess.TimeoutExpired:
- print("\nInstallation timed out. Try again later.\n")
- sys.exit(1)
- except subprocess.CalledProcessError:
- print("\nError occurred during installation. Please try again or install manually.\n")
- sys.exit(1)
- else:
- print("\nUnable to install OpenSSL automatically.\n")
- if sys.platform == 'win32':
- print("\nTo install OpenSSL on Windows, you'll need Chocolatey.\n")
- print("\nPlease install Chocolatey from https://chocolatey.org/ and then run the following command in your command prompt:\n")
- print("\nchoco install openssl.light\n")
- else:
- print("\nPlease install OpenSSL manually using your system's package manager or by downloading it from https://wiki.openssl.org/index.php/Binaries\n")
- def encrypt_file(input_file, output_file):
- """
- Encrypt a file using AES-256-CBC encryption with salt and PBKDF2 key derivation.
- Parameters:
- input_file (str): Path to the input file.
- output_file (str): Path to the output encrypted file.
- """
- try:
- subprocess.run(['openssl', 'enc', '-aes-256-cbc', '-salt', '-pbkdf2', '-in', input_file, '-out', output_file, '-k', 'password'], check=True)
- print("\nFile encrypted successfully.\n")
- except subprocess.CalledProcessError:
- print("\nAn error occurred during encryption.\n")
- def decrypt_file(input_file, output_file):
- """
- Decrypt a file encrypted with AES-256-CBC encryption using salt and PBKDF2 key derivation.
- Parameters:
- input_file (str): Path to the input encrypted file.
- output_file (str): Path to the output decrypted file.
- """
- try:
- subprocess.run(['openssl', 'enc', '-d', '-aes-256-cbc', '-pbkdf2', '-in', input_file, '-out', output_file, '-k', 'password'], check=True)
- print("\nFile decrypted successfully.\n")
- except subprocess.CalledProcessError:
- print("\nAn error occurred during decryption.\n")
- 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
- def generate_test_files():
- """
- Generate sample text files for testing encryption and decryption.
- """
- with open("sample_e.txt", "w") as file:
- file.write("This is a sample text file used for testing encryption.")
- with open("sample_d.txt", "w") as file:
- file.write("This is a sample text file used for testing decryption.")
- def view_encrypted_content(input_file):
- """
- View the content of an encrypted file without decrypting it.
- Parameters:
- input_file (str): Path to the input encrypted file.
- """
- try:
- with open(input_file, "rb") as f:
- encrypted_data = f.read()
- print("\nEncrypted content:\n", encrypted_data)
- except FileNotFoundError:
- print("\nFile not found.\n")
- except Exception as e:
- print("\nAn error occurred:", e)
- check_openssl()
- print("\nWelcome To OpenSSL Fundamentals!\n")
- while True:
- print("\nPlease select an option:")
- print("1. Generate a RSA private key")
- print("2. Generate a self-signed certificate")
- print("3. Encrypt a file")
- print("4. Decrypt a file")
- print("5. Generate test files")
- print("6. View encrypted file content")
- print("7. Exit")
- option = input()
- if option == '1':
- print("\nGenerating RSA private key...\n")
- try:
- subprocess.run(['openssl', 'genrsa', '-out', 'private.key', '2048'], timeout=30, check=True)
- print("\nRSA private key generated successfully.\n")
- except subprocess.TimeoutExpired:
- print("\nOperation timed out. Try again later.\n")
- except subprocess.CalledProcessError:
- print("\nAn error occurred during RSA key generation.\n")
- elif option == '2':
- print("\nGenerating self-signed certificate...\n")
- try:
- subprocess.run(['openssl', 'req', '-x509', '-newkey', 'rsa:2048', '-keyout', 'private.key', '-out', 'certificate.crt', '-days', '365', '-nodes', '-subj', '/C=US'], timeout=30, check=True)
- print("\nSelf-signed certificate generated successfully.\n")
- except subprocess.TimeoutExpired:
- print("\nOperation timed out. Try again later.\n")
- except subprocess.CalledProcessError:
- print("\nAn error occurred during certificate generation.\n")
- elif option == '3':
- input_file = select_file()
- if input_file:
- encrypted_file = input("\nEnter the output file name without extension (encrypted): ") + ".enc"
- print("\nEncrypting file...\n")
- encrypt_file(input_file, encrypted_file)
- elif option == '4':
- input_file = select_file()
- if input_file:
- output_file = input("\nEnter the output file name without extension (decrypted): ") + ".txt"
- print("\nDecrypting file...\n")
- decrypt_file(input_file, output_file)
- elif option == '5':
- print("\nGenerating test files...\n")
- generate_test_files()
- print("\nTest files generated successfully.\n")
- elif option == '6':
- input_file = select_file()
- if input_file:
- view_encrypted_content(input_file)
- elif option == '7':
- print("\nExiting...\n")
- break
- else:
- print("\nInvalid option. Please select a valid option.\n")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement