def decrypt(cipherlist): """列をリストのリストの要素に変換し、暗号文を復号する""" col_combos = perms.perms(COLS) for key in col_combos: translation_matrix = [None] * COLS plaintext = "" start = 0 stop = ROWS for k in key: if k < 0: # 列を下から上に読む col_items = cipherlist[start:stop] elif k > 0: # 列を上から下に読む col_items = list(reversed(cipherlist[start:stop])) translation_matrix[abs(k) - 1] = col_items start += ROWS stop += ROWS for i in range(ROWS): for matrix_col in translation_matrix: word = str(matrix_col.pop()) plaintext += word + " " print("\n使用する鍵 = {}".format(key)) print("変換結果 = {}".format(plaintext)) print("\n鍵の数 = {}".format(len(col_combos)))
def decrypt(cipherlist): """ Turn columns into items in list of lists & decrypt ciphertext. """ col_combos = perms.perms(COLS) for key in col_combos: translation_matrix = [None] * COLS plaintext = '' start = 0 stop = ROWS for k in key: if k < 0: # reading bottom-to-top of column col_items = cipherlist[start:stop] elif k > 0: # reading top-to-bottom of columnn col_items = list((reversed(cipherlist[start:stop]))) translation_matrix[abs(k) - 1] = col_items start += ROWS stop += ROWS # loop through nested lists popping off last item to a new list: for i in range(ROWS): for matrix_col in translation_matrix: word = str(matrix_col.pop()) plaintext += word + ' ' print("\nusing key = {}".format(key)) print("translated = {}".format(plaintext)) print("\nnumber of keys = {}".format(len(col_combos)))
def perm_matrix(size): """ __params__: size : size of the matrix you want to create """ org_matrix = np.identity(size) string = [str(i) for i in range(0, size)] all_strings = p.perms("".join(string)) all_matrix = [] for k, v in enumerate(all_strings): matrix = np.zeros((size, size)) #perm matrix is square in cases of graphs adj matrix #do this with list comprehension for key, i in enumerate(v): matrix[key][int(i)] = 1 all_matrix.append(matrix) #print all_matrix[2] return all_matrix
def perm_matrix(size): """ __params__: size : size of the matrix you want to create """ org_matrix = np.identity(size) string = [str(i) for i in range(0,size)] all_strings = p.perms("".join(string)) all_matrix = [] for k,v in enumerate(all_strings): matrix = np.zeros((size,size)) #perm matrix is square in cases of graphs adj matrix #do this with list comprehension for key,i in enumerate(v): matrix[key][int(i)] = 1 all_matrix.append(matrix) #print all_matrix[2] return all_matrix