-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathruncryptkicker.py
135 lines (111 loc) · 3.66 KB
/
runcryptkicker.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
from cryptkicker.cryptkicker import CryptKicker
import codecs
reload(sys)
sys.setdefaultencoding('utf-8')
def decrypt(phrase, seed):
"""
:param phrase: phrase to be decrypted
:param seed: seed that indictes a sentence that we know is within phrase
:return:
"""
ck = CryptKicker(phrase, seed)
return ck.get_decrypted_text()
def parse_input(file_input):
"""
receives a file handler and parses the number of phrases and the phrases to be __decrypted
:param file_input: file to be used as input
TODO: add checks for maximum 100 phrases
TODO: add checks for maximum 100 chars length for each line
"""
class States(object):
"""
class used to emulate an enumeration
"""
SEARCH_NUM, SEARCH_BLANK, SEARCH_PHRASE, IN_PHRASE, END, ABORTING = range(6)
state = States.SEARCH_NUM
remaining_phrases = 0
phrases = list()
current_phrase = str()
lines = file_input.readlines()
for line in lines:
if state == States.SEARCH_NUM:
# print "CS: SEARCH_NUM"
# noinspection PyUnusedLocal
try:
remaining_phrases = int(line.strip())
if remaining_phrases:
state = States.SEARCH_BLANK
# print "NS: SEARCH_BLANK"
else:
# print "Number of phrases is 0"
state = States.END
# print "NS: END"
except ValueError as e:
# print "NS: ABORTING"
state = States.ABORTING
elif state == States.SEARCH_BLANK:
# print "CS: SEARCH_BLANK"
if line.strip() == '':
state = States.SEARCH_PHRASE
# print "NS: SEARCH_PHRASE"
elif state == States.SEARCH_PHRASE:
# print "CS: SEARCH_PHRASE"
if line.strip() == '':
pass
elif not current_phrase:
current_phrase += line.strip()
state = States.IN_PHRASE
# print "NS: IN_PHRASE"
elif state == States.IN_PHRASE:
# print "CS: SEARCH_PHRASE"
if line.strip() == '':
phrases.append(current_phrase)
current_phrase = ''
remaining_phrases -= 1
state = States.SEARCH_PHRASE
if not remaining_phrases:
state = States.END
# print "NS: END"
else:
current_phrase += " " + line.strip()
elif state == States.END:
# print "CS: END"
pass
elif state == States.ABORTING:
# print "CS: ABORTING"
break
if current_phrase:
phrases.append(current_phrase)
return phrases
def main(args):
#process each of the files submitted as input
"""
main function
:param args:
"""
try:
fpath = args[0]
seed = args[1]
except IndexError:
print "usage: runcryptkicker.py input_file \"seed_phrase\""
sys.exit(-1)
phrases = None
try:
print "Processing: %s file" % fpath
phrases = parse_input(codecs.open(fpath, 'r', encoding='latin-1'))
except IOError, e:
print e
if not phrases:
print "No phrases to be decrypted. Quitting"
sys.exit(-1)
# decrypt each phrase individually
#seed = u"la zorra cafe rapidamente brinco sobre el perro negro"
print "phrase seed = %s\n" % seed
for phrase in phrases:
print decrypt(phrase, seed)
print ""
if __name__ == "__main__":
main(sys.argv[1:])