-
Notifications
You must be signed in to change notification settings - Fork 0
/
railfence.py
executable file
·53 lines (47 loc) · 975 Bytes
/
railfence.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
#!/usr/bin/python
# -*- encoding: utf-8 -*-
import sys
import re
def bars(src, bar_num):
result = ''
if len(src)%bar_num != 0:
num = len(src)/bar_num + 1
else:
num = len(src)/bar_num
for i in range(0,num):
for j in range(0,bar_num):
if (i+j*num) < len(src):
result+=src[i+j*num]
else:
result=result
return result
def main():
if len(sys.argv) == 1:
msg = '''
The Rail-Fence Cipher Cracker.
Useage:
railfence.py encoded-text [regexp]
regexp: only match results will be printed
'''
print msg.encode()
exit(0)
else:
results = []
src = sys.argv[1]
for i in range(2,len(src)):
results.append(bars(src,i))
#remove the repeated items
results = list(set(results))
if len(sys.argv) == 2:
for item in results:
print item
else:
regexp = sys.argv[2]
temp = []
for item in results:
if re.search(sys.argv[2], item):
temp.append(item)
for item in temp:
print item
if __name__ == '__main__':
main()