-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModified_PyTEA.py
404 lines (193 loc) · 7.26 KB
/
Modified_PyTEA.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
#!/usr/bin/env python
#################################################################################
# Python implementation of the Tiny Encryption Algorithm (TEA)
# By Moloch
#
# About: TEA has a few weaknesses. Most notably, it suffers from
# equivalent keys each key is equivalent to three others,
# which means that the effective key size is only 126 bits.
# As a result, TEA is especially bad as a cryptographic hash
# function. This weakness led to a method for hacking Microsoft's
# Xbox game console (where I first encountered it), where the
# cipher was used as a hash function. TEA is also susceptible
# to a related-key attack which requires 2^23 chosen plaintexts
# under a related-key pair, with 2^32 time complexity.
#
# Block size: 64bits
# Key size: 128bits
#
##################################################################################
import os
import getpass
import platform
import struct
from random import choice
from hashlib import sha256
from ctypes import c_uint32
from string import ascii_letters, digits
if platform.system().lower() in ['linux', 'darwin']:
INFO = "\033[1m\033[36m[*]\033[0m "
WARN = "\033[1m\033[31m[!]\033[0m "
else:
INFO = "[*] "
WARN = "[!] "
### Magical Constants
DELTA = 0x9e3779b9
SUMATION = 0xc6ef3720
ROUNDS = 32
BLOCK_SIZE = 2 # number of 32-bit ints
KEY_SIZE = 4
### Functions ###
def encrypt_block(block, key, verbose=False):
'''
Encrypt a single 64-bit block using a given key
@param block: list of two c_uint32s
@param key: list of four c_uint32s
'''
assert len(block) == BLOCK_SIZE
assert len(key) == KEY_SIZE
sumation = c_uint32(0)
delta = c_uint32(DELTA)
for index in range(0, ROUNDS):
sumation.value += delta.value
block[0].value += ((block[1].value << 4) + key[0].value) ^ (block[1].value + sumation.value) ^ ((block[1].value >> 5) + key[1].value)
block[1].value += ((block[0].value << 4) + key[2].value) ^ (block[0].value + sumation.value) ^ ((block[0].value >> 5) + key[3].value)
if verbose: print("\t--> Encrypting block round %d of %d" % (index + 1, ROUNDS))
return block
def decrypt_block(block, key, verbose=False):
'''
Decrypt a single 64-bit block using a given key
@param block: list of two c_uint32s
@param key: list of four c_uint32s
'''
assert len(block) == BLOCK_SIZE
assert len(key) == KEY_SIZE
sumation = c_uint32(SUMATION)
delta = c_uint32(DELTA)
for index in range(0, ROUNDS):
block[1].value -= ((block[0].value << 4) + key[2].value) ^ (block[0].value + sumation.value) ^ ((block[0].value >> 5) + key[3].value);
block[0].value -= ((block[1].value << 4) + key[0].value) ^ (block[1].value + sumation.value) ^ ((block[1].value >> 5) + key[1].value);
sumation.value -= delta.value
if verbose: print("\t<-- Decrypting block round %d of %d" % (index + 1, ROUNDS))
return block
def to_c_array(data):
''' Converts a string to a list of c_uint32s '''
c_array = []
for index in range(0, len(data)/4):
chunk = data[index*4:index*4+4]
packed = struct.unpack(">L", chunk)[0]
c_array.append(c_uint32(packed))
return c_array
def to_string(c_array):
''' Converts a list of c_uint32s to a Python (ascii) string '''
output = ''
for block in c_array:
output += struct.pack(">L", block.value)
return output
def random_chars(nchars):
chars = ''
for n in range(0, nchars):
chars += choice(ascii_letters + digits)
return chars
def add_padding(data, verbose=False):
pad_delta = 4 - (len(data) % 4)
if verbose:
print(INFO + "Padding delta: %d" % pad_delta)
data += random_chars(pad_delta)
data += "%s%d" % (random_chars(3), pad_delta)
return data
def encrypt(data, key, verbose=False):
'''
Encrypt string using TEA algorithm with a given key
'''
data = add_padding(data, verbose)
data = to_c_array(data)
key = to_c_array(key.encode('ascii', 'ignore'))
cipher_text = []
for index in range(0, len(data), 2):
if verbose:
print(INFO + "Encrypting block %d" % index)
block = data[index:index + 2]
block = encrypt_block(block, key, verbose)
for uint in block:
cipher_text.append(uint)
if verbose:
print(INFO + "Encryption completed successfully")
return to_string(cipher_text)
def decrypt(data, key, verbose=False):
data = to_c_array(data)
key = to_c_array(key.encode('ascii', 'ignore'))
plain_text = []
for index in range(0, len(data), 2):
if verbose:
print(INFO + "Encrypting block %d" % index)
block = data[index:index + 2]
decrypted_block = decrypt_block(block, key, verbose)
for uint in decrypted_block:
plain_text.append(uint)
data = to_string(plain_text)
if verbose:
print(INFO + "Decryption compelted successfully")
return data
def get_key(password=''):
''' Generate a key based on user password '''
if 0 == len(password):
password = getpass.getpass(INFO + "Password: ")
sha = sha256()
sha.update(password + "Magic Static Salt")
sha.update(sha.hexdigest())
return ''.join([char for char in sha.hexdigest()[::4]])
def encrypt_file(fpath, key, verbose=False):
with open(fpath, 'rb+') as fp:
data = fp.read()
cipher_text = encrypt(data, key, verbose)
fp.seek(0)
fp.write(cipher_text)
fp.close()
def decrypt_file(fpath, key, verbose=False):
with open(fpath, 'rb+') as fp:
data = fp.read()
plain_text = decrypt(data, key, verbose)
fp.close()
fp = open(fpath, 'w')
fp.write(plain_text)
fp.close()
### UI Code ###
if __name__ == '__main__':
from argparse import ArgumentParser
parser = ArgumentParser(
description='Python implementation of the TEA cipher',
)
parser.add_argument('-e', '--encrypt',
help='encrypt a file',
dest='epath',
default=None
)
parser.add_argument('-d', '--decrypt',
help='decrypt a file',
dest='dpath',
default=None
)
parser.add_argument('--verbose',
help='display verbose output',
default=False,
action='store_true',
dest='verbose'
)
args = parser.parse_args()
if args.epath is None and args.dpath is None:
print('Error: Must use --encrypt or --decrypt')
elif args.epath is not None:
print(WARN + 'Encrypt Mode: The file will be overwritten')
if os.path.exists(args.epath) and os.path.isfile(args.epath):
key = get_key()
encrypt_file(args.epath, key, args.verbose)
else:
print(WARN + 'Error: target does not exist, or is not a file')
elif args.dpath is not None:
print(WARN + 'Decrypt Mode: The file will be overwritten')
if os.path.exists(args.dpath) and os.path.isfile(args.dpath):
key = get_key()
decrypt_file(args.dpath, key, args.verbose)
else:
print(WARN + 'Error: target does not exist, or is not a file')