2022-12-09 12:03:45 +01:00
import argparse
2022-12-09 20:13:48 +01:00
from classes . Encryption import Encryption
2022-12-09 13:08:07 +01:00
from classes . Cleanup import Cleanup
2022-12-11 14:55:17 +01:00
from classes . Decryption import Decryption , AutomaticIdentificationImpossibleException
2022-12-09 14:52:57 +01:00
from getpass import getpass
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
2022-12-10 21:20:26 +01:00
standard_exit ( )
2022-12-10 18:31:49 +01:00
def dirty_exit ( ) :
print ( " ATTENTION: SECURITY RISK !!! \n PROGRAM DIDN ' T CLEAN UP DECRYPTED DATA. \n DECRYPTED DATA EXISTS AND CAN BE READ BY EVERYBODY! " )
2022-12-10 22:19:57 +01:00
print ( " TO REMOVE DECRYPTED DATA EXECUTE: \n main.py --mode cleanup --file-types " + Paths . TYPE_DECRYPTED )
2022-12-10 21:20:26 +01:00
standard_exit ( )
def standard_exit ( ) :
2022-12-10 18:31:49 +01:00
print ( " Leaving program. " )
exit ( )
2022-12-10 21:20:26 +01:00
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 ] )
2022-12-11 18:46:32 +01:00
parser . add_argument ( ' --secret-holders-amount ' , type = int , dest = ' amount_of_secret_holders ' , required = False , choices = Encryption . getCoSecretHoldersRange ( ) , help = " Needed for creating of encryption meta data. " )
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:27:28 +01:00
parser . add_argument ( ' --user ' , type = int , dest = ' user ' , choices = Encryption . getSecretHoldersRange ( ) , required = False )
2022-12-11 18:46:32 +01:00
parser . add_argument ( ' --add-user-information ' , type = bool , dest = ' add_user_information ' , default = False , required = False , action = argparse . BooleanOptionalAction , help = " Add additional information to users. " )
parser . add_argument ( ' --input-directory ' , type = str , dest = ' input_directory ' , required = False , help = " The directory from which the data should be encrypted. " )
2022-12-13 16:23:24 +01:00
parser . add_argument ( ' --meta-data ' , type = bool , dest = ' meta_data ' , default = False , required = False , action = argparse . BooleanOptionalAction , help = " When mode is encrypt and this flag is set, the encrypted meta data is created. When mode is decrypt and this flag is set the accumulated file will be decrypted. " )
2022-12-10 14:42:11 +01:00
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-11 16:47:07 +01:00
print ( " To leave the appplication use the key kombination: <<Ctr>> + <<Alt>> + <<C>> " )
2022-12-13 15:55:01 +01:00
print ( " Cleaning up all decrypted files. " )
cleanup . cleanupFiles ( Paths . TYPE_DECRYPTED )
2022-12-10 14:42:11 +01:00
print ( " Selected Mode: " + args . mode )
2022-12-09 13:08:07 +01:00
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 ( )
2022-12-10 21:20:26 +01:00
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 )
2022-12-10 21:20:26 +01:00
standard_exit ( )
2022-12-10 18:31:49 +01:00
print ( " Deleting all " + args . file_types + " files. " )
cleanup . cleanupFiles ( args . file_types )
2022-12-10 21:20:26 +01:00
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-13 16:23:24 +01:00
if args . meta_data is True :
2022-12-13 15:55:01 +01:00
if args . master_password is None :
print ( " Enter the master password: " )
master_password = getpass ( )
2022-12-10 14:42:11 +01:00
else :
2022-12-13 15:55:01 +01:00
master_password = args . master_password
decrypt = Decryption ( cli , paths )
print ( " Decrypting accumulated data. " )
decrypt . setUserPassword ( master_password )
decrypt . decryptAccumulatedFile ( )
dirty_exit ( )
if args . user is None :
try :
print ( " Attempt to identify user. " )
user_id = decrypt . identifyUser ( )
print ( " The user id is: " + user_id )
except :
print ( " A automatic user id identification wasn ' t possible. " )
print ( " Type in the user id: " )
user_id = input ( )
decrypt . initializeUser ( user_id )
else :
decrypt . initializeUser ( args . user )
if args . user_password is None :
while True :
print ( " Enter the user password: " )
decrypt . setUserPassword ( getpass ( ) )
2022-12-09 23:43:28 +01:00
print ( " Decrypting User File... " )
try :
2022-12-10 12:21:43 +01:00
decrypt . initializeUserDataDecryption ( ) ;
2022-12-10 17:30:27 +01:00
break ;
2022-12-13 15:55:01 +01:00
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 )
print ( " Decrypting User File... " )
try :
decrypt . initializeUserDataDecryption ( ) ;
except Exception as error :
print ( " An error occured. Propably you passed a wrong password :( The error is: " + str ( error ) )
clean_exit ( )
print ( " \n Contact the following persons and request their password share: \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 ] )
while True :
print ( " \n Reset password shares. \n " )
decrypt . resetDecrypterIds ( )
try :
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 ( " \n Type in the user id of another decrypter: " )
decrypt . addDecrypterId ( int ( input ( ) ) )
password_shares_count + = 1
break
except Exception as error :
print ( " The following error occured << " + str ( error ) + " >> :( \n Try again :) " )
print ( " \n Your data is: \n " )
print ( " FOR PASSWORD GROUP: " + decrypt . getDecryptersGroupName ( ) )
print ( " FOR USER ID: " + decrypt . getUserId ( ) )
print ( " PASSWORD SHARE IS: " + decrypt . getPasswordShare ( ) + " \n " )
while True :
try :
decrypt . resetPasswordShare ( )
co_decrypter_ids = decrypt . getCoDecrypterIds ( )
for co_decrypter_id in decrypt . getCoDecrypterIds ( ) :
print ( " Type in the password share for: \n " )
print ( " FOR PASSWORD GROUP: " + decrypt . getDecryptersGroupName ( ) )
print ( " FOR USER: " + str ( co_decrypter_id ) )
print ( " PASSWORD SHARE IS: " )
decrypt . addPasswordShare ( co_decrypter_id , input ( ) )
print ( " \n THE GROUP PASSWORD IS: " + decrypt . getGroupPassword ( ) )
print ( " \n Decrypting 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 ( " The data was decrypted to: " + paths . getDecryptedMainDataStandartFolder ( ) )
2022-12-10 21:20:26 +01:00
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-11 18:46:32 +01:00
if args . add_user_information is True :
2022-12-10 14:42:11 +01:00
for user_id in encrypt . user_mapped_data :
2022-12-11 18:46:32 +01:00
for label in [ ' name ' , ' phone ' , ' email ' , ' address ' , ' notes ' ] :
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 ( )
2022-12-13 16:23:24 +01:00
if args . meta_data is True :
2022-12-11 18:46:32 +01:00
print ( " Create and encrypt meta data. " )
encrypt . encryptMetaData ( )
if args . input_directory is not None :
print ( " Encrypt main data. " )
encrypt . encryptMainData ( args . input_directory )
2022-12-10 21:20:26 +01:00
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 ( )