def e_myszkowski(plaintext, key): # your code here key_order = get_keyOrder_myszkowski(key) ciphertext = '' c = len(key_order) r = int(math.ceil(len(plaintext) / c)) matrix = utilities.new_matrix(r, c, '') counter = 0 for i in range(r): for j in range(c): matrix[i][j] = plaintext[counter] if counter < len( plaintext) else 'q' counter += 1 # read the matrix: unique --- vertically & repeated --- horizontally for i in range(c): for j in range(r): Index = 0 while i in key_order[Index:c]: Index = key_order.index(i, Index, c) ciphertext += matrix[j][Index] Index += 1 return ciphertext
def d_xcrypt(ciphertext, key): plaintext = '' r = key c = math.ceil(len(ciphertext) / r) #print(c) #print(c) matrix = utilities.new_matrix(r, c, "") count = 0 if (r * c != len(ciphertext)): return "" for i in range(r): for j in range(c): matrix[i][j] = ciphertext[count] count += 1 #utilities.print_matrix(matrix) for i in range(c): for j in range(r): #if(j != c and matrix[j][i] != 'q'): plaintext += matrix[j][i] #utilities.print_matrix(matrix) #print(plaintext) flag = False while (flag == False): if plaintext[len(plaintext) - 1] == 'q': plaintext = plaintext[:-1] else: flag = True return plaintext
def d_columnarTrans(ciphertext, key): # your code here key_order = get_keyOrder_columnarTrans(key) c = len(key_order) r = int(math.ceil(len(ciphertext) / c)) matrix = utilities.new_matrix(r, c, '') blocks = utilities.text_to_blocks(ciphertext, r) # fill the matrix according to the key_order counter = 0 for i in key_order: for j in range(r): matrix[j][i] = blocks[counter][j] counter += 1 # read the matrix and ignore the 'q' plaintext = '' for i in range(r): for j in range(c): if matrix[i][j] != 'q': plaintext += matrix[i][j] return plaintext
def e_scytale(plaintext, key): assert type(plaintext) == str, 'invalid plaintext' assert type(key) == int, 'invalid key' assert key >= 0, 'invalid key' col = int(math.ceil(len(plaintext)/key)) text_matrix = utilities.new_matrix(key, col, '') count = 0 ciphertext = '' # print(key, col) for i in range(key): for j in range(col): # utilities.print_matrix(text_matrix) if count < len(plaintext): text_matrix[i][j] = plaintext[count] count +=1 # utilities.print_matrix(text_matrix) for k in range(col): for l in range(key): ciphertext += text_matrix[l][k] return ciphertext
def e_ct(plaintext, key): assert type(plaintext) == str if _get_order_ct(key) == []: print('Error(e_ct): invalid key') return '' positions = [] ciphertext = plaintext for a in range(len(ciphertext)): if ciphertext[a] == ' ': positions.append([ciphertext[a], a]) if ciphertext[a] == '\n': positions.append([ciphertext[a], a]) if ciphertext[a] == '\t': positions.append([ciphertext[a], a]) ciphertext = clean_text(ciphertext, ' ') ciphertext = clean_text(ciphertext, '\n') ciphertext = clean_text(ciphertext, '\t') key_order = _get_order_ct(key) columnar_table = utilities.new_matrix( math.ceil(len(ciphertext) / len(key_order)), len(key_order), PAD) # for i in range(len(columnar_table)): # print(columnar_table[i]) k = 0 for i in range(len(columnar_table)): for j in range(len(columnar_table[i])): if k < len(ciphertext): columnar_table[i][j] = ciphertext[k] k += 1 else: columnar_table[i][j] = PAD # print(key_order) # for i in range(len(columnar_table)): # print(columnar_table[i]) ciphertext = '' for i in range(len(key_order)): for j in range(len(columnar_table)): ciphertext = ciphertext + columnar_table[j][key_order[i]] # print(key_order[i],j) # print(ciphertext) # print(ciphertext) ciphertext = insert_positions(ciphertext, positions) ciphertext = ciphertext.rstrip(PAD) return ciphertext
def pad(matrix, pad_amount): num_r = len(lines) num_c = len(lines[0]) new = utilities.new_matrix( num_r + (pad_amount * 2), num_c + (pad_amount * 2), '.') num_r += pad_amount * 2 num_c += pad_amount * 2 for r in range(pad_amount, num_r - pad_amount): for c in range(pad_amount, num_c - pad_amount): new[r][c] = lines[r-pad_amount][c-pad_amount] return new
def e_xcrypt(plaintext, key): # your code here r = key c = int(math.ceil(len(plaintext) / key)) cipherMatrix = utilities.new_matrix(r, c, "") counter = 0 for i in range(c): for j in range(r): cipherMatrix[j][i] = plaintext[counter] if counter < len( plaintext) else 'q' counter += 1 ciphertext = "" #utilities.print_matrix(cipherMatrix) for i in range(r): for j in range(c): ciphertext += cipherMatrix[i][j] return ciphertext
def d_myszkowski(ciphertext, key): # your code here key_order = get_keyOrder_myszkowski(key) c = len(key_order) r = int(math.ceil(len(ciphertext) / c)) matrix = utilities.new_matrix(r, c, '') blocks = utilities.text_to_blocks(ciphertext, r) # combine all the blocks of the same key character to one block for i in range(c): Index = 0 while i in key_order[Index:c]: if Index != 0: Index = key_order.index(i, Index, c) blocks[key_order[Index]] += blocks[key_order[Index] + 1] blocks.pop(key_order[Index] + 1) Index = key_order.index(i, Index, c) Index += 1 # fill the matrix for i in range(c): counter = 0 for j in range(r): Index = 0 while i in key_order[Index:c]: Index = key_order.index(i, Index, c) matrix[j][Index] = blocks[i][counter] Index += 1 counter += 1 # read the matrix and ignore the 'q' plaintext = '' for i in range(r): for j in range(c): if matrix[i][j] != 'q': plaintext += matrix[i][j] return plaintext
def d_scytale(ciphertext, key): assert type(ciphertext) == str, 'invalid ciphertext' assert type(key) == int, 'invalid key' assert key > 0, 'invalid key' col = int(math.ceil(len(ciphertext)/key)) text_matrix = utilities.new_matrix(key, col, '') count = 0 plaintext = '' unused = (key * col) - len(ciphertext) -1 # utilities.print_matrix(text_matrix) for i in range(col): for j in range(key): if j == (key-1) and i == (col-1)-unused: unused-=1 else: if count < len(ciphertext): text_matrix[j][i] = ciphertext[count] count +=1 # print(ciphertext) # utilities.print_matrix(text_matrix) for k in range(key): for l in range(col): plaintext += text_matrix[k][l] # print(len(ciphertext), len(plaintext)) return plaintext