def encrypt_with_rsa():
    pub_keys = tools.get_all_public_keys()
    if(len(pub_keys) > 0):
        print("Select the public key to encrypt with.")
        choice = c_io.menu_selection(pub_keys) - 1
        message_to_encrypt = c_io.get_input("Enter any message to encrypt")
        encrypted_message = rsa.encrypt(pub_keys[choice], message_to_encrypt)
        path_to_save_to = c_io.get_input(
            "Enter the File name to save the encrypted message", filter=file_filter)
        with open(path_to_save_to, 'w') as message_file:
            message_file.write(encrypted_message)
    else:
        print("No Public keys found")
def do_bulk_rsa():
    message_file = c_io.get_input(
        "Enter file with messages", filter=file_filter)
    text = tools.read_in_file(message_file)
    files = tools.get_all_public_keys()
    if(len(files) > 0):
        for block in text:
            print("Text to encrypt")
            print(block)
            print("Select a key to encrypt message above\n")
            choice = c_io.menu_selection(files)
            save_name = c_io.get_input("What should i name the file?")
            save_name = resolve_file_name(save_name)
            encrypted_message = rsa.encrypt(files[choice - 1], block)
            with open(save_name, 'w') as file:
                file.write(encrypted_message)
    else:
        print("You must have some public keys in the current directory")
def decrypt_with_rsa():
    priv_keys = tools.get_all_private_keys()
    if(len(priv_keys) > 0):
        choice = c_io.menu_selection(priv_keys) - 1
        path_to_message = c_io.get_input(
            "Enter the file path to the message", filter=file_filter)
        decrypted_message = rsa.decrypt(priv_keys[choice], path_to_message)
        print(decrypted_message)
    else:
        print("No private keys found")
Example #4
0
def do_action(cypher, message, attr, key=None):
    method = getattr(cypher, attr)
    parameters = len(inspect.signature(method).parameters)
    output = None
    if(parameters is 2):
        if(key is None):
            key = c_io.get_input("Enter a key")
        output = method(key, message)
    else:
        output = method(message)
    return output
def brute_force_finder():
    applicable_cyphers = cyphers.brute_force_cyphers
    opts = list(map(cyphers.get_name, applicable_cyphers))
    selection = c_io.menu_selection(opts, True)
    if(selection > 0):
        selection -= 1
        message = c_io.get_input("Please Enter Message To Decrypt")
        possible_choices = applicable_cyphers[selection].brute_force_crack(
            message)
        for possible_choice in possible_choices:
            print(possible_choice)
        pause()
    else:
        cls()
    return True
def do_file_encryption():
    cypher_names = list(map(cyphers.get_name, cyphers.cyphers))
    running = True
    selection = c_io.menu_selection(cypher_names, True)
    if(selection > 0):
        selection -= 1
        action = c_io.menu_selection(cyphers.actions) - 1
        message_file = c_io.get_input(
            "Enter a Message File", filter=check_if_file_exists)
        message_file = resolve_file_name(message_file)
        message = cyphers.do_action_based_on_cypher(
            cyphers.cyphers[selection], cyphers.actions[action], message_file)
        with open("%s_%s" % (cyphers.actions[action], re.sub(r"(\.{1}[A-z]{1,})", ".txt", message_file)), 'w') as file:
            file.write(message)
        pause()
    cls()
    return running
def encryptor_decryptor():
    cypher_names = list(map(cyphers.get_name, cyphers.cyphers))
    running = True
    selection = c_io.menu_selection(cypher_names, True)
    if(selection > 0):
        selection -= 1
        action = c_io.menu_selection(cyphers.actions) - 1
        user_input = c_io.get_input("Enter a Message", False)
        was_valid_input = False
        while(not was_valid_input):
            try:
                message = cyphers.do_action_based_on_cypher(
                    cyphers.cyphers[selection], cyphers.actions[action], message=user_input)
                print(message)
                was_valid_input = True
            except AssertionError as error:
                print(error)
        pause()
    else:
        cls()
    return running
import cypher_app.command_line.console_io as c_io


def brute_force_message(message):
    character_count = count_characters(message)
    frequencies = generate_character_frequencies(character_count, len(message))
    # best guess at substitution
    # substitute the string


def count_characters(message):
    character_count = {}
    for c in message:
        c = c.upper()
        if (c.isalpha()):
            if (c in character_count.keys()):
                character_count[c] += 1
            else:
                character_count[c] = 1
    return character_count


def generate_character_frequencies(character_counts, message_length):
    pass


if __name__ == '__main__':
    message = c_io.get_input("Enter A Message to analyize")
    print(count_characters(message))
def generate_keys():
    file_name = c_io.get_input("Enter File Name For Keys")
    key_size_in_bits = c_io.get_int_input(
        "Enter a Key Size[in bits]", 2, 10000)
    rsa_gen.generate_key_files(file_name, key_size_in_bits)