예제 #1
0
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
예제 #2
0
 def getKeyValue(keyName):
     key = message.prompt(keyName + ' value')
     if (key != ''):
         dictkeys[keyName] = key
     elif (key == ''):
         message.error('Value cannot be empty.')
         getKeyValue(keyName)
예제 #3
0
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)
예제 #4
0
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
예제 #5
0
    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!')
예제 #6
0
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
예제 #7
0
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
예제 #8
0
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
예제 #9
0
def main_menu(args=sys.argv[1:]):
    if len(args) == 0:
        help()
    else:
        if args[0].lower() == 'init':
            init_function()
        elif args[0].lower() == '--help':
            help()
        elif args[0].lower() == '--version':
            version()
        elif len(args) == 2:
            fname = args[0]

            # check if file exists
            if not os.path.isfile(fname):
                message.error(f'{fname} not found')
                help()
                return

            cmd = args[1]

            # encrypt file
            if cmd.lower() == 'enc':
                if checkEnc(fname) != False:
                    shouldLoad = message.prompt('Load key from memory? (Y/n): ')
                    if shouldLoad.lower() == 'y' or shouldLoad.lower() == '':
                        keyFname = message.prompt('File with key for decryption (' + STORAGE_DIR + '/' + STORE_KEY_F + '): ')
                        key = getKey() if keyFname == '' else getKey(keyFname)
                    elif shouldLoad.lower() == 'n':
                        key = message.prompt('Key for encryption: ')
                    encryption(key, fname)
                else:
                    message.error(fname + ' is already encrypted')

            # decrypt file
            elif cmd.lower() == 'dec':
                if checkEnc(fname) != False:
                    message.error(fname + ' is already decrypted')
                else:
                    shouldLoad = message.prompt('Load key from memory? (Y/n): ')
                    if shouldLoad.lower() == 'y' or shouldLoad.lower() == '':
                        keyFname = message.prompt('File with key for decryption (' + STORAGE_DIR + '/' + STORE_KEY_F + '): ')
                        key = getKey() if keyFname == '' else getKey(keyFname)
                    elif shouldLoad.lower() == 'n':
                        key = message.prompt('Key for decryption: ')
                    decryption(key, fname)

            # 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')
            
            # invalid arguments
            else:
                message.error('Invalid Argument')
                help()
        # invalid arguments
        else:
            message.error('Invalid Argument')
            help()