-
Notifications
You must be signed in to change notification settings - Fork 55
/
Bifid.py
62 lines (47 loc) · 1.43 KB
/
Bifid.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# -*- coding: utf-8 -*-
#
# Bifid Cipher
#
# @author lellansin <lellansin@gmail.com>
# @website http://www.lellansin.com/tutorials/ciphers
#
import Polybius
#
# 加密
#
def encrypt(table, words):
string = table
cipher_row, cipher_col = '', ''
for ch in words.upper():
for row in range(len(table)):
if ch in table[row]:
cipher_row += str(row + 1)
cipher_col += str((table[row].index(ch) + 1))
return Polybius.decrypt(table, cipher_row + cipher_col)
#
# 解密
#
def decrypt(table, text):
numbers = ''
text = Polybius.encrypt(table, text)
a, b = text[:len(text) / 2], text[len(text) / 2:]
numbers = ''.join(a[i] + b[i] for i in range(len(a)))
return Polybius.decrypt(table, numbers)
if __name__ == '__main__':
# 本例推算见 http://en.wikipedia.org/wiki/Bifid_cipher
# 初始化棋盘(也可以用 polybius.generate_table 来生成一个随机的)
table = [['B', 'G', 'W', 'K', 'Z'],
['Q', 'P', 'N', 'D', 'S'],
['I', 'O', 'A', 'X', 'E'],
['F', 'C', 'L', 'U', 'M'],
['T', 'H', 'Y', 'V', 'R']]
# 输出棋盘
Polybius.print_table(table)
# 密文
text = "F L E E A T O N C E"
# 使用棋盘加密字符串
ciphertext = encrypt(table, text)
# 密文
print('密文: ' + ciphertext)
# 解密字符串
print('解密: ' + decrypt(table, ciphertext))