Refactored Encryption class

This commit is contained in:
Kevin Veen-Birkenbach 2022-12-09 21:49:06 +01:00
parent 284fec6c15
commit 09f494804f
3 changed files with 37 additions and 45 deletions

View File

@ -4,8 +4,9 @@ The purpose of this software is to splitt a secret over multiple people. Just if
# testing # testing
```bash ```bash
python scripts/main.py --mode cleanup && python scripts/main.py --mode cleanup &&
python scripts/main.py --amount 6 --quota 50 --mode generate --master-password "ewrwerwerew" && python scripts/main.py --amount 6 --quota 50 --mode encrypt --master-password "ewrwerwerew" &&
python scripts/main.py --mode decrypt --master-password "ewrwerwerew" python scripts/main.py --mode decrypt --master-password "ewrwerwerew" &&
python scripts/main.py --mode decrypt --user "1"
``` ```
# Requirements to know # Requirements to know
- Amount of People - Amount of People

View File

@ -42,19 +42,19 @@ class Encryption(AbstractSplittedSecret):
unvalid_sequenz = re.compile("(.)\\1+") unvalid_sequenz = re.compile("(.)\\1+")
return re.search(valid_numbers, password_group_index_str) and not re.search(unvalid_sequenz, password_group_index_str) return re.search(valid_numbers, password_group_index_str) and not re.search(unvalid_sequenz, password_group_index_str)
def createUserMappedDataFrame(self): def createUserDataFrame(self):
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)} self.user_mapped_data[str(user_count)] = {"groups":{},"user_password":self.createPassword(64)}
user_count += 1; user_count += 1;
def createGroupMappedDataFrame(self): def createGroupDataFrame(self):
self.group_mapped_data = {} self.group_mapped_data = {}
def generateMappedData(self): def generateData(self):
self.createUserMappedDataFrame() self.createUserDataFrame()
self.createGroupMappedDataFrame() self.createGroupDataFrame()
index = self.getStartnumber() index = self.getStartnumber()
while index < self.getEndnumber(): while index < self.getEndnumber():
password_group_index_str = ''.join(sorted(str(index))) password_group_index_str = ''.join(sorted(str(index)))
@ -78,7 +78,7 @@ class Encryption(AbstractSplittedSecret):
def encryptStringToFile(self,text,output_file,password): def encryptStringToFile(self,text,output_file,password):
self.executeCommand('echo \'' + text + '\' | gpg --symmetric --armor --batch --passphrase "' + password + '" -o "' + output_file + '"') self.executeCommand('echo \'' + text + '\' | gpg --symmetric --armor --batch --passphrase "' + password + '" -o "' + output_file + '"')
def generateEncryptedGroupFiles(self): def encryptGroupFiles(self):
for password_group_index_int in self.group_mapped_data: for password_group_index_int in self.group_mapped_data:
encrypted_group_password_file_path = self.getGroupFilePath(password_group_index_int,"encrypted") encrypted_group_password_file_path = self.getGroupFilePath(password_group_index_int,"encrypted")
self.encryptStringToFile(self.master_password,encrypted_group_password_file_path,self.group_mapped_data[password_group_index_int]['password']) self.encryptStringToFile(self.master_password,encrypted_group_password_file_path,self.group_mapped_data[password_group_index_int]['password'])
@ -86,38 +86,19 @@ class Encryption(AbstractSplittedSecret):
def encryptToJsonFile(self,data,file_path,password): def encryptToJsonFile(self,data,file_path,password):
self.encryptStringToFile(json.dumps(data,ensure_ascii=False), file_path, password) self.encryptStringToFile(json.dumps(data,ensure_ascii=False), file_path, password)
def encryptUserMappedData(self): def encryptUserData(self):
for user_id in self.user_mapped_data: for user_id in self.user_mapped_data:
file_path=self.getUserFilePath(user_id,"encrypted") file_path=self.getUserFilePath(user_id,"encrypted")
data=self.user_mapped_data[user_id]['groups'] data=self.user_mapped_data[user_id]['groups']
password=self.user_mapped_data[user_id]['user_password'] password=self.user_mapped_data[user_id]['user_password']
self.encryptToJsonFile(data,file_path,password) self.encryptToJsonFile(data,file_path,password)
def encryptAccumulatedMappedData(self): def encryptAccumulatedData(self):
file_path=self.getAccumulatedFilePath("encrypted") file_path=self.getAccumulatedFilePath("encrypted")
data={"user_mapped": self.user_mapped_data, "group_mapped": self.group_mapped_data} data={"user_mapped": self.user_mapped_data, "group_mapped": self.group_mapped_data}
self.encryptToJsonFile(data,file_path,self.master_password) self.encryptToJsonFile(data,file_path,self.master_password)
def saveMappedData(self): def encrypt(self):
self.encryptUserMappedData() self.encryptUserData()
self.encryptAccumulatedMappedData() self.encryptAccumulatedData()
self.encryptGroupFiles()
def encryptMappedUserData(self):
self.user_passwords = {}
for user_id in self.user_mapped_data:
self.user_passwords[user_id] = self.createPassword(64)
def encryptMappedData(self):
self.encryptMappedUserData()
def generate(self):
self.generateMappedData()
self.saveMappedData()
self.encryptMappedData()
self.generateEncryptedGroupFiles()
def getUserMappedData(self):
return self.user_mapped_data
def getGroupMappedData(self):
return self.group_mapped_data

View File

@ -6,14 +6,18 @@ from getpass import getpass
if __name__ == '__main__': if __name__ == '__main__':
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument('--mode',type=str, dest='mode',required=True,choices=['cleanup','generate','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=range(1,9))
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',type=int, dest='user',choices=range(1,9),required=False) parser.add_argument('--user',type=int, dest='user',choices=range(1,9),required=False)
args = parser.parse_args() args = parser.parse_args()
mode = args.mode
if args.mode == 'cleanup': print("Splitted Secret Interface started.")
print("Selected Mode: " + mode)
if mode == 'cleanup':
cleanup = Cleanup() cleanup = Cleanup()
if args.user is None: if args.user is None:
cleanup.deleteAll() cleanup.deleteAll()
@ -21,31 +25,37 @@ if __name__ == '__main__':
cleanup.cleanupForUser(args.user) cleanup.cleanupForUser(args.user)
exit() exit()
if args.mode == 'decrypt': if mode == 'decrypt':
decrypt = Decryption() decrypt = Decryption()
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.setUserId(input())
else: else:
decrypt.setUser(args.user) decrypt.setUserId(args.user)
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:
decrypt.decryptUserFile(); decrypt.decryptUserFile();
break;
except:
print("Wrong password :(")
print("File encrypted :) ")
exit() exit()
print("Decrypting accumulated file...") print("Decrypting accumulated file...")
decrypt.setUserPassword(args.master_password) decrypt.setUserPassword(args.master_password)
decrypt.decryptAccumulatedFile() decrypt.decryptAccumulatedFile()
exit() exit()
if mode == 'encrypt':
if args.mode == 'generate':
if args.master_password is None: if args.master_password is None:
print("Please enter the master password:") print("Please enter the master password:")
master_password = getpass() master_password = getpass()
else: else:
master_password = args.master_password master_password = args.master_password
generate = Encryption(args.amount_of_secret_holders, args.decryption_quota,master_password) encrypt = Encryption(args.amount_of_secret_holders, args.decryption_quota, master_password)
generate.generate() encrypt.generateData()
encrypt.encrypt()
exit() exit()