def s_box(input, s_box_index): if len(input) != 6: raise Exception("Wrong dimension") row = convert_bin_array_to_dec([input[0], input[5]]) col = convert_bin_array_to_dec(input[1:5]) return convert_to_bin_array(s_box_array[s_box_index][row][col], 4)
def convert_text_to_bin_array(text): result = [] byte_str = text.encode('utf-8') for b in byte_str: result.extend(convert_to_bin_array(b, 8)) return result
def encrypt_one_block(plain_text, key): initial_permutation_result = initial_permutation(plain_text) keys = round_key_generator(convert_to_bin_array(key, 64)) rounds_result = rounds(initial_permutation_result, keys) cipher_text = final_permutation(rounds_result) return cipher_text
def decrypt_text(text, key): bin_array = convert_to_bin_array(text, 0) blocks_input = convert_bin_array_to_block(bin_array, 64) block_output = [] for block in blocks_input: block_output.append(decrypt_one_block(block, key)) bin_array = convert_block_to_bin_array(block_output) return convert_bin_array_to_text(bin_array)
def main(): print(convert_to_bin_array('0x18ca18ad', 32)) print(expansion_p_box(convert_to_bin_array('0x18ca18ad', 32)))