0

我正在尝试创建一个 python playfair 密码,并且在此过程中遇到了一些问题。我有一个五乘五的表格,其中包含以下信息:

 [['A', 'B', 'C', 'D', 'E'],
 ['F', 'G', 'H', 'I', 'Y'],
 ['K', 'L', 'M', 'N', 'O'],
 ['P', 'Q', 'R', 'S', 'T'],
 ['U', 'V', 'W', 'X', 'Z']]

我应该一次加密两个字母。当给定输入加密 (B, N) 时,结果输出应该是 DL。输入中的第一个字母应返回与 B 位于同一行的字母,但该字母具有 N 列。我希望有人可以解释一种方法让我完成此操作。

在友好用户的帮助下,部分代码如下所示:

def find_index(letter, table):
    for i,li in enumerate(table):
        try:
            j=li.index(letter)
            return i,j
        except ValueError:
            pass    

    raise ValueError("'{}' not in matrix".format(letter))
print "Row:Column"
print find_index('I', table)  


def encode(a, b):
    if a == b:
        print "ERROR: letters to encode_pair must be distinct"
    print find_index (a,table)
    print find_index (b, table)
4

5 回答 5

1

您可以在 python 中使用 ord() 获取 char 的 ASCII 值。

EX的

s='A'

change_s=chr(ord(s)+2)

所以结果是 change_s = 'C'

于 2013-10-29T02:49:55.287 回答
0

您需要保存 find_index (a,table) 和 find_index (a,table) 的返回值,如果我理解正确,这是您需要实现的代码类型

def encode(a, b):
    if a == b:
        print "ERROR: letters to encode_pair must be distinct"
    print find_index (a,table)
    print find_index (b, table) 
    index_a= find_index (a,table)
    index_b = find_index (a,table)
    new_a_index = [index_a[0], index_b[1]]
    new_b_index = [index_b[0], index_a[1]]
    new_a = table[new_a_index]
    new_b = table[new_b_index]

最后的步骤可以一步完成,但我试图澄清以确保您理解我在做什么,并在我误解时纠正我

于 2013-10-29T02:54:54.350 回答
0

这是你要找的:

In [25]: table = [['A', 'B', 'C', 'D', 'E'],
 ['F', 'G', 'H', 'I', 'Y'],
 ['K', 'L', 'M', 'N', 'O'],
 ['P', 'Q', 'R', 'S', 'T'],
 ['U', 'V', 'W', 'X', 'Z']]

In [26]: def encrypt(B, N):
    b = [L.index(B) for L in table if B in L][0]
    answer = [L for L in table if N in L][0][b]
    b = [L.index(N) for L in table if N in L][0]
    answer = answer,[L for L in table if B in L][0][b]
    return answer

In [27]: encrypt("B", "N")
Out[27]: ('L', 'D')
于 2013-10-29T02:44:41.487 回答
0

那很有趣……谢谢。

In [2]:



class Encoder(object):
    m = [['A', 'B', 'C', 'D', 'E'],
     ['F', 'G', 'H', 'I', 'Y'],
     ['K', 'L', 'M', 'N', 'O'],
     ['P', 'Q', 'R', 'S', 'T'],
     ['U', 'V', 'W', 'X', 'Z']]    

    def encode(self, first, second):
        first_row_idx, first_col_idx = self.get_rowcol(first)
        second_row_idx, second_col_idx = self.get_rowcol(second)        
        encoded_first = self.m[first_row_idx][second_col_idx]
        encoded_second = self.m[second_row_idx][first_col_idx]
        return encoded_first, encoded_second

    def get_rowcol(self, letter):
        letter = letter.upper()
        for row_idx, row in enumerate(self.m):
            for col_idx, col_letter in enumerate(row):
                if col_letter == letter:
                    return row_idx, col_idx
        raise ValueError("({}) not found in matrix!".format(letter))


e = Encoder()
e.encode("B", "N")

Out[2]:
('D', 'L')
于 2013-10-29T02:55:39.477 回答
0

可能是这个?

>>> from itertools import chain
>>> cl=[['A', 'B', 'C', 'D', 'E'],
 ['F', 'G', 'H', 'I', 'Y'],
 ['K', 'L', 'M', 'N', 'O'],
 ['P', 'Q', 'R', 'S', 'T'],
 ['U', 'V', 'W', 'X', 'Z']]
>>> M = list(chain(*cl))
>>> N = len(cl[0])
>>> def e(a, b):
        ia,ib = M.index(a), M.index(b)
        ar,ac = ia / N, ia % N
        br,bc = ib / N, ib % N
        return M[ar*N + bc], M[br*N + ac]

>>> e('B','N')
# ('D', 'L')
于 2013-10-29T02:57:41.673 回答