split-secret/scripts/main.py

156 lines
7.8 KiB
Python
Raw Normal View History

2022-12-09 12:03:45 +01:00
import argparse
2022-12-09 20:13:48 +01:00
from classes.Encryption import Encryption
from classes.Cleanup import Cleanup
2022-12-09 20:13:48 +01:00
from classes.Decryption import Decryption
from getpass import getpass
2022-12-10 22:19:57 +01:00
from classes.Paths import Paths
2022-12-10 14:42:11 +01:00
import traceback
2022-12-10 22:03:29 +01:00
from classes.Cli import Cli
2022-12-10 22:19:57 +01:00
from classes.Paths import Paths
2022-12-09 12:03:45 +01:00
2022-12-10 22:03:29 +01:00
cli = Cli()
2022-12-10 22:19:57 +01:00
paths = Paths()
cleanup = Cleanup(cli,paths)
2022-12-10 13:22:09 +01:00
def clean_exit():
print("Cleaning up.")
2022-12-10 18:31:49 +01:00
try:
2022-12-10 22:19:57 +01:00
cleanup.cleanupFiles(Paths.TYPE_DECRYPTED)
2022-12-10 18:31:49 +01:00
except:
pass
standard_exit()
2022-12-10 18:31:49 +01:00
def dirty_exit():
print("ATTENTION: SECURITY RISK !!!\nPROGRAM DIDN'T CLEAN UP DECRYPTED DATA. \nDECRYPTED DATA EXISTS AND CAN BE READ BY EVERYBODY!")
2022-12-10 22:19:57 +01:00
print("TO REMOVE DECRYPTED DATA EXECUTE:\nmain.py --mode cleanup --file-types " + Paths.TYPE_DECRYPTED)
standard_exit()
def standard_exit():
2022-12-10 18:31:49 +01:00
print("Leaving program.")
exit()
2022-12-10 14:42:11 +01:00
try:
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--mode',type=str, dest='mode',required=True,choices=['cleanup','encrypt','decrypt'])
2022-12-10 22:19:57 +01:00
parser.add_argument('--file-types',type=str, dest='file_types',required=False,choices=[Paths.TYPE_DECRYPTED, Paths.TYPE_ENCRYPTED])
parser.add_argument('--amount',type=int, dest='amount_of_secret_holders',required=False,choices=Paths.getCoSecretHoldersRange())
2022-12-10 14:42:11 +01:00
parser.add_argument('--quota', type=int, dest='decryption_quota', choices=range(1,101),required=False)
parser.add_argument('--master-password',type=str, dest='master_password',required=False)
parser.add_argument('--user-password',type=str, dest='user_password',required=False)
2022-12-10 22:19:57 +01:00
parser.add_argument('--user',type=int, dest='user',choices=Paths.getSecretHoldersRange(),required=False)
2022-12-10 14:42:11 +01:00
parser.add_argument('--add-user-information',type=bool, dest='add_user_information', default=False, required=False, action=argparse.BooleanOptionalAction)
args = parser.parse_args()
2022-12-10 13:22:09 +01:00
2022-12-10 18:31:49 +01:00
print("Application started.")
2022-12-10 14:42:11 +01:00
print("Selected Mode: " + args.mode)
2022-12-10 14:42:11 +01:00
if args.mode == 'cleanup':
2022-12-10 18:31:49 +01:00
print("Cleaning up.")
if args.file_types is None:
if args.user is None:
print("Deleting all encrypted and decrypted files.")
cleanup.deleteAll()
standard_exit()
2022-12-10 18:31:49 +01:00
print("Deleting all files which aren't related to user: " + str(args.user));
cleanup.cleanupForUser(args.user)
standard_exit()
2022-12-10 18:31:49 +01:00
print("Deleting all " + args.file_types + " files.")
cleanup.cleanupFiles(args.file_types)
standard_exit()
2022-12-10 14:42:11 +01:00
if args.mode == 'decrypt':
2022-12-10 22:19:57 +01:00
decrypt = Decryption(cli,paths)
2022-12-10 14:42:11 +01:00
if args.master_password is None:
if args.user is None:
2022-12-10 17:30:27 +01:00
print("Type in the user id:")
2022-12-10 14:42:11 +01:00
decrypt.initializeUser(input())
else:
decrypt.initializeUser(args.user)
if args.user_password is None:
while True:
2022-12-10 17:30:27 +01:00
print("Enter the user password:")
2022-12-10 14:42:11 +01:00
decrypt.setUserPassword(getpass())
print("Decrypting User File...")
try:
decrypt.initializeUserDataDecryption();
break;
except Exception as error:
print("An error occured. Propably you typed in a wrong password :( The error is: " + str(error))
else:
decrypt.setUserPassword(args.user_password)
2022-12-09 23:43:28 +01:00
print("Decrypting User File...")
try:
2022-12-10 12:21:43 +01:00
decrypt.initializeUserDataDecryption();
except Exception as error:
2022-12-10 14:42:11 +01:00
print("An error occured. Propably you passed a wrong password :( The error is: " + str(error))
clean_exit()
2022-12-10 18:31:49 +01:00
print("\nContact the following persons and request their password share: \n")
2022-12-10 14:42:11 +01:00
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])
while True:
2022-12-10 18:31:49 +01:00
print("\nReset password shares.\n")
2022-12-10 14:42:11 +01:00
decrypt.resetDecrypterIds()
try:
2022-12-10 18:31:49 +01:00
password_shares_count = 1
while password_shares_count < decrypt.getNeededDecryptersAmount():
print(str(password_shares_count) + " password shares had been added.")
print("Password shares for the the users " + str(decrypt.getDecrypterIds()) + " been added. ")
print("You need to add " + str((decrypt.getNeededDecryptersAmount()-password_shares_count)) +" more password shares.")
print("\nType in the user id of another decrypter:")
2022-12-10 14:42:11 +01:00
decrypt.addDecrypterId(int(input()))
2022-12-10 18:31:49 +01:00
password_shares_count += 1
2022-12-10 14:42:11 +01:00
break
except Exception as error:
2022-12-10 17:30:27 +01:00
print("The following error occured <<" + str(error) + ">> :( \n Try again :)")
print("\nYour data is:\n")
print("FOR PASSWORD GROUP: " + decrypt.getDecryptersGroupName())
2022-12-10 14:42:11 +01:00
print("FOR USER ID: " + decrypt.getUserId())
print("PASSWORD SHARE IS: " + decrypt.getPasswordShare() + "\n")
while True:
2022-12-10 17:30:27 +01:00
try:
decrypt.resetPasswordShare()
co_decrypter_ids = decrypt.getCoDecrypterIds()
for co_decrypter_id in decrypt.getCoDecrypterIds():
2022-12-10 18:31:49 +01:00
print("Type in the password share for: \n")
print("FOR PASSWORD GROUP: " + decrypt.getDecryptersGroupName())
2022-12-10 17:30:27 +01:00
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())
print("Decrypting main data.")
decrypt.decryptMainData()
print("All data decrypted.")
2022-12-10 18:31:49 +01:00
dirty_exit()
print("Decrypting accumulated data.")
2022-12-10 14:42:11 +01:00
decrypt.setUserPassword(args.master_password)
decrypt.decryptAccumulatedFile()
dirty_exit()
2022-12-10 14:42:11 +01:00
if args.mode == 'encrypt':
if args.master_password is None:
2022-12-10 17:30:27 +01:00
print("Enter the master password:")
2022-12-10 14:42:11 +01:00
master_password = getpass()
2022-12-09 23:43:28 +01:00
else:
2022-12-10 14:42:11 +01:00
master_password = args.master_password
2022-12-10 22:19:57 +01:00
encrypt = Encryption(cli,paths,args.amount_of_secret_holders, args.decryption_quota, master_password)
2022-12-10 14:42:11 +01:00
if args.add_user_information is not None:
for user_id in encrypt.user_mapped_data:
for label in ['name','phone','email','address']:
2022-12-10 17:30:27 +01:00
print("Enter attribut <<" + label + ">> for user <<" + user_id+ ">>:" )
2022-12-10 14:42:11 +01:00
encrypt.addInformationToUser(user_id, label, str(input()))
encrypt.compileData()
encrypt.encryptAll()
dirty_exit()
2022-12-10 17:30:27 +01:00
except KeyboardInterrupt:
print("Program interrupted by user.")
2022-12-10 13:22:09 +01:00
clean_exit()