Ejemplo n.º 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
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)
Ejemplo n.º 3
0
def version():
    message.success("CLIK Version 0.0.0")


#test functions
#help()
#version()
Ejemplo n.º 4
0
def display_json(filename, display_indent=4):
    json_val = jsonToDict(filename)
    if json_val:
        message.success(filename + ' loaded successfully')
        printy(json_val, indentation=display_indent)
    else:
        message.error('JSON couldn\'t be loaded')
        debug.error('JSON to Dict Failed')
Ejemplo n.º 5
0
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')
Ejemplo n.º 6
0
def dictToJson(inputDict, filename, indents=2):
    try:
        with open(filename, 'w') as fh2:
            fh2.write(json.dumps(inputDict, indent=indents, sort_keys=True))
        message.success('Keys stored successfully')
        return True
    except Exception as e:
        message.error('Couldn\'t write to JSON')
        debug.error(str(e))
        return False
Ejemplo n.º 7
0
def keygen():
    try:
        #generating the key and storing them in a file
        key = Fernet.generate_key()
        message.success("Encryption key generated successfully")
    except Exception as e:
        message.error('Couldn\'t generate encryption key')
        debug.error(str(e))
        return False
    return key.decode('utf-8')
Ejemplo n.º 8
0
def decryption(key, filename):
    try:
        with open(filename,'rb') as f:
            data = f.read()
        q = Fernet(key)
        decrypted_message = q.decrypt(data)
        with open(filename,'wb') as f:
            f.write(decrypted_message)
        message.success(filename + ' successfully decrypted')
    except Exception as e:
        message.error(filename + ' couldn\'t be decrypted')
        debug.error(filename + ' couldn\'t be decrypted')
        print(e)
        return False
    return True
Ejemplo n.º 9
0
def encryption(key, filename):
    try:
        with open(filename, 'rb') as f:
            data = f.read()
        #encrypting the data from the given file name
        fernet = Fernet(key)
        encrypted=fernet.encrypt(data)    
        with open(filename,'wb') as f:
            f.write(encrypted)
        message.success(filename + ' successfully encrypted')
    except Exception as e:
        message.error(filename + ' couldn\'t be encrypted')
        debug.error(filename + ' couldn\'t be encrypted')
        print (e)
        return False
    return True
Ejemplo n.º 10
0
def storeKey(key, filename = STORE_KEY_F):
    storeTo = os.path.join (STORAGE_DIR, filename) + "_keys.key"
    if init_default_key_storage():
        try:
            #creating the key file in which the key'll be stored
            file = open(storeTo,'w')
            file.write(key)
            file.close()
            message.success("Key was stored to " + storeTo + " successfully")
        except Exception as e:
            message.error("Key couldn't be stored to " + storeTo)
            debug.error("Key couldn't be stored to " + storeTo)
            return False
        return True
    else:
        return False
Ejemplo n.º 11
0
def init_function():
    if init_default_key_storage():
        filename = get_filename_from_user()
        tokendict = get_keys_from_user()
        enc_key = generate_encryption()

        if dictToJson(tokendict, filename):
            if encryption(enc_key, filename):
                message.success("Filename: " + filename)
        else:
            message.error("Filename: " + filename)
        message.success("Keys Added: ")
        printy(tokendict, indentation=4)


# init_function()
Ejemplo n.º 12
0
def getKey(filename = STORE_KEY_F):
    getFrom = os.path.join (STORAGE_DIR, filename) + "_keys.key"
    if init_default_key_storage():
        try:
            # retriving key file
            file = open (getFrom, 'r')
            key = file.read ()
            file.close ()
            message.success ('Loaded encryption key successfully')
            return key
        except:
            message.error ('Couldn\'t load encryption key')
            debug.error ('Couldn\'t load encryption key')
            return False
    else:
        message.error ('Couldn\'t load encryption key')
        debug.error ('Couldn\'t load encryption key')
        return False
Ejemplo n.º 13
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
Ejemplo n.º 14
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
Ejemplo n.º 15
0
def help():
    message.success(
        "Usage: clik <command> \n \n init\t\tinitializes the script to input key value pairs \n add\t\tinsert key value pair in existing json file \n subtract\tdelete key value pair in existing json file \n modify\t\talter key value in existing json file \n encrypt\tencrypts key value dictionary to json and generate encryption key \n decrypt\tdecrypts json file to dictionary \n help\t\tguide through all the CLIK functions \n version\tshows the CLIK version installed "
    )