def apply_f_n_times(a, n): x_0 = np.arange(0, 256).astype(np.uint8) x_prev = x_0 + 0 x_next = np.roll(f(a, x_prev), 1) x_next = swap_4bits(x_next) # same_idx = np.where(x_next==x_0)[0].astype(np.uint8) print("{}x_next{}:".format(clrs.lcb, clrs.rst)) pretty_block_printer(x_next, 8, 256) # print("{}same_idx{}:".format(clrs.lgb, clrs.rst)) # pretty_block_printer(same_idx, 8, len(same_idx)) for i in xrange(0, n): x_new = x_prev[x_next] x_prev = x_next x_next = np.roll(f(a, x_new), 1) x_next = swap_4bits(x_next) print("i: {}{}{}, {}x_next{}:".format(clrs.lwb, i, clrs.rst, clrs.lcb, clrs.rst)) pretty_block_printer(x_next, 8, 256) if check_if_sbox_good(x_next) == False: return None return x_next
def decrypt_arr(arr, sbox): block_size = 16 in_block_bytes = 8 length = arr.shape[0] assert len(arr) % block_size == 0 block_amount = length // block_size - 1 print("block_amount: {}".format(block_amount)) # get the length_orig at the beginning of the arr, and add random bytes too # e.g. block size is 16 length_arr = decrypt_arr_block(arr[:16], sbox)[:8] print("length_arr:") pretty_block_printer(length_arr, 8, 8) length_orig = int(length_arr.view(np.uint64)[0]) arr_new = np.zeros((length_orig, ), dtype=np.uint8) for i in xrange(1, block_amount + 1): block_arr = decrypt_arr_block(arr[block_size * i:block_size * (i + 1)], sbox) if i == block_amount and length_orig % in_block_bytes != 0: arr_new[in_block_bytes * (i - 1):] = block_arr[:length_orig % in_block_bytes] else: arr_new[in_block_bytes * (i - 1):in_block_bytes * i] = block_arr[:in_block_bytes] print("i: {}, block_arr:".format(i)) pretty_block_printer(block_arr, 8, len(block_arr)) return arr_new
def encrypt_arr(arr, sbox): block_size = 16 in_block_bytes = 8 length_orig = arr.shape[0] block_amount = length_orig // in_block_bytes + ( (length_orig % in_block_bytes) != 0) print("length_orig: {:X}".format(length_orig)) print("block_amount: {}".format(block_amount)) # add the length_orig at the beginning of the arr, and add random bytes too # e.g. block size is 16 length_arr = np.array([length_orig], dtype=np.uint64).view(np.uint8) rand_bytes = np.random.randint(0, 256, (8, )).astype(np.uint8) first_block = np.hstack((length_arr, rand_bytes)) print("first_block:") pretty_block_printer(first_block, 8, block_size) arr_new = np.zeros((block_size * (block_amount + 1), ), dtype=np.uint8) arr_new[:block_size] = encrypt_arr_block(first_block, sbox) for i in xrange(1, block_amount + 1): if i == block_amount and length_orig % in_block_bytes != 0: block_arr = arr[in_block_bytes * (i - 1):] block_arr = np.hstack( (block_arr, np.random.randint(0, 256, (in_block_bytes - (length_orig % in_block_bytes))).astype( np.uint8))) else: block_arr = arr[in_block_bytes * (i - 1):in_block_bytes * i] block_arr = np.hstack( (block_arr, np.random.randint(0, 256, (block_size - len(block_arr), )).astype( np.uint8))) print("i: {}, block_arr:".format(i)) pretty_block_printer(block_arr, 8, block_size) arr_new[block_size * i:block_size * (i + 1)] = encrypt_arr_block( block_arr, sbox) return arr_new
sbox_1 = invert_bits_func(bits) sbox_2 = invert_pos_func(bits) sbox_3 = swap_pos_func(bits) sbox_4 = roll_shift_func(bits) return [sbox_1, sbox_2, sbox_3, sbox_4] if __name__ == "__main__": print("different simple sbox functions") bits = 8 ib_table = invert_bits_func(bits) print("{}ib_table:{}".format(clrs.lgb, clrs.rst)) pretty_block_printer(ib_table, 8, len(ib_table)) ip_table = invert_pos_func(bits) print("{}ip_table:{}".format(clrs.lgb, clrs.rst)) pretty_block_printer(ip_table, 8, len(ip_table)) sp_table = swap_pos_func(bits) print("{}sp_table:{}".format(clrs.lgb, clrs.rst)) pretty_block_printer(sp_table, 8, len(sp_table)) rs_table = roll_shift_func(bits) print("{}rs_table:{}".format(clrs.lgb, clrs.rst)) pretty_block_printer(rs_table, 8, len(rs_table)) # rx_table_sort = np.sort(rs_table) # diff = rx_table_sort[1:]-rx_table_sort[:-1]
i] = block_arr[:in_block_bytes] print("i: {}, block_arr:".format(i)) pretty_block_printer(block_arr, 8, len(block_arr)) return arr_new if __name__ == "__main__": f = lambda a, x: a[0] * x + a[1] * x**2 sbox_8bit = f([3, 6], np.arange(0, 256).astype(np.uint8)) sbox_inv_8bit = sbox_8bit + 0 sbox_inv_8bit[sbox_8bit] = np.arange(0, 256).astype(np.uint8) print("sbox_8bit:") pretty_block_printer(sbox_8bit, 8, 256) arr_sorted = np.sort(sbox_8bit) # print("arr_sorted:") # pretty_block_printer(arr_sorted, 8, 256) diff = arr_sorted[1:] - arr_sorted[:-1] is_good_sbox = np.sum(diff != 1) == 0 print("is_good_sbox: {}".format(is_good_sbox)) # print("diff") # pretty_block_printer(diff, 8, len(diff)) sys.exit(0)
return cycles if __name__ == "__main__": # acceptable_constants = find_acceptable_constants() # for i, accept_consts in enumerate(acceptable_constants): # print("i: {}, accept_consts: {}".format(i, accept_consts)) # sys.exit(0) # a = [5, 5, 4, 2, 2, 6, 8, 6] a = [(0, 5), (1, 5)] sbox_8bit = f(a, np.arange(0, 16).astype(np.uint8)) print("sbox_8bit:") pretty_block_printer(sbox_8bit, 8, 16, 4) arr_sorted = np.sort(sbox_8bit) diff = arr_sorted[1:] - arr_sorted[:-1] is_good_sbox = np.sum(diff != 1) == 0 print("is_good_sbox: {}".format(is_good_sbox)) if is_good_sbox: cycles = get_sbox_cycles(sbox_8bit) # print("cycles:\n{}".format(cycles)) for i, cycle in enumerate(cycles): print("i: {}, len(cycle): {}".format(i, len(cycle))) # print("cycle:\n{}".format(cycle)) print("cycle:") pretty_block_printer(cycle, 8, len(cycle), 4)
#! /usr/bin/python2.7 # -*- coding: utf-8 -*- import sys sys.path.append("../encryption") import numpy as np from Utils import pretty_block_printer, clrs if __name__ == "__main__": rnd_data_8bit = np.random.randint(0, 256, (24, )).astype(np.uint8) print("{}rnd_data_8bit:{}".format(clrs.lyb, clrs.rst)) pretty_block_printer(rnd_data_8bit, 8, rnd_data_8bit.shape[0]) with open("some_random_data.hex", "wb") as fout: rnd_data_8bit.tofile(fout) with open("some_random_data.hex", "rb") as fin: read_data = np.fromfile(fin, dtype=np.uint8, count=24) # read_data = np.fromfile(fin, dtype=np.uint16, count=12) # # To change from big to little endian, and vice versa # read_data = (read_data>>8)|(read_data<<8) print("{}read_data:{}".format(clrs.lyb, clrs.rst)) pretty_block_printer(read_data, 8, read_data.shape[0]) # with open("/dev/sdf", "rb") as fin: # data = np.fromfile(fin, dtype=np.uint8, count=512)
# a = [2, 10, 12, 13, 17, 33, 22, 6, 3, 8, 7, 4, 11, 64, 7, 5, 3, 8, # 10, 12, 13, 17, 33, 22, 6, 3, 8, 7, 4, 11, 64, 7, 5, 3, 8, 7, 4, 11, 64, 7, 5, 3] # a = [3, 5, 6, 2, 4, 7, 5, 10, 5, 6, 23, 44, 23, 14] a = np.random.randint(1, 256, (128, )) different_sboxes = get_basic_sboxes(8) sbox_8bit = apply_f_n_times_roll(a, different_sboxes) # sbox_8bit = apply_f_n_times(a, 2) if sbox_8bit is None: print("NOOO!!") sys.exit(0) print("{}a:{}".format(clrs.lgb, clrs.rst)) pretty_block_printer(a, 8, a.shape[0]) print("{}sbox_8bit:{}".format(clrs.lgb, clrs.rst)) pretty_block_printer(sbox_8bit, 8, 256) sbox_int = sbox_8bit.astype(np.int) x_0 = np.arange(0, 256).astype(np.int) diff_1 = sbox_int - x_0 diff_1_abs = np.abs(diff_1) diff_2 = np.roll(sbox_int, 1) - sbox_int diff_2_abs = np.abs(diff_2) sbox1, sbox2, sbox3, sbox4 = list( map(lambda x: x.astype(np.int), different_sboxes))