def generate_encryption(): key = keygen() #Displaying the Key message.success("SECURITY KEY GENERATED: " + key) user_choice = message.prompt("Save the key locally (Y/n)") # user choice to save the file # default user choice is YES if user_choice == "" or user_choice.lower() == "y" or user_choice.lower( ) == "yes": storage_choice = message.prompt( "Path to store key to (" + os.path.join(STORAGE_DIR, STORE_KEY_F) + "_keys.key" + ")") if storage_choice != "": storeKey(key, storage_choice) else: storeKey(key) print(key) elif user_choice.lower() == "n" or user_choice.lower() == "no": message.success("YOUR KEY WILL NOT BE STORED. KEEP IT SAFE.") else: message.error("INVALID INPUT") generateEncryption() return key
def main_menu(args): # print(args) if args[0].lower() == 'init': init_function() elif args[0].lower() == '--help': help() elif args[0].lower() == '--version': version() else: fname = args[0] cmd = args[1] # encrypt file if cmd.lower() == 'enc': if checkEnc(fname) != False: keyFname = message.prompt('File with key for description (' + STORAGE_DIR + '/' + STORE_KEY_F + '): ') key = getKey() if keyFname == '' else getKey(keyFname) encryption(key, fname) message.success(fname + ' is now encrypted') else: message.error(fname + ' is already encrypted') # decrypt file elif cmd.lower() == 'dec': if checkEnc(fname) != False: message.error(fname + ' is already decrypted') else: keyFname = message.prompt('File with key for description (' + STORAGE_DIR + '/' + STORE_KEY_F + '): ') key = getKey() if keyFname == '' else getKey(keyFname) decryption(key, fname) message.success(fname + ' is now decrypted') # add new keys elif cmd.lower() == 'add': if checkEnc(fname) != False: add(fname) else: message.error(fname + ' is encrypted') # subtract keys elif cmd.lower() == 'sub': if checkEnc(fname) != False: subtract(fname) else: message.error(fname + ' is encrypted') # subtract keys elif cmd.lower() == 'mod': if checkEnc(fname) != False: modify(fname) else: message.error(fname + ' is encrypted')
def getKeyValue(keyName): key = message.prompt(keyName + ' value') if (key != ''): dictkeys[keyName] = key elif (key == ''): message.error('Value cannot be empty.') getKeyValue(keyName)
def modify(fileName): dictkeys = jsonToDict(fileName) def modifyKeyValue(keyName): if keyName in dictkeys: key = message.prompt(keyName + ' new value') if (key != ''): dictkeys[keyName] = key elif (key == ''): message.error('Value cannot be empty.') getKeyValue(keyName) else: message.error('No such Key exists!') while (True): keyName = message.prompt('Enter Key to be modified (done)') if (keyName != ''): modifyKeyValue(keyName) elif (keyName == ''): break try: dictToJson(dictkeys, fileName) message.success('Keys modified successfully!') except: message.error('Some error occured!') return False #test functions #add(fileName) #subtract(fileName) #modify(fileName)
def get_filename_from_user(): filename = message.prompt('Name of file holding keys (keys.json)') # default input: keys.json if (filename == ''): filename = 'keys.json' return filename
def modifyKeyValue(keyName): if keyName in dictkeys: key = message.prompt(keyName + ' new value') if (key != ''): dictkeys[keyName] = key elif (key == ''): message.error('Value cannot be empty.') getKeyValue(keyName) else: message.error('No such Key exists!')
def subtract(fileName): dictkeys = jsonToDict(fileName) def removeKeyValue(keyName): try: del dictkeys[keyName] except: message.error('No such Key exists!') while (True): keyName = message.prompt('Enter Key to be removed (done)') if (keyName != ''): removeKeyValue(keyName) elif (keyName == ''): break try: dictToJson(dictkeys, fileName) message.success('Keys removed successfully!') except: message.error('Some error occured!') return False
def add(fileName): dictkeys = jsonToDict(fileName) def getKeyValue(keyName): key = message.prompt(keyName + ' value') if (key != ''): dictkeys[keyName] = key elif (key == ''): message.error('Value cannot be empty.') getKeyValue(keyName) while (True): keyName = message.prompt('Key (done)') if (keyName != ''): getKeyValue(keyName) elif (keyName == ''): break try: dictToJson(dictkeys, fileName) message.success('New keys added successfully!') except: message.error('Some error occured!') return False
def get_keys_from_user(): keyDict = {} # get value def getKeyValue(keyName): key = message.prompt(keyName + ' value') if (key != ''): keyDict[keyName] = key elif (key == ''): message.error('Value cannot be empty.') getKeyValue(keyName) # get key while (True): keyName = message.prompt('Key (done)') if (keyName != ''): getKeyValue(keyName) elif (keyName == ''): break # default input ends key-value input sequence message.delete_last() return keyDict