Пример #1
0
def idea_main_encryption():
    """ Launch IDEA Encryption process """

    print(
        "========================================IDEA ENCRYPTION========================================"
    )
    key_len, mod_of_operation = idea_menu()

    print(
        "-------------------------------------------PASSWORD-------------------------------------------"
    )
    # key = diffie_hellman(1024, True)  # Launch Diffie Hellman with default values
    print("\033[1;34m[?]\033[1;m", "PASSWORD", "\x1b[0m",
          "(diffie-hellman shared key)")
    key = input("IDEA/> ")  # convert <srt> to <int>
    bytes_key = bytes.fromhex(key[2:])
    if key_len == 128:
        key = int.from_bytes(hashlib.md5(bytes_key).digest(), 'little')
    elif key_len == 256:
        # key = int.from_bytes(hashlib.sha3_256(bytes_key).digest(), 'little')
        key = int(SHA3.sha3_int(int(key, 16), 256)[2:], 16)
    else:
        raise ValueError("Wrong key length value")
    # print('\033[91mKEY:', hex(key), '\x1b[0m')
    # input("Please Consider Saving this Key... (Press any Key)")

    print(
        "---------------------------------------SELECTING A FILE---------------------------------------"
    )
    filename = utils.get_filename()

    # filename = "C:/Users/antoine/Desktop/CryptoSystem/core/tests/idea_test.txt"
    print("File to encrypt:", filename)
    # cypher_file = filename.split(".", 1)[0] + '.cypher'
    cypher_file = filename + '.cypher'

    print(
        "------------------------------------GATHERING FILE CONTENT------------------------------------"
    )
    hex_message = utils.get_file_hex(filename)  # Getting the Hex
    pad = block_feeder.PKCS7_padding(hex_message)  # Padding
    file_blocks = block_feeder.generate_blocks(pad)  # Generating Blocks
    print("File Blocks: {0}\n{1}\n{2}\n...".format(file_blocks[:3],
                                                   file_blocks[3:7],
                                                   file_blocks[7:11]))

    print(
        "------------------------------------------ENCRYPTION------------------------------------------"
    )
    # Managing mode of operation
    if mod_of_operation is "ECB":
        idea = IDEAModeOfOperationECB(key, key_len)
    elif mod_of_operation is "CBC":
        # iv = secrets.randbits(64)    # 16 hex long = 64 bits  # works with static iv
        idea = IDEAModeOfOperationCBC(key, key_len)
    elif mod_of_operation is "PCBC":
        # iv = secrets.randbits(64)    # 16 hex long = 64 bits  # works with static iv
        idea = IDEAModeOfOperationPCBC(key, key_len)
    else:
        raise ValueError("Wrong Mode of Operation.")

    # Encryption (It's about time)
    output = ''
    for i in range(len(file_blocks)):
        encrypted = idea.encrypt(file_blocks[i])
        output += hex(encrypted)[2:].zfill(16)

    wrap = textwrap.fill(output, 94)
    print(wrap)

    print(
        "------------------------------------CREATE ENCRYPTED FILE-------------------------------------"
    )
    utils.wipe_file(cypher_file)  # Clean cypher file if it already exist
    with open(cypher_file, mode='a') as file:
        file.write(output)
    print("\x1b[6;30;42mCypher successfully created\x1b[0m\n")
    sys.stdout.flush()

    print(
        "-------------------------------------HASH PLAINTEXT FILE--------------------------------------"
    )
    plaintext_hash = SHA3.sha3_file(filename, 256)
    print("\033[1;32m[+]\033[1;m", "SHA_256:\033[1;32m", plaintext_hash,
          "\x1b[0m")
    return 0
Пример #2
0
def idea_main_decryption():
    """ Launch IDEA Decryption process """

    print(
        "========================================IDEA DECRYPTION========================================"
    )
    key_len, mod_of_operation = idea_menu()

    print(
        "---------------------------------------SELECTING A FILE---------------------------------------"
    )
    filename = utils.get_filename()
    print("File to Decrypt:", filename)

    # Grabbing original file extension
    file_extension = filename.split('.cypher', 1)[0]
    if len(file_extension.split('.', 1)) != 1:
        file_extension = '.' + file_extension.split('.', 1)[1]
    else:
        file_extension = ''

    print(
        "------------------------------------GATHERING FILE CONTENT------------------------------------"
    )
    hex_message = utils.get_file_hex(filename)
    true_hex_message = bytes.decode(
        bytes.fromhex(hex_message)
    )  # one more decoding step due to how the file is open ('rb')
    file_blocks = block_feeder.generate_blocks(true_hex_message)
    print("File Blocks: {0}\n{1}\n{2}\n...".format(file_blocks[:3],
                                                   file_blocks[3:7],
                                                   file_blocks[7:11]))

    print(
        "-------------------------------------------PASSWORD-------------------------------------------"
    )
    print(
        "Please type-in the Hexadecimal password given during the encryption process"
    )
    # key = int(input("> "), 16)
    key = input("IDEA/> ")
    bytes_key = bytes.fromhex(key[2:])
    if key_len == 128:
        key = int.from_bytes(hashlib.md5(bytes_key).digest(), 'little')
    elif key_len == 256:
        # key = int.from_bytes(hashlib.sha3_256(bytes_key).digest(), 'little')
        key = int(SHA3.sha3_int(int(key, 16), 256)[2:], 16)
    else:
        raise ValueError("Wrong key length value")

    print(
        "------------------------------------------DECRYPTION------------------------------------------"
    )
    # Managing mode of operation
    if mod_of_operation is "ECB":
        idea = IDEAModeOfOperationECB(key, key_len)
    elif mod_of_operation is "CBC":
        # iv = secrets.randbits(64)    # 16 hex long = 64 bits  # works with static iv
        idea = IDEAModeOfOperationCBC(key, key_len)
    elif mod_of_operation is "PCBC":
        # iv = secrets.randbits(64)    # 16 hex long = 64 bits  # works with static iv
        idea = IDEAModeOfOperationPCBC(key, key_len)
    else:
        raise ValueError("Wrong Mode of Operation.")

    # Decryption
    output = ''
    for i in range(len(file_blocks)):
        decrypted = idea.decrypt(file_blocks[i])
        decrypted = hex(decrypted)[2:].zfill(16)  # keeping the right format
        output += decrypted

    unpadded = block_feeder.PKCS7_unpadding(output)
    try:
        unpadded_to_bytes = bytes.fromhex(unpadded)

        wrap = textwrap.fill(str(unpadded_to_bytes)[2:], 94)
        print(wrap)

        print(
            "----------------------------------SAVING DECRYPTED TEXT-----------------------------------"
        )
        # new_file = utils.save_file() + ".txt"
        new_file = utils.save_file() + file_extension
        utils.write_file(new_file, unpadded_to_bytes)
    except:
        print("\033[1;31m[-]\x1b[0m",
              "It seems like you entered the wrong Password")

    try:
        print(
            "-----------------------------------HASH DECRYPTED FILE------------------------------------"
        )
        plaintext_hash = SHA3.sha3_file(new_file, 256)
        print("\033[1;32m[+]\033[1;m", "SHA_256:\033[1;32m", plaintext_hash,
              "\x1b[0m")
    except:
        print("\033[1;31m[-]\x1b[0m",
              "Something went wrong during the hash process")

    return 0
Пример #3
0
def get_upload_to(instance, filename):
    name, ext = os.path.splitext(filename)
    return 'jobs-images/%s' % get_filename(ext)
Пример #4
0
def get_img_path(instance, filename):
    name, ext = os.path.splitext(filename)
    return 'blog/%s' % get_filename(ext)
Пример #5
0
def get_img_path(instance, filename):
    name, ext = os.path.splitext(filename)
    return 'blog/%s' % get_filename(ext)
Пример #6
0
 def gen_output_path(self):
     outdir = utils.get_parent(self.options.get('output'))
     basename = utils.get_filename(self.options.get('output'))
     self.options['repo_output'] = utils.join_path(outdir, f'repo-{basename}')
     self.options['user_output'] = utils.join_path(outdir, f'user-{basename}')
     self.options['org_output'] = utils.join_path(outdir, f'org-{basename}')
Пример #7
0
def get_upload_to(instance, filename):
    name, ext = os.path.splitext(filename)
    return 'jobs-images/%s' % get_filename(ext)