diff options
author | Marvin Borner | 2019-06-17 14:03:27 +0200 |
---|---|---|
committer | Marvin Borner | 2019-06-17 14:03:27 +0200 |
commit | 36b138f022274f1178958fa55b6c466a8b5ac984 (patch) | |
tree | 05caeda3793c66b6d16e0ce6c2f9c59ab645db57 | |
parent | 95cd9cc51a15d9e21767f2267eb385ef3d1cdc3a (diff) |
Added diffusion and confusion
-rw-r--r-- | main.py | 122 |
1 files changed, 105 insertions, 17 deletions
@@ -9,29 +9,38 @@ matrix_size = 4 def hex_to_matrix(hex_array): + """ + Converts hex arrays to 4x4 matrices + :param hex_array: Array of 16 hex chars + :return: 4x4 matrix representation + """ matrix = [] for i in range(16): if i % matrix_size == 0: matrix.append([hex_array[i]]) else: - matrix[int(i / 4)].append(hex_array[i]) + matrix[int(i / matrix_size)].append(hex_array[i]) return matrix def text_to_hex(text): + """ + Converts text to a array-hex representation + :param text: String/ASCII/UNICODE text + :return: Array of hex chars + """ hex_array = [] for char in text: hex_array.append(ord(char)) return hex_array -def shift_matrix(matrix): - for i in range(matrix_size): - matrix[i] = matrix[i][-i:] + matrix[i][:-i] - return matrix - - def key_expansion(key_matrix): + """ + Expands a single key matrix to 11 distinct ones + :param key_matrix: Matrix of master passphrase + :return: Array of 11 round keys + """ round_keys = [key_matrix] round_key = key_matrix[:] for r in range(0, 10): @@ -47,14 +56,13 @@ def key_expansion(key_matrix): return round_keys -def xor_matrix(first, second): - first = first.copy() - for i in range(4): - first[i] = first[i] ^ second[i] - return first - - def round_last(round_key, r): + """ + Applies special actions for bottom row + :param round_key: Complete matrix round key + :param r: Round index + :return: Modified bottom row (last column) + """ # Shift bottom row last_column = round_key[1:] + round_key[:1] @@ -67,9 +75,89 @@ def round_last(round_key, r): return last_column +def encrypt(text, passphrase): + """ + Encrypts a text using a 128-Bit passphrase + :param text: Plain text + :param passphrase: 128-Bit passphrase (16 chars) + :return: Encrypted text + """ + key_matrix = hex_to_matrix(text_to_hex(passphrase)) + text_matrix = hex_to_matrix(text_to_hex(text)) + round_keys = key_expansion(key_matrix) + merged_matrix = xor_matrices(key_matrix, text_matrix) + # 9 intermediate rounds for 128 Bit key size + for i in range(1): + confused_matrix = confusion(merged_matrix) + diffused_matrix = diffusion(confused_matrix) + mixed_matrix = mix_columns(diffused_matrix) + merged_matrix = xor_matrices(merged_matrix, mixed_matrix) + + +def confusion(merged_matrix): + """ + Applies confusion by running bytes through sbox + :param merged_matrix: Merged matrix of key and text + :return: New "confused" matrix + """ + pprint(merged_matrix) + merged_matrix = merged_matrix.copy() + for i in range(matrix_size): + for j in range(matrix_size): + merged_matrix[i][j] = sbox[merged_matrix[i][j]] + pprint(merged_matrix) + return merged_matrix + + +def diffusion(merged_matrix): + """ + Shifts the merged matrix to the left + :param merged_matrix: Merged matrix of key and text + :return: New "diffused" matrix + """ + merged_matrix = merged_matrix.copy() + for i in range(matrix_size): + merged_matrix[i] = merged_matrix[i][-i:] + merged_matrix[i][:-i] + pprint(merged_matrix) + return merged_matrix + + +def mix_columns(merged_matrix): + """ + Mixes columns with AES MixColumns algorithm + :param merged_matrix: Merged matrix of key and text + :return: New "mixed" matrix + """ + return merged_matrix # TODO + + +def xor_matrix(first, second): + """ + XORs 1-dimensional matrices + :param first: First matrix + :param second: Second matrix + :return: XORed (first) matrix + """ + first = first.copy() + for i in range(4): + first[i] = first[i] ^ second[i] + return first + + +def xor_matrices(first, second): + """ + XORs 2-dimensional matrices + :param first: First matrix + :param second: Second matrix + :return: XORed (first) matrix + """ + first = first.copy() + for i in range(len(first)): + first[i] = xor_matrix(first[i], second[i]) + return first + + test_key = text_to_hex("Thats my Kung Fu") test_text = text_to_hex("Two One Nine Two") -pprint(hex_to_matrix(test_key)) -print("\n\n") -pprint(key_expansion(hex_to_matrix(test_key))) +encrypt("ATTACK AT DAWN! ", "SOME 128 BIT KEY") |