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
```bash
python scripts/main.py --mode cleanup &&
python scripts/main.py --amount 6 --quota 50 --mode generate --master-password "ewrwerwerew" &&
python scripts/main.py --mode decrypt --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 --user "1"
```
# Requirements to know
- Amount of People

View File

@ -42,19 +42,19 @@ class Encryption(AbstractSplittedSecret):
unvalid_sequenz = re.compile("(.)\\1+")
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 = {}
user_count = 1
while user_count <= self.amount_of_secret_holders:
self.user_mapped_data[str(user_count)] = {"groups":{},"user_password":self.createPassword(64)}
user_count += 1;
def createGroupMappedDataFrame(self):
def createGroupDataFrame(self):
self.group_mapped_data = {}
def generateMappedData(self):
self.createUserMappedDataFrame()
self.createGroupMappedDataFrame()
def generateData(self):
self.createUserDataFrame()
self.createGroupDataFrame()
index = self.getStartnumber()
while index < self.getEndnumber():
password_group_index_str = ''.join(sorted(str(index)))
@ -78,7 +78,7 @@ class Encryption(AbstractSplittedSecret):
def encryptStringToFile(self,text,output_file,password):
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:
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'])
@ -86,38 +86,19 @@ class Encryption(AbstractSplittedSecret):
def encryptToJsonFile(self,data,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:
file_path=self.getUserFilePath(user_id,"encrypted")
data=self.user_mapped_data[user_id]['groups']
password=self.user_mapped_data[user_id]['user_password']
self.encryptToJsonFile(data,file_path,password)
def encryptAccumulatedMappedData(self):
def encryptAccumulatedData(self):
file_path=self.getAccumulatedFilePath("encrypted")
data={"user_mapped": self.user_mapped_data, "group_mapped": self.group_mapped_data}
self.encryptToJsonFile(data,file_path,self.master_password)
def saveMappedData(self):
self.encryptUserMappedData()
self.encryptAccumulatedMappedData()
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
def encrypt(self):
self.encryptUserData()
self.encryptAccumulatedData()
self.encryptGroupFiles()

View File

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