Continued implementation of decryption

This commit is contained in:
Kevin Veen-Birkenbach 2022-12-10 12:21:43 +01:00
parent e5806992ce
commit f182e3226a
6 changed files with 83 additions and 27 deletions

View File

@ -33,7 +33,7 @@ python scripts/main.py --mode cleanup --user "1" && python scripts/main.py --mo
- Plattform independend - Plattform independend
- easy to use - easy to use
# setup # required software
```bash ```bash
pip install numpy pip install numpy
gpg gpg
@ -41,6 +41,10 @@ python scripts/main.py --mode cleanup --user "1" && python scripts/main.py --mo
pip pip
``` ```
## todo
- Implement cleanup
- implement relativ call
## Further Information ## Further Information
- https://www.tutorialspoint.com/python/python_command_line_arguments.htm - https://www.tutorialspoint.com/python/python_command_line_arguments.htm
- https://docs.python.org/3/library/argparse.html#module-argparse - https://docs.python.org/3/library/argparse.html#module-argparse

View File

@ -1,11 +1,23 @@
from .Cli import Cli from .Cli import Cli
class AbstractSplittedSecret(Cli): class AbstractSplittedSecret(Cli):
USER_PASSWORD_LENGTHS = 64
OVERALL_PASSWORD_LENGTHS = 128
# At the moment the programm can used deal with one digit numbers.
MAXIMUM_SECRET_HOLDERS = 9
MINIMUM_SECRET_HOLDERS = 2
def __init__(self): def __init__(self):
super(Cli, self).__init__() super(Cli, self).__init__()
self.data_folder = "data/" self.data_folder = "data/"
def getCoSecretHoldersRange():
return range(AbstractSplittedSecret.MINIMUM_SECRET_HOLDERS,AbstractSplittedSecret.MAXIMUM_SECRET_HOLDERS)
def getSecretHoldersRange():
return range(1,AbstractSplittedSecret.MAXIMUM_SECRET_HOLDERS)
def getFolderPath(self,folder_type): def getFolderPath(self,folder_type):
return self.data_folder + folder_type + "/" return self.data_folder + folder_type + "/"

View File

@ -19,8 +19,7 @@ class Cli(object):
for line in stdout: for line in stdout:
self.output.append(line.decode("utf-8")) self.output.append(line.decode("utf-8"))
if process.wait() > bool(0): if process.wait() > bool(0):
print(command, out, err) raise Exception("Error for: \nCommand:<<" + command + ">>\nOutput:<<" + out + ">>\nExitcode:<<" + err + ">>")
raise Exception("Exitcode is greater then 0")
return self.output return self.output
def getOutputString(self): def getOutputString(self):

View File

@ -7,22 +7,54 @@ class Decryption(AbstractSplittedSecret):
self.user_password='' self.user_password=''
super(Decryption, self).__init__() super(Decryption, self).__init__()
def setUserId(self,user_id): def initializeUser(self,user_id):
self.user_id=str(user_id) self.user_id=str(user_id)
self.user_file_decrypted_path = self.getUserFilePath(self.user_id,"decrypted") self.user_file_decrypted_path = self.getUserFilePath(self.user_id,"decrypted")
def initializeUserDataDecryption(self):
self.decryptUserFile()
self.user_data = self.loadJsonFile(self.user_file_decrypted_path)
self.initializeNeededDecryptersAmount()
self.initializeValidDecrypterIds()
def initializeNeededDecryptersAmount(self):
self.needed_decrypters_amount = len(str(list(self.user_data['groups'].keys())[0]))
def initializeValidDecrypterIds(self):
self.valid_decrypter_ids = []
self.valid_decrypter_ids.append(int(self.user_id))
for contact_id in self.user_data['contacts']:
self.valid_decrypter_ids.append(int(contact_id))
def setUserPassword(self,user_password): def setUserPassword(self,user_password):
self.user_password = str(user_password) self.user_password = str(user_password)
def resetDecrypterIds(self):
self.decrypter_ids = []
self.addDecrypterId(self.user_id)
def addDecrypterId(self,decrypter_id):
decrypter_id = int(decrypter_id)
if decrypter_id not in self.valid_decrypter_ids:
raise Exception("The encrypter id is not valid. Valid encrypter ids are: " + str(self.valid_decrypter_ids))
if len(self.decrypter_ids) >= self.needed_decrypters_amount:
raise Exception("There are already sufficients decrypters (" + str(len(self.decrypter_ids)) + ") defined!")
if decrypter_id in self.decrypter_ids:
raise Exception("The decrypter is already in the list.")
self.decrypter_ids.append(decrypter_id)
def getDecryptersIds(self):
return self.decrypter_ids
def getNeededCoDecryptersAmount(self):
return self.needed_decrypters_amount -1
def loadJsonFile(self,file_path): def loadJsonFile(self,file_path):
file = open(file_path) file = open(file_path)
data = json.load(file) data = json.load(file)
file.close() file.close()
return data return data
def setNeededEncryptersAmount(self):
self.needed_encrypters_amount = len(str(list(self.user_data['groups'].keys())[0]))-1
def decryptFile(self,password,input_file_path,output_file_path): def decryptFile(self,password,input_file_path,output_file_path):
self.executeCommand('gpg --batch --passphrase "'+ password + '" -o "' + output_file_path +'" "'+ input_file_path+'"') self.executeCommand('gpg --batch --passphrase "'+ password + '" -o "' + output_file_path +'" "'+ input_file_path+'"')
@ -34,8 +66,3 @@ class Decryption(AbstractSplittedSecret):
input_file_path = self.getAccumulatedFilePath("encrypted") input_file_path = self.getAccumulatedFilePath("encrypted")
output_file_path = self.getAccumulatedFilePath("decrypted") output_file_path = self.getAccumulatedFilePath("decrypted")
self.decryptFile(self.user_password, input_file_path, output_file_path) self.decryptFile(self.user_password, input_file_path, output_file_path)
def initializeData(self):
self.decryptUserFile()
self.user_data = self.loadJsonFile(self.user_file_decrypted_path)
self.setNeededEncryptersAmount()

View File

@ -22,7 +22,7 @@ class Encryption(AbstractSplittedSecret):
self.user_mapped_data = {} self.user_mapped_data = {}
user_count = 1 user_count = 1
while user_count <= self.amount_of_secret_holders: while user_count <= self.amount_of_secret_holders:
self.user_mapped_data[str(user_count)] = {"groups":{},"user_password":self.createPassword(64),"about":{}} self.user_mapped_data[str(user_count)] = {"groups":{},"user_password":self.createPassword(self.USER_PASSWORD_LENGTHS),"about":{}}
user_count += 1; user_count += 1;
def initializeGroupData(self): def initializeGroupData(self):
@ -81,7 +81,7 @@ class Encryption(AbstractSplittedSecret):
password = '' password = ''
for secret_holder_index in password_group_index_str: for secret_holder_index in password_group_index_str:
self.group_mapped_data[password_group_index_int]['members'][secret_holder_index]={} self.group_mapped_data[password_group_index_int]['members'][secret_holder_index]={}
particial_password_length= int(128*self.quota_factor); particial_password_length= int(self.OVERALL_PASSWORD_LENGTHS*self.quota_factor);
password_part = self.createPassword(particial_password_length) password_part = self.createPassword(particial_password_length)
self.group_mapped_data[password_group_index_int]['members'][secret_holder_index] = password_part self.group_mapped_data[password_group_index_int]['members'][secret_holder_index] = password_part
password += password_part password += password_part

View File

@ -3,15 +3,16 @@ from classes.Encryption import Encryption
from classes.Cleanup import Cleanup from classes.Cleanup import Cleanup
from classes.Decryption import Decryption from classes.Decryption import Decryption
from getpass import getpass from getpass import getpass
from classes.AbstractSplittedSecret import AbstractSplittedSecret
if __name__ == '__main__': if __name__ == '__main__':
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument('--mode',type=str, dest='mode',required=True,choices=['cleanup','encrypt','decrypt']) parser.add_argument('--mode',type=str, dest='mode',required=True,choices=['cleanup','encrypt','decrypt'])
parser.add_argument('--amount',type=int, dest='amount_of_secret_holders',required=False,choices=range(1,9)) parser.add_argument('--amount',type=int, dest='amount_of_secret_holders',required=False,choices=AbstractSplittedSecret.getCoSecretHoldersRange())
parser.add_argument('--quota', type=int, dest='decryption_quota', choices=range(1,101),required=False) 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('--master-password',type=str, dest='master_password',required=False)
parser.add_argument('--user-password',type=str, dest='user_password',required=False) parser.add_argument('--user-password',type=str, dest='user_password',required=False)
parser.add_argument('--user',type=int, dest='user',choices=range(1,9),required=False) parser.add_argument('--user',type=int, dest='user',choices=AbstractSplittedSecret.getSecretHoldersRange(),required=False)
parser.add_argument('--add-user-information',type=bool, dest='add_user_information', default=False, required=False, action=argparse.BooleanOptionalAction) parser.add_argument('--add-user-information',type=bool, dest='add_user_information', default=False, required=False, action=argparse.BooleanOptionalAction)
args = parser.parse_args() args = parser.parse_args()
mode = args.mode mode = args.mode
@ -34,26 +35,26 @@ if __name__ == '__main__':
if args.master_password is None: if args.master_password is None:
if args.user is None: if args.user is None:
print("Please type in the user number:") print("Please type in the user number:")
decrypt.setUserId(input()) decrypt.initializeUser(input())
else: else:
decrypt.setUserId(args.user) decrypt.initializeUser(args.user)
if args.user_password is None: if args.user_password is None:
while True: while True:
print("Please enter the user password:") print("Please enter the user password:")
decrypt.setUserPassword(getpass()) decrypt.setUserPassword(getpass())
print("Decrypting User File...") print("Decrypting User File...")
try: try:
decrypt.initializeData(); decrypt.initializeUserDataDecryption();
break; break;
except: except Exception as error:
print("Wrong password :(") print("An error occured. Propably you typed in a wrong password :( The error is: " + str(error))
else: else:
decrypt.setUserPassword(args.user_password) decrypt.setUserPassword(args.user_password)
print("Decrypting User File...") print("Decrypting User File...")
try: try:
decrypt.initializeData(); decrypt.initializeUserDataDecryption();
except: except Exception as error:
print("Wrong password :(") print("An error occured. Propably you passed a wrong password :( The error is: " + str(error))
exit() exit()
print("File decrypted :) \n") print("File decrypted :) \n")
print("Please contact the following persons and tell them that you need help to encrypt the data: \n") print("Please contact the following persons and tell them that you need help to encrypt the data: \n")
@ -61,7 +62,20 @@ if __name__ == '__main__':
print("user_id: " + contact_id) print("user_id: " + contact_id)
for label in decrypt.user_data['contacts'][contact_id]: for label in decrypt.user_data['contacts'][contact_id]:
print(label + ": " + decrypt.user_data['contacts'][contact_id][label]) print(label + ": " + decrypt.user_data['contacts'][contact_id][label])
print("You need at least <<" + str(decrypt.needed_encrypters_amount) +">> other person to decrypt the secret.") print("--------------------------------\n")
while True:
decrypt.resetDecrypterIds()
try:
person_counter = 1
while person_counter <= decrypt.getNeededCoDecryptersAmount():
print("The following user id's are in the decryption list: " + str(decrypt.getDecryptersIds()))
print("You need at least <<" + str(decrypt.getNeededCoDecryptersAmount()) +">> other person to decrypt the secret.")
print("Type in the user id of another encrypter:")
decrypt.addDecrypterId(int(input()))
person_counter += 1
break
except Exception as error:
print("The following error occured <<" + str(error) + ">> :( \n Please try again :)")
exit() exit()
print("Decrypting accumulated file...") print("Decrypting accumulated file...")
decrypt.setUserPassword(args.master_password) decrypt.setUserPassword(args.master_password)