- 做CTF crypto时遇到了棋盘密码,写了个脚本
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
polybius = [[0,0,0,0,0,0],
[0,'A','B','C','D','E'],
[0,'F','G','H','I','K'],
[0,'L','M','N','O','P'],
[0,'Q','R','S','T','U'],
[0,'V','W','X','Y','Z']]
polybius2 = [[0,0,0,0,0,0],
[0,'b','t','a','l','p'],
[0,'d','h','o','z','k'],
[0,'q','f','v','s','n'],
[0,'g','i','c','u','x'],
[0,'m','r','e','w','y']]
def adfgx_to_num(my_str):
my_str = my_str.upper()
l_m = len(my_str)
s_num = ""
for i in range(l_m):
t = my_str[i]
if t == "A":
s_num += "1"
elif t == "D":
s_num += "2"
elif t == "F":
s_num += "3"
elif t == "G":
s_num += "4"
elif t == "X":
s_num += "5"
else:
pass
return s_num
def decode_polybius1(c_str):
s1 = ""
l_c = len(c_str)
for i in range(0,l_c,2):
t = c_str[i:i+2]
data = polybius[int(t[0])][int(t[1])]
s1 += data
print(s1)
def decode_polybius2(c_str):
c_str = adfgx_to_num(c_str)
s1 = ""
l_c = len(c_str)
for i in range(0,l_c,2):
t = c_str[i:i+2]
data = polybius2[int(t[0])][int(t[1])]
s1 += data
print(s1)
def main():
c_str = "2315313134"
decode_polybius1(c_str)
c_str = "DDXFAGAGDF"
decode_polybius2(c_str)
print("如果语境不通,i 改为 j")
if __name__ == "__main__":
main()
棋盘密码(polybius)
Author: felzl
Permalink: https://felzl.github.io/2021/10/30/%E6%A3%8B%E7%9B%98%E5%AF%86%E7%A0%81-polybius/
License: Copyright (c) 2019 CC-BY-NC-4.0 LICENSE
Slogan: Do you believe in DESTINY?