-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmultiencoder.py
179 lines (152 loc) · 5.56 KB
/
multiencoder.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/usr/bin/env python2.7
#
# UI for the MultiEncoder
#
# Author: Deque
# Co-author : Psycho_Coder
# URL: https://github.com/HCDevelopers/MultiEncoder
# For license information, see LICENSE
import hcdev
import sys
encrypters = hcdev.get_encryptors()
encoders = hcdev.get_encoders()
abbreviations = hcdev.get_abbrevs()
title = """
__ __ _ _ _ ______ _
| \/ | | | | (_) ____| | |
| \ / |_ _| | |_ _| |__ _ __ ___ ___ __| | ___ _ __
| |\/| | | | | | __| | __| | '_ \ / __/ _ \ / _` |/ _ \ '__|
| | | | |_| | | |_| | |____| | | | (_| (_) | (_| | __/ |
|_| |_|\__,_|_|\__|_|______|_| |_|\___\___/ \__,_|\___|_|
Version 0.4 at Hackcommunity.com
Authors: Ex094, noize, Psycho_Coder, Deque
"""
def get_algorithms():
"""Asks the user for the encoding and encryption algorithms that shall be
used to encode or decode some text.
Returns a list of the encoders and encrypters the user wants to apply.
"""
userargs = []
while True:
userin = raw_input("Encodings/encryptions (type '-i algo-name' to get a description): ")
userargs = userin.split()
if len(userargs) >= 2 and userargs[0] == "-i":
print_description(userargs[1])
else:
break
return clean_list(userargs)
def print_description(algorithm):
"""Prints a description of the given algorithm
The algorithm instance must have the attribute description, otherwise
it prints that no description is available.
"""
enc = None
alglist = clean_list([algorithm])
if len(alglist) == 1:
alg = alglist[0]
if alg in encoders:
enc = encoders[alg]
elif alg in encrypters:
enc = encrypters[alg]
if hasattr(enc, "description"):
print("\n-----------------------------------------------------------------")
print(enc.description())
print("-----------------------------------------------------------------")
else:
print("No description available for " + alg)
print("\n")
def print_algorithms():
"""Prints a list of all available encoders and encrypters, including a (-i)
if a description is available
"""
print("Available encodings:\n"),
for algo in encoders.keys():
print(algo),
if hasattr(encoders[algo], "description"):
print "(-i)",
print("\n\nAvailable encryptions:\n"),
for algo in encrypters.keys():
print(algo),
if hasattr(encrypters[algo], "description"):
print("(-i)"),
print("\n")
def en_de_code(is_decode, text, algorithms):
"""Takes a list of algorithms and performs encoding or decoding on the given
text, depending on the is_decode argument.
The encoding or decoding is performed in the order the algorithms are given.
Intermediate results and end result are printed.
"""
print
for alg in algorithms:
if alg in encoders:
encoder = encoders[alg]
function = encoder.decode if is_decode else encoder.encode
text = function(text)
if alg in encrypters:
encrypter = encrypters[alg]
key = ask_for_key(alg, encrypter)
function = encrypter.decode if is_decode else encrypter.encode
text = function(text, key)
print(" > " + alg + " : " + text)
print("\nResult: " + text)
def ask_for_key(algorithm, gen):
"""Asks the user to input or generate a key.
Returns the key.
"""
if hasattr(gen, "generate_key"):
print "Key for", algorithm, "(type '-gen' to generate):",
key = raw_input()
if key == "-gen":
key = gen.generate_key()
print("\nGenerated key: %s \n" % key)
else:
print("Key for " + algorithm + " : "),
key = raw_input()
return key
def clean_list(dirtylist):
"""Takes a list of algorithm names as the user put them in.
Turns abbreviations in to their non-abbreviated form.
Returns a list of the corresponding encoders or encrypters in the same
order as given in the input list.
Returns an empty list if any of the input elements is invalid (not found).
"""
cleanlist = []
for element in dirtylist:
if element in abbreviations:
element = abbreviations[element]
if element in encoders or element in encrypters:
cleanlist.append(element)
# one element couldn't be found, so return empty list
else:
print("\ncouldn't find " + element)
return []
return cleanlist
def ask_is_decode():
"""Ask user if he/she wants to decode or encode until appropriate answer
was given
"""
while True:
user_input = raw_input("Encode (e) or decode (d)?")
if user_input == "d" or user_input == "D":
return True
if user_input == "e" or user_input == "E":
return False
print("\nCan not recognize your input " + user_input)
def main():
print title
print_algorithms()
# ask for algorithms until the user gets it right
algorithms = get_algorithms()
while not algorithms:
algorithms = get_algorithms()
# ask if decode or encode
is_decode = ask_is_decode()
# ask for text to decode or encode
text = raw_input("Text: ")
# apply encoding or decoding to text
try:
en_de_code(is_decode, text, algorithms)
except hcdev.EncodeInputError as e:
sys.stderr.write("Input Error! " + e.msg + "\n")
if __name__ == "__main__":
main()