Created
June 5, 2023 18:20
-
-
Save shadowdevnotreal/c60b707c2d788674e4b2f07e8e25825f to your computer and use it in GitHub Desktop.
file crypt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import zipfile | |
| import hashlib | |
| import os | |
| import tkinter as tk | |
| def encrypt_and_destroy(): | |
| # Compress the folder using Zip file format | |
| root = tk.Tk() | |
| input_folder = tk.filedialog.askdirectory(parent=root, initialdir="/", title="Select a folder to encrypt") | |
| root.destroy() | |
| output_file = input("Enter the name of the output file: ") | |
| password = input("Enter a password to encrypt the file: ") | |
| zipf = zipfile.ZipFile(output_file, 'w', zipfile.ZIP_DEFLATED) | |
| for root, dirs, files in os.walk(input_folder): | |
| for file in files: | |
| zipf.write(os.path.join(root, file)) | |
| zipf.close() | |
| # Hash the password using a secure cryptographic algorithm | |
| salt = os.urandom(16) | |
| hashed_password = hashlib.sha512(password.encode('utf-8') + salt).hexdigest() | |
| # Prompt user for password and compare with stored hash | |
| user_password1 = input("Enter password to encrypt file: ") | |
| user_password2 = input("Re-enter password to encrypt file: ") | |
| if user_password1 != user_password2: | |
| print("Passwords do not match.") | |
| return | |
| if hashed_password != hashlib.sha512(user_password1.encode('utf-8') + salt).hexdigest(): | |
| # If password is incorrect, delete the compressed file | |
| os.remove(output_file) | |
| print("Wrong password entered. File has been deleted.") | |
| return | |
| # If password is correct, decompress and decrypt the file | |
| zipf = zipfile.ZipFile(output_file) | |
| for file in zipf.namelist(): | |
| zipf.extract(file, path=output_folder) | |
| zipf.close() | |
| print("File successfully decrypted.") | |
| if __name__ == "__main__": | |
| encrypt_and_destroy() |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The encrypt_and_destroy() function first prompts the user to select a folder to encrypt.
The function then prompts the user to enter a password to encrypt the folder.
The function then hashes the password using the SHA-512 algorithm and stores the hash in a variable.
The function then prompts the user to re-enter the password.
The function then compares the two passwords. If the passwords do not match, the function prints an error message and returns.
If the passwords match, the function decompresses and decrypts the file.
The function then prints a message stating that the file has been successfully decrypted.
The if name == "main": statement ensures that the encrypt_and_destroy() function is only executed when the file is run as a script.
A filedialog.askdirectory() function was added to the encrypt_and_destroy() function. This function opens a file selection dialogue box and allows the user to select the folder they want to encrypt.
The input_folder variable was changed to a global variable. This was done so that the encrypt_and_destroy() function can access the value of the input_folder variable.