diff --git a/scripts/classes/Cli.py b/scripts/classes/Cli.py index d75388b..087c2af 100644 --- a/scripts/classes/Cli.py +++ b/scripts/classes/Cli.py @@ -19,7 +19,7 @@ class Cli(object): for line in stdout: self.output.append(line.decode("utf-8")) if process.wait() > bool(0): - raise Exception("Error for: \nCommand:<<" + command + ">>\nOutput:<<" + out + ">>\nExitcode:<<" + err + ">>") + raise Exception("Error for: \nCommand:<<" + str(command) + ">>\nOutput:<<" + str(out) + ">>\nExitcode:<<" + str(err) + ">>") return self.output def getOutputString(self): diff --git a/scripts/classes/Decryption.py b/scripts/classes/Decryption.py index 033fcba..e6cbb42 100644 --- a/scripts/classes/Decryption.py +++ b/scripts/classes/Decryption.py @@ -1,5 +1,6 @@ from .AbstractSplittedSecret import AbstractSplittedSecret import json +from pathlib import Path class Decryption(AbstractSplittedSecret): def __init__(self): @@ -16,6 +17,12 @@ class Decryption(AbstractSplittedSecret): self.user_data = self.loadJsonFile(self.user_file_decrypted_path) self.initializeNeededDecryptersAmount() self.initializeValidDecrypterIds() + + def initializeGroupDataEncryption(self): + self.group_name = self.getDecryptersGroupName() + self.encrypted_group_file_path = self.getGroupFilePath(self.group_name, AbstractSplittedSecret.TYPE_DECRYPTED) + self.decryptGroupFile() + self.master_password = self.loadTxtFile(self.encrypted_group_file_path) def initializeNeededDecryptersAmount(self): self.needed_decrypters_amount = len(str(list(self.user_data['groups'].keys())[0])) @@ -40,12 +47,15 @@ class Decryption(AbstractSplittedSecret): def addPasswordShare(self,user_id,password_share): self.password_parts[str(user_id)] = password_share - def getSharedPassword(self): + def getGroupPassword(self): shared_password = '' for password_share_index in sorted(self.password_parts): shared_password += str(self.password_parts[password_share_index]) return shared_password + def getMasterPassword(self): + return self.master_password + def addDecrypterId(self,decrypter_id): decrypter_id = int(decrypter_id) if decrypter_id not in self.valid_decrypter_ids: @@ -77,6 +87,9 @@ class Decryption(AbstractSplittedSecret): def getNeededCoDecryptersAmount(self): return self.needed_decrypters_amount -1 + def loadTxtFile(self,file_path): + return Path(file_path).read_text() + def loadJsonFile(self,file_path): file = open(file_path) data = json.load(file) @@ -90,6 +103,10 @@ class Decryption(AbstractSplittedSecret): input_file_path = self.getUserFilePath(self.user_id,AbstractSplittedSecret.TYPE_ENCRYPTED) self.decryptFile(self.user_password, input_file_path, self.user_file_decrypted_path) + def decryptGroupFile(self): + input_file_path = self.getGroupFilePath(self.group_name, AbstractSplittedSecret.TYPE_ENCRYPTED) + self.decryptFile(self.getGroupPassword(), input_file_path, self.encrypted_group_file_path) + def decryptAccumulatedFile(self): input_file_path = self.getAccumulatedFilePath(AbstractSplittedSecret.TYPE_ENCRYPTED) output_file_path = self.getAccumulatedFilePath(AbstractSplittedSecret.TYPE_DECRYPTED) diff --git a/scripts/main.py b/scripts/main.py index b4fb981..33913db 100644 --- a/scripts/main.py +++ b/scripts/main.py @@ -12,7 +12,7 @@ def clean_exit(): print("Cleaning up.") cleanup.cleanupFiles(AbstractSplittedSecret.TYPE_DECRYPTED) print("Leaving program. Goodby :)") - exit(); + exit() pass try: if __name__ == '__main__': @@ -42,13 +42,13 @@ try: decrypt = Decryption() if args.master_password is None: if args.user is None: - print("Please type in the user number:") + print("Type in the user id:") decrypt.initializeUser(input()) else: decrypt.initializeUser(args.user) if args.user_password is None: while True: - print("Please enter the user password:") + print("Enter the user password:") decrypt.setUserPassword(getpass()) print("Decrypting User File...") try: @@ -64,13 +64,12 @@ try: except Exception as error: print("An error occured. Propably you passed a wrong password :( The error is: " + str(error)) clean_exit() - print("File decrypted :) \n") - print("Please contact the following persons and tell them that you need help to encrypt the data: \n") + print("Contact the following persons and tell them that you need help to encrypt the data: \n") for contact_id in decrypt.user_data['contacts']: print("user_id: " + contact_id) for label in decrypt.user_data['contacts'][contact_id]: print(label + ": " + decrypt.user_data['contacts'][contact_id][label]) - print("--------------------------------\n") + print() while True: decrypt.resetDecrypterIds() try: @@ -83,23 +82,30 @@ try: person_counter += 1 break except Exception as error: - print("The following error occured <<" + str(error) + ">> :( \n Please try again :)") - print("\nFOR PASSWORD GROUP: " + decrypt.getDecryptersGroupName()) + print("The following error occured <<" + str(error) + ">> :( \n Try again :)") + print("\nYour data is:\n") + print("FOR PASSWORD GROUP: " + decrypt.getDecryptersGroupName()) print("FOR USER ID: " + decrypt.getUserId()) print("PASSWORD SHARE IS: " + decrypt.getPasswordShare() + "\n") while True: - decrypt.resetPasswordShare() - co_decrypter_ids = decrypt.getCoDecrypterIds() - print("Please execute this script at the users " + str(co_decrypter_ids) + ".") - for co_decrypter_id in decrypt.getCoDecrypterIds(): - print("\nFOR PASSWORD GROUP: " + decrypt.getDecryptersGroupName()) - print("FOR USER: " + str(co_decrypter_id)) - print("PASSWORD SHARE IS: ") - decrypt.addPasswordShare(co_decrypter_id, input()) - print("\nTHE SHARED PASSWORD IS: " + decrypt.getSharedPassword()) - break; - - clean_exit() + try: + decrypt.resetPasswordShare() + co_decrypter_ids = decrypt.getCoDecrypterIds() + for co_decrypter_id in decrypt.getCoDecrypterIds(): + print("Execute this script for user: " + str(co_decrypter_id) + ".") + print("Type in the password share.\n") + print("\nFOR PASSWORD GROUP: " + decrypt.getDecryptersGroupName()) + print("FOR USER: " + str(co_decrypter_id)) + print("PASSWORD SHARE IS: ") + decrypt.addPasswordShare(co_decrypter_id, input()) + print("\nTHE GROUP PASSWORD IS: " + decrypt.getGroupPassword()) + print("\nDecrypting group password file.\n") + decrypt.initializeGroupDataEncryption() + print("THE MASTER PASSWORD IS: " + decrypt.getMasterPassword()) + break; + except: + print("An unexpected error occured: \n" + traceback.format_exc()) + clean_exit() print("Decrypting accumulated file...") decrypt.setUserPassword(args.master_password) decrypt.decryptAccumulatedFile() @@ -107,7 +113,7 @@ try: if args.mode == 'encrypt': if args.master_password is None: - print("Please enter the master password:") + print("Enter the master password:") master_password = getpass() else: master_password = args.master_password @@ -115,11 +121,13 @@ try: if args.add_user_information is not None: for user_id in encrypt.user_mapped_data: for label in ['name','phone','email','address']: - print("Please enter attribut <<" + label + ">> for user <<" + user_id+ ">>:" ) + print("Enter attribut <<" + label + ">> for user <<" + user_id+ ">>:" ) encrypt.addInformationToUser(user_id, label, str(input())) encrypt.compileData() encrypt.encrypt() clean_exit() -except Exception: - print(traceback.format_exc()) +except KeyboardInterrupt: + print("Program interrupted by user.") +except: + print("An unexpected error occured: \n" + traceback.format_exc()) clean_exit() \ No newline at end of file