Skip to content

Commit

Permalink
message input as external editor
Browse files Browse the repository at this point in the history
  • Loading branch information
caner-cetin committed Apr 24, 2022
1 parent 863a5fc commit bf7565f
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .idea/A10_cipher.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

129 changes: 115 additions & 14 deletions a10cipher.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
from pathlib import Path
import tkinter as tk
from tkinter import filedialog


import click
class Encryptor():
def __init__(self):
self.AAAAA_code_DICT_lower = {
Expand Down Expand Up @@ -63,8 +62,10 @@ def decrypt_msg(self, msg):
deciphered += "\n"
placeholder = s.replace("\n", "")
s = placeholder
else:
pass
if "NEW_WORD" in s:
deciphered += " "
placeholder = s.replace("NEW_WORD", "")
s = placeholder
if s in lowerdict.keys():
deciphered += lowerdict[s]
elif s in upperdict.keys():
Expand Down Expand Up @@ -193,6 +194,14 @@ def encrypt_txt_files(file):
encryptor.encrypt_file(file_data, file)


def encrypt_messages(message):
encryptor = Encryptor()
ciphered = encryptor.encrypt_msg(message)
with open("message.txt", "w") as f:
f.write(ciphered)
return ciphered


def decrypt_txt_folder(dir):
# Find subfolders
for folder in os.listdir(dir):
Expand All @@ -207,7 +216,13 @@ def decrypt_txt_folder(dir):
with open(path, "r") as f:
file_data = f.read()
encryptor.decrypt_file(file_data, path)

def decrypt_messages(message):
encryptor = Encryptor()
deciphered = encryptor.decrypt_msg(message)
print(deciphered)
with open("message.txt", "w") as f:
f.write(deciphered)
return deciphered

def decrypt_txt_file(file):
encryptor = Encryptor()
Expand Down Expand Up @@ -257,27 +272,52 @@ def __call__(self, path):
print("Please enter a command")
print("1. Encrypt")
print("2. Decrypt")
print("3. Exit")
print("3. Display Encrypted Messages")
print("4. Exit")
choice = input()
if choice == "1":
print("Please choose if you want to encrypt a file or a folder")
print("1. File")
print("2. Folder")
print("3. Single Message")
choice = input()
if choice == "1":
root = tk.Tk()
root.withdraw()
root.title("Please choose a file")
file_path = filedialog.askopenfilename()
encrypt_txt_files(file_path)
flag = False
print("Encryption complete")
print("Press 3 to exit, press 2 to return to main menu")
choice = input()
if choice == "3":
flag = False
elif choice == "2":
flag = True
elif choice == "2":
root = tk.Tk()
root.withdraw()
root.title("Please choose a dir")
folder_path = filedialog.askdirectory()
encrypt_txt_folder(folder_path)
flag = False
print("Encryption complete")
print("Press 3 to exit, press 2 to return to main menu")
choice = input()
if choice == "3":
flag = False
elif choice == "2":
flag = True
elif choice == "3":
initial_message = b"#Enter the message you want to encrypt, save and close the editor when you are done."
edited_message = click.edit(initial_message)
encrypt_messages(edited_message)
print("Encrytion complete, encrypted text is in the file 'message.txt'")
print("Press 3 to exit, press 2 to return to main menu")
choice = input()
if choice == "3":
flag = False
elif choice == "2":
flag = True
else:
print("Please enter a valid choice")
elif choice == "2":
Expand All @@ -287,23 +327,84 @@ def __call__(self, path):
choice = input()
if choice == "1":
root = tk.Tk()
root.title("Please choose a file")
root.withdraw()
file_path = filedialog.askopenfilename()
decrypt_txt_file(file_path)
flag = False
print("Decryption complete")
print("Press 3 to exit, press 2 to return to main menu")
choice = input()
if choice == "3":
flag = False
elif choice == "2":
flag = True
elif choice == "2":
root = tk.Tk()
root.withdraw()
root.title("Please choose a dir")
folder_path = filedialog.askdirectory()
decrypt_txt_folder(folder_path)
flag = False
print("Decryption complete")
print("Press 3 to exit, press 2 to return to main menu")
choice = input()
if choice == "3":
flag = False
elif choice == "2":
flag = True
else:
print("Please enter a valid choice")
elif choice == "3":
flag = False
sys.exit()
print("Please pick the message or file you want to display")
print("1. Message")
print("2. File")
choice = input()
if choice == "1":
root = tk.Tk()
root.withdraw()
file_path = filedialog.askopenfilename()
with open(file_path, "r") as f:
print(f.read())
print("Press 1 to decrypt the text, press 2 to return to main menu, 3 to exit.")
choice = input()
if choice == "1":
decrypt_messages(file_path)
print("Decryption complete")
print("Decrypted text has overwritten the original file")
print("Press 3 to exit, press 2 to return to main menu")
choice = input()
if choice == "3":
flag = False
elif choice == "2":
flag = True
if choice == "2":
flag = True
if choice == "3":
sys.exit()
elif choice == "2":
root = tk.Tk()
root.withdraw()
root.title("Please choose a file")
file_path = filedialog.askopenfilename()
with open(file_path, "r") as f:
print(f.read())
print("Press 1 to decrypt the text, press 2 to return to main menu, 3 to exit.")
choice = input()
if choice == "1":
decrypt_txt_file(file_path)
print("Decryption complete")
print("Decrypted text has overwritten the original file")
print("Press 3 to exit, press 2 to return to main menu")
choice = input()
if choice == "3":
flag = False
elif choice == "2":
flag = True
if choice == "2":
flag = True
if choice == "3":
sys.exit()

elif choice == "4":
flag = False
sys.exit()
generate_key = args.generate_key
directory_ = args.directory
encrypt_ = args.encrypt
Expand Down

0 comments on commit bf7565f

Please sign in to comment.