-
Notifications
You must be signed in to change notification settings - Fork 55
/
Four-Square.py
97 lines (80 loc) · 2.4 KB
/
Four-Square.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# -*- coding: utf-8 -*-
#
# Four-Square Cipher
#
# @author lellansin <lellansin@gmail.com>
# @website http://www.lellansin.com/tutorials/ciphers
#
import re
# 默认密表
table = [['A', 'B', 'C', 'D', 'E'],
['F', 'G', 'H', 'I', 'J'],
['K', 'L', 'M', 'N', 'O'],
['P', 'R', 'S', 'T', 'U'],
['V', 'W', 'X', 'Y', 'Z']]
#
# 生成棋盘
#
def generate_table(key = ''):
# wiki原文:usually omitting "Q" or putting both "I" and "J" in the same location to reduce the alphabet to fit
alphabet = 'ABCDEFGHIJKLMNOPRSTUVWXYZ'
table = [[0] * 5 for row in range(5)]
key = re.sub(r'[\W]', '', key).upper()
for row in range(5):
for col in range(5):
if len(key):
table[row][col] = key[0]
alphabet = alphabet.replace(key[0], '')
key = key.replace(key[0], '')
else:
table[row][col] = alphabet[0]
alphabet = alphabet[1:]
return table
#
# 加密
#
def encrypt(keys, words):
ciphertext = ''
words = re.sub(r'[\W]', '', words).upper().replace('Q', '')
R, L = generate_table(key[0]), generate_table(key[1])
for i in range(0, len(words), 2):
digraphs = words[i:i+2]
ciphertext += mangle(R, L, digraphs)
return ciphertext
def mangle(R, L, digraphs):
a = position(table, digraphs[0])
b = position(table, digraphs[1])
return R[a[0]][b[1]] + L[b[0]][a[1]]
#
# 解密
#
def decrypt(keys, words):
ciphertext = ''
words = re.sub(r'[\W]', '', words).upper().replace('Q', '')
R, L = generate_table(key[0]), generate_table(key[1])
for i in range(0, len(words), 2):
digraphs = words[i:i+2]
ciphertext += unmangle(R, L, digraphs)
return ciphertext.lower()
def unmangle(R, L, digraphs):
a = position(R, digraphs[0])
b = position(L, digraphs[1])
return table[a[0]][b[1]] + table[b[0]][a[1]]
# todo
def position(table, ch):
for row in range(5):
for col in range(5):
if table[row][col] == ch:
return (row, col)
return (None, None)
if __name__ == '__main__':
# 本例推算见 http://en.wikipedia.org/wiki/Four-square_cipher
# 明文
plaintext = 'help me obiwankenobi'
# 密匙
key = ['example', 'keyword']
# 加密
ciphertext = encrypt(key, plaintext)
print(ciphertext)
# 解密
print(decrypt(key, ciphertext))