Exemplo n.º 1
0
def decrypt():
    print("Preparing to decrypt...")
    data = get_decrypt_input()

    while True:
        cypher = input(
            "1   : Ceaser (shift) Cypher\n2   : Block Cypher\n3   : Diffie-Hellman Cypher\nPlease select a cypher (1, 2, or 3): "
        )

        try:
            cypher = int(cypher)
        except ValueError:
            print("Sorry, {} is not a valid choice. Pick 1, 2, or 3.".format(
                cypher))
            continue

        if cypher == 1:
            decrypted = shift_cypher(data[0], -data[1])
            break
        elif cypher == 2:
            chunk_list = list(map(int, data[0].split("\n")))
            chunk_list = block_unshift(chunk_list, data[1])
            decrypted = block_rebuild(chunk_list)
            break
        elif cypher == 3:
            shared_key = dh_shared_key(dh_private_key, data[1])
            decrypted = dh_unshift(data[0], shared_key)
            break
        elif cypher == 0:
            return

    print("The decrypted message is:\n'{}'".format(decrypted))

    return
Exemplo n.º 2
0
def decrypt():
    print(colored_text17)
    data = get_decrypt_input()

    while True:
        cypher = input(colored_text18)

        try:
            cypher = int(cypher)
        except ValueError:
            print(colored_text19.format(cypher))
            continue

        if cypher == 1:
            decrypted = shift_cypher(data[0], -data[1])
            break
        elif cypher == 2:
            chunk_list = list(map(int, data[0].split("\n")))
            chunk_list = block_unshift(chunk_list, data[1])
            decrypted = block_rebuild(chunk_list)
            break
        elif cypher == 3:
            shared_key = dh_shared_key(dh_private_key, data[1])
            decrypted = dh_unshift(data[0], shared_key)
            break
        elif cypher == 0:
            return

    print(colored_text20.format(decrypted))

    return
Exemplo n.º 3
0
def encrypt():
    print("Preparing to encrypt...")
    data = get_encrypt_input()

    while True:
        file_name = input("Please enter your message's name: ").strip()
        if "{}.txt".format(file_name) in os.listdir("msgs"):
            print(
                "Sorry, there is already a secret message with that name. Choose another."
            )
            continue

        cypher = input(
            "1   : Ceaser (shift) Cypher\n2   : Block Cypher\n3   : Diffie-Hellman Cypher\nPlease select a cypher (1, 2, or 3): "
        )

        try:
            cypher = int(cypher)
        except ValueError:
            print("Sorry, {} is not a valid choice. Pick 1, 2, or 3.".format(
                cypher))
            continue

        if cypher == 1:
            encrypted = shift_cypher(data[0], data[1])
            break
        elif cypher == 2:
            chunk_list = block_pad(data[0])
            encrypted = block_shift(chunk_list, data[1])
            encrypted = "\n".join(str(s) for s in encrypted)
            break
        elif cypher == 3:
            msg_public_key = dh_base**data[1] % dh_mod
            shared_key = dh_shared_key(dh_private_key, msg_public_key)
            encrypted = dh_shift(data[0], shared_key)
            break
        elif cypher == 0:
            return

    with io.open("msgs/{}.txt".format(file_name), 'w+',
                 encoding="utf-8") as file:
        file.write(encrypted)
    text = "|"
    escape = "\033["
    print("Your message was successfully encrypted!\n ")
Exemplo n.º 4
0
def encrypt():
    print(colored_text10)
    data = get_encrypt_input()

    while True:
        file_name = input(colored_text11).strip()
        if "{}.txt".format(file_name) in os.listdir("msgs"):
            print(colored_text12)
            continue

        cypher = input(colored_text13)

        try:
            cypher = int(cypher)
        except ValueError:
            print(colored_text14.format(cypher))
            continue

        if cypher == 1:
            encrypted = shift_cypher(data[0], data[1])
            break
        elif cypher == 2:
            chunk_list = block_pad(data[0])
            encrypted = block_shift(chunk_list, data[1])
            encrypted = "\n".join(str(s) for s in encrypted)
            break
        elif cypher == 3:

            shared_key = dh_shared_key(dh_private_key, data[1])
            encrypted = dh_shift(data[0], shared_key)
            break
        elif cypher == 0:
            return

    with io.open("msgs/{}.txt".format(file_name), 'w+',
                 encoding="utf-8") as file:
        file.write(encrypted)
    print(colored_text15)