-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathencoder-decoder.py
241 lines (179 loc) · 6.05 KB
/
encoder-decoder.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import base64
import hashlib
import os
import sys
import binascii
def xor(data, key):
return bytearray(a^b for a, b in zip(*map(bytearray, [data, key])))
def md5(string_to_hash):
return hashlib.md5(string_to_hash).hexdigest()
def sha1(string_to_hash):
return hashlib.sha1(string_to_hash).hexdigest()
def sha224(string_to_hash):
return hashlib.sha224(string_to_hash).hexdigest()
def sha256(string_to_hash):
return hashlib.sha256(string_to_hash).hexdigest()
def sha384(string_to_hash):
return hashlib.sha384(string_to_hash).hexdigest()
def sha512(string_to_hash):
return hashlib.sha512(string_to_hash).hexdigest()
def hash_all(string_to_hash):
print ("\n")
print ("MD5 : " + str(md5(string_to_hash)))
print ("SHA1 : " + str(sha1(string_to_hash)))
print ("SHA224 : " + str(sha224(string_to_hash)))
print ("SHA256 : " + str(sha256(string_to_hash)))
print ("SHA384 : " + str(sha384(string_to_hash)))
print ("SHA512 : " + str(sha512(string_to_hash)))
def en_base2(string_to_encode):
return "0" + bin(int(binascii.hexlify(string_to_encode), 16))[2:]
def de_base2(string_to_decode):
try:
s = int(string_to_decode, 2)
binascii.unhexlify('%x' % s)
except (TypeError, ValueError) :
print ("Non-Binary String Provided")
def en_base16(string_to_encode):
return base64.b16encode(string_to_encode)
def de_base16(string_to_decode):
try:
if string_to_decode[:2].lower() == '0x':
string_to_decode = string_to_decode[2:]
decoded_string = base64.b16decode(string_to_decode)
return decoded_string
except TypeError:
print ("Non-Hexadecimal String Provided")
def en_base32(string_to_encode):
return base64.b32encode(string_to_encode)
def de_base32(string_to_decode):
try:
decoded_string = base64.b32decode(string_to_decode)
return decoded_string
except TypeError:
print ("Non-Base32 String Provided")
def en_base64(string_to_encode):
return base64.b64encode(string_to_encode)
def de_base64(string_to_decode):
try:
decoded_string = base64.b64decode(string_to_decode)
return decoded_string
except TypeError:
print ("Non-Base64 String Provided")
def en_all(string_to_encode):
print ("Binary : " + str(en_base2(string_to_encode)))
print ("Base16 : " + str(en_base16(string_to_encode)))
print ("Base32 : " + str(en_base32(string_to_encode)))
print ("Base64 : " + str(en_base64(string_to_encode)))
def de_all(string_to_decode, string_type):
if string_type == "b":
print ("Decoded String : " + str(de_base2(string_to_decode)))
elif string_type == "b16":
print ("Decoded String : " + str(de_base16(string_to_decode)))
elif string_type == "b32":
print ("Decoded String : " + str(de_base32(string_to_decode)))
elif string_type == "b64":
print ("Decoded String : " + str(de_base64(string_to_decode)))
else:
print ("Error")
def get_checksum(file_path, hash_method="all"):
if hash_method == "md5":
hash_ = hashlib.md5()
elif hash_method == "sha1":
hash_ = hashlib.sha1()
elif hash_method == "sha224":
hash_ = hashlib.sha224()
elif hash_method == "sha256":
hash_ = hashlib.sha256()
elif hash_method == "sha384":
hash_ = hashlib.sha384()
elif hash_method == "sha512":
hash_ = hashlib.sha512()
if hash_method == "all":
for hash_name, hash_element in {"MD5 ": hashlib.md5(), "SHA1 ": hashlib.sha1(), "SHA224 ": hashlib.sha224(),
"SHA256 ": hashlib.sha256(), "SHA384 ": hashlib.sha384(),
"SHA512 ": hashlib.sha512()}.iteritems():
with open(file_path, "rb") as f:
for chunk in iter(lambda: f.read(4096), b""):
hash_element.update(chunk)
print (hash_name + ":" + str(hash_element.hexdigest()))
else:
with open(file_path, "rb") as f:
for chunk in iter(lambda: f.read(4096), b""):
hash_.update(chunk)
print (hash_method.upper() + " : " + str(hash_.hexdigest()))
# Main menu
def main_menu():
print ("1. Encode")
print ("2. Decode")
print ("3. Checksum")
print ("4. Hash")
print ("5. XoR")
print ("\n0. Quit")
choice = str(raw_input(" >> "))
exec_menu(choice)
return
# Execute menu
def exec_menu(choice):
print ("\n")
ch = choice.lower()
if ch == '':
menu_actions['main_menu']()
else:
try:
menu_actions[ch]()
except KeyError:
print "Invalid selection, please try again.\n"
menu_actions['main_menu']()
return
# Encode Menu
def encode_menu():
string_to_encode = str(raw_input("Enter the string you want to ENCODE : "))
en_all(string_to_encode)
exec_menu("9")
return
# Decode Menu
def decode_menu():
string_to_decode = str(raw_input("Enter the string you want to DECODE : "))
string_type = str(raw_input("Enter the string type (b: Binary, b16: Base16, b32: Base32, b64: Base64) : "))
de_all(string_to_decode, string_type)
exec_menu("9")
return
# Checksum Menu
def checksum_menu():
path_of_file = str(raw_input("Path of file : "))
get_checksum(path_of_file)
exec_menu("9")
return
# Hash Menu
def hash_menu():
string_to_hash = str(raw_input("Enter the string you want to HASH : "))
hash_all(string_to_hash)
exec_menu("9")
return
# Xor Menu
def xor_menu():
string_to_xor = str(raw_input("Enter the string you want to XOR : "))
key = str(raw_input("Key : "))
print ("Results : " + str(xor(string_to_xor, key)))
exec_menu("9")
return
# Back to main menu
def back():
menu_actions['main_menu']()
# Exit program
def exit_():
sys.exit()
# Menu definition
menu_actions = {
'main_menu': main_menu,
'1': encode_menu,
'2': decode_menu,
'3': checksum_menu,
'4': hash_menu,
'5': xor_menu,
'9': back,
'0': exit_,
}
# Main Program
if __name__ == "__main__":
main_menu()