Last active
June 27, 2023 19:31
-
-
Save SirRomey/626ec0d87aed54bc8a4e8f5dfbcac993 to your computer and use it in GitHub Desktop.
This file contains 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 hashlib | |
import hashlib | |
import os | |
import tkinter as tk | |
from tkinter import filedialog | |
from Crypto.Cipher import AES | |
class EncryptionTool: | |
def __init__(self, user_file, user_key, xor_key): | |
self.user_file = user_file | |
self.input_file_size = os.path.getsize(self.user_file) | |
self.chunk_size = 1024 | |
self.total_chunks = (self.input_file_size // self.chunk_size) + 1 | |
self.user_key = bytes(user_key, "utf-8") | |
self.user_salt = bytes(user_key[::-1], "utf-8") | |
self.xor_key = int(xor_key) | |
self.file_extension = self.user_file.split(".")[-1] | |
self.hash_type = "SHA256" | |
self.encrypt_output_file = ".".join(self.user_file.split(".")[:-1]) \ | |
+ "." + self.file_extension + ".enc" | |
self.decrypt_output_file = self.user_file[:-4].split(".") | |
self.decrypt_output_file = ".".join(self.decrypt_output_file[:-1]) \ | |
+ "_decrypted." + \ | |
self.decrypt_output_file[-1] | |
self.hashed_key_salt = dict() | |
self.hash_key_salt() | |
def read_in_chunks(self, file_object, chunk_size=1024): | |
while True: | |
data = file_object.read(chunk_size) | |
if not data: | |
break | |
yield data | |
def encrypt(self): | |
cipher_object = AES.new( | |
self.hashed_key_salt["key"], | |
AES.MODE_CFB, | |
self.hashed_key_salt["salt"] | |
) | |
self.abort() | |
input_file = open(self.user_file, "rb") | |
output_file = open(self.encrypt_output_file, "ab") | |
done_chunks = 0 | |
for piece in self.read_in_chunks(input_file, self.chunk_size): | |
encrypted_content = cipher_object.encrypt(piece) | |
output_file.write(encrypted_content) | |
done_chunks += 1 | |
yield (done_chunks / self.total_chunks) * 100 | |
output_file.close() | |
input_file.close() | |
del cipher_object | |
output_file = open(self.encrypt_output_file, "rb") | |
xor_encrypt = bytearray(output_file.read()) | |
output_file.close() | |
for index, values in enumerate(xor_encrypt): | |
xor_encrypt[index] = values ^ self.xor_key | |
output_file = open(self.encrypt_output_file, "wb") | |
output_file.write(xor_encrypt) | |
output_file.close() | |
def decrypt(self): | |
cipher_object = AES.new( | |
self.hashed_key_salt["key"], | |
AES.MODE_CFB, | |
self.hashed_key_salt["salt"] | |
) | |
self.abort() | |
input_file = open(self.user_file, "rb") | |
output_file = open(self.decrypt_output_file, "xb") | |
done_chunks = 0 | |
xor_decrypt = bytearray(input_file.read()) | |
input_file.close() | |
for index, values in enumerate(xor_decrypt): | |
xor_decrypt[index] = values ^ self.xor_key | |
temp_file = open("temp_file", "wb") | |
temp_file.write(xor_decrypt) | |
temp_file.close() | |
temp_file = open("temp_file", "rb") | |
for piece in self.read_in_chunks(temp_file): | |
decrypted_content = cipher_object.decrypt(piece) | |
output_file.write(decrypted_content) | |
done_chunks += 1 | |
yield (done_chunks / self.total_chunks) * 100 | |
temp_file.close() | |
output_file.close() | |
os.remove("temp_file") | |
del cipher_object | |
def abort(self): | |
if os.path.isfile(self.encrypt_output_file): | |
os.remove(self.encrypt_output_file) | |
if os.path.isfile(self.decrypt_output_file): | |
os.remove(self.decrypt_output_file) | |
def hash_key_salt(self): | |
hasher = hashlib.new(self.hash_type) | |
hasher.update(self.user_key) | |
self.hashed_key_salt["key"] = bytes(hasher.hexdigest()[:32], "utf-8") | |
del hasher | |
hasher = hashlib.new(self.hash_type) | |
hasher.update(self.user_salt) | |
self.hashed_key_salt["salt"] = bytes(hasher.hexdigest()[:16], "utf-8") | |
del hasher | |
class MainWindow: | |
def __init__(self, root): | |
self.root = root | |
self._cipher = None | |
self._file_url = tk.StringVar() | |
self._xor_key = tk.StringVar() | |
self._secret_key = tk.StringVar() | |
self._status = tk.StringVar() | |
self._status.set("---") | |
root.title("Image Encrypter/Decrypter") | |
self.file_entry_label = tk.Label( | |
root, | |
text="Choose Image File", | |
anchor=tk.W | |
) | |
self.file_entry_label.grid( | |
padx=12, | |
pady=(8, 0), | |
ipadx=0, | |
ipady=1, | |
row=0, | |
column=0, | |
columnspan=4, | |
sticky=tk.W + tk.E + tk.N + tk.S | |
) | |
self.file_entry = tk.Entry( | |
root, | |
textvariable=self._file_url, | |
exportselection=0, | |
) | |
self.file_entry.grid( | |
padx=15, | |
pady=6, | |
ipadx=8, | |
ipady=8, | |
row=1, | |
column=0, | |
columnspan=4, | |
sticky=tk.W + tk.E + tk.N + tk.S | |
) | |
self.select_btn = tk.Button( | |
root, | |
text="SELECT FILE", | |
command=self.selectfile_callback, | |
width=42, | |
) | |
self.select_btn.grid( | |
padx=15, | |
pady=8, | |
ipadx=24, | |
ipady=6, | |
row=2, | |
column=0, | |
columnspan=4, | |
sticky=tk.W + tk.E + tk.N + tk.S | |
) | |
self.key_entry_label = tk.Label( | |
root, | |
text="Enter Secret Key", | |
anchor=tk.W | |
) | |
self.key_entry_label.grid( | |
padx=12, | |
pady=(8, 0), | |
ipadx=0, | |
ipady=1, | |
row=3, | |
column=0, | |
columnspan=4, | |
sticky=tk.W + tk.E + tk.N + tk.S | |
) | |
self.key_entry = tk.Entry( | |
root, | |
textvariable=self._secret_key, | |
exportselection=0, | |
) | |
self.key_entry.grid( | |
padx=15, | |
pady=6, | |
ipadx=8, | |
ipady=8, | |
row=4, | |
column=0, | |
columnspan=4, | |
sticky=tk.W + tk.E + tk.N + tk.S | |
) | |
self.xor_key_entry_label = tk.Label( | |
root, | |
text="Enter XOR Key", | |
anchor=tk.W | |
) | |
self.xor_key_entry_label.grid( | |
padx=12, | |
pady=(8, 0), | |
ipadx=0, | |
ipady=1, | |
row=5, | |
column=0, | |
columnspan=4, | |
sticky=tk.W + tk.E + tk.N + tk.S | |
) | |
self.xor_key_entry = tk.Entry( | |
root, | |
textvariable=self._xor_key, | |
exportselection=0, | |
) | |
self.xor_key_entry.grid( | |
padx=15, | |
pady=6, | |
ipadx=8, | |
ipady=8, | |
row=6, | |
column=0, | |
columnspan=4, | |
sticky=tk.W + tk.E + tk.N + tk.S | |
) | |
self.encrypt_btn = tk.Button( | |
root, | |
text="ENCRYPT", | |
command=self.encrypt_callback, | |
) | |
self.encrypt_btn.grid( | |
padx=(15, 6), | |
pady=8, | |
ipadx=24, | |
ipady=6, | |
row=7, | |
column=0, | |
columnspan=2, | |
sticky=tk.W + tk.E + tk.N + tk.S | |
) | |
self.decrypt_btn = tk.Button( | |
root, | |
text="DECRYPT", | |
command=self.decrypt_callback, | |
) | |
self.decrypt_btn.grid( | |
padx=(6, 15), | |
pady=8, | |
ipadx=24, | |
ipady=6, | |
row=7, | |
column=2, | |
columnspan=2, | |
sticky=tk.W + tk.E + tk.N + tk.S | |
) | |
self.reset_btn = tk.Button( | |
root, | |
text="RESET", | |
command=self.reset_callback, | |
) | |
self.reset_btn.grid( | |
padx=15, | |
pady=(4, 12), | |
ipadx=24, | |
ipady=6, | |
row=8, | |
column=0, | |
columnspan=4, | |
sticky=tk.W + tk.E + tk.N + tk.S | |
) | |
self.status_label = tk.Label( | |
root, | |
textvariable=self._status, | |
anchor=tk.W, | |
justify=tk.LEFT, | |
wraplength=350 | |
) | |
self.status_label.grid( | |
padx=12, | |
pady=(0, 12), | |
ipadx=0, | |
ipady=1, | |
row=9, | |
column=0, | |
columnspan=4, | |
sticky=tk.W + tk.E + tk.N + tk.S | |
) | |
tk.Grid.columnconfigure(root, 0, weight=1) | |
tk.Grid.columnconfigure(root, 1, weight=1) | |
tk.Grid.columnconfigure(root, 2, weight=1) | |
tk.Grid.columnconfigure(root, 3, weight=1) | |
def selectfile_callback(self): | |
try: | |
name = filedialog.askopenfile() | |
self._file_url.set(name.name) | |
except Exception as e: | |
self._status.set(e) | |
self.status_label.update() | |
def encrypt_callback(self): | |
try: | |
self._cipher = EncryptionTool( | |
self._file_url.get(), | |
self._secret_key.get(), | |
self._xor_key.get() | |
) | |
for percentage in self._cipher.encrypt(): | |
percentage = "{0:.2f}%".format(percentage) | |
self._status.set(percentage) | |
self.status_label.update() | |
self._status.set("File Encrypted!") | |
self._cipher = None | |
except Exception as e: | |
self._status.set(e) | |
def decrypt_callback(self): | |
try: | |
self._cipher = EncryptionTool( | |
self._file_url.get(), | |
self._secret_key.get(), | |
self._xor_key.get() | |
) | |
for percentage in self._cipher.decrypt(): | |
percentage = "{0:.2f}%".format(percentage) | |
self._status.set(percentage) | |
self.status_label.update() | |
self._status.set("File Decrypted!") | |
self._cipher = None | |
except Exception as e: | |
self._status.set(e) | |
def reset_callback(self): | |
self._cipher = None | |
self._file_url.set("") | |
self._secret_key.set("") | |
self._status.set("---") | |
self._xor_key.set("") | |
if __name__ == "__main__": | |
ROOT = tk.Tk() | |
MAIN_WINDOW = MainWindow(ROOT) | |
ROOT.mainloop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment