-
Notifications
You must be signed in to change notification settings - Fork 0
/
MOTP.py
383 lines (363 loc) · 21.4 KB
/
MOTP.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
# coding=utf-8
"""
Modified One-Time Pad
Compiled with <3 by an anonymous coder.
GitHub repository: https://github.com/An-anonymous-coder/MOTP
FUNCTIONS
---------
encrypt(file_path, password, verbose): Encrypts any file. Returns nothing.
decrypt(file_path, password, verbose): Decrypts any file. Returns nothing.
destroy(file_path, verbose): Destroys the decryption key for a file. Returns nothing.
"""
import os
import sys
import time
import typing
import numpy # https://numpy.org/install/
# I hate `tkinter`'s inconsistency.
if sys.version_info.major < 3 or sys.version_info.minor < 3: # For Python 3.2.x and below (Untested)
import tkSimpleDialog as dialog
elif sys.version_info.minor < 6: # For Python 3.3.x through 3.5.x (Untested)
from tkinter import simpledialog as dialog
else: # For Python 3.6.x and above (Tested Python 3.10.6)
import tkinter
import tkinter.simpledialog as dialog
tkinter.Tk().withdraw() # Someone said this was necessary (It worked without in Python 3.10.6)
red = '\u001b[38;2;255;0;0m'
yellow = '\u001b[38;2;255;255;0m'
green = '\u001b[38;2;0;255;0m'
cyan = '\u001b[38;2;0;255;255m'
blue = '\u001b[38;2;0;127;255m' # Technically azure
grey = '\u001b[38;2;127;127;127m'
bold = '\u001b[1m'
reset = '\u001b[0m'
def encrypt(file_path: typing.Union[str, None] = None, password: typing.Union[str, None] = None,
verbose: bool = False) -> None:
"""
This function encrypts any file.
:param file_path: This is the file path for the file to be encrypted. If none is provided, you will be prompted for
one. Defaults to None.
:type file_path: str or None
:param password: This is the password used to encrypt the file. If none is provided, you will be prompted for one.
Defaults to None.
:type password: str or None
:param bool verbose: If this is set to True, this will print what the function is doing at each step. Otherwise,
those print statements are hidden. Defaults to False.
"""
if not file_path:
if verbose:
print('{blue}[v] {yellow}No file provided.{reset}'.format(blue=blue, yellow=yellow, reset=reset))
file_path = input('{bold}Enter the path of the file to encrypt: {reset}'.format(bold=bold, reset=reset))
elif verbose:
print('{blue}[v] {green}File provided.{reset}'.format(blue=blue, green=green, reset=reset))
while True:
if verbose:
print('{blue}[v] {grey}Checking file...{reset}'.format(blue=blue, grey=grey, reset=reset))
if os.path.isfile(file_path) and os.path.exists(file_path):
if verbose:
print('{blue}[v] {green}File is valid.{reset}'.format(blue=blue, green=green, reset=reset))
break
else:
print('{red}File is invalid.{reset}'.format(red=red, reset=reset))
file_path = input('{bold}Enter the path of the file to encrypt: {reset}'.format(bold=bold, reset=reset))
decrypted_file_name = os.path.basename(file_path)
if not password:
if verbose:
print('{blue}[v] {yellow}Password not provided.{reset}'.format(blue=blue, yellow=yellow, reset=reset))
while True:
try:
password = dialog.askstring("Password", 'Enter the password to encrypt the file:', show='*')
if not password:
print('{red}Operation canceled.{reset}'.format(red=red, reset=reset))
exit()
confirmation = dialog.askstring("Password", 'Enter the password again to verify it:', show='*')
if not confirmation:
print('{red}Operation canceled.{reset}'.format(red=red, reset=reset))
exit()
except ModuleNotFoundError: # Please contact me if this happens
password = input('{bold}Enter the password to encrypt the file: {reset}'.format(bold=bold, reset=reset))
confirmation = input(
'{bold}Enter the password again to verify it: {reset}'.format(bold=bold, reset=reset))
if confirmation == password:
if verbose:
print('{blue}[v] {green}Password verified.{reset}'.format(blue=blue, green=green, reset=reset))
break
print('{red}Passwords do not match.{reset}'.format(red=red, reset=reset))
elif verbose:
print('{blue}[v] {green}Password provided.{reset}'.format(blue=blue, green=green, reset=reset))
if verbose:
print('{blue}[v] {grey}Generating decryption key...{reset}'.format(blue=blue, grey=grey, reset=reset))
decryption_key = str(numpy.random.default_rng().random()) # This generates a random decryption key.
if verbose:
print('{blue}[v] {cyan}Decryption key: {reset}{bold}{key}{reset}'.format(blue=blue, cyan=cyan, bold=bold,
reset=reset,
key=decryption_key))
if verbose:
print('{blue}[v] {grey}Opening file... ({green}{path}{grey}){reset}'.format(blue=blue, grey=grey, green=green,
reset=reset, path=file_path))
with open(file_path, 'rb') as decrypted_file:
encrypted_file_name = decrypted_file_name + '.MOTP'
# This generates a file name compatible with the `decrypt` function.
encrypted_file_path = file_path.rstrip(decrypted_file_name) + encrypted_file_name
# This generates the file path used to store the encrypted file.
if os.path.exists(encrypted_file_path):
print(
'{red}[!] File {green}{path} {red}already exists.{reset}\n'.format(red=red, green=green, reset=reset,
path=encrypted_file_path))
def generate_pad(seed, size: int) -> numpy.ndarray:
"""
This generates a pseudorandom pad for the encryption.
:param seed: This is the default_rng seed to set the random outcome
:type seed: <class 'numpy.random._generator.Generator'>
:param int size: This is the size of the pad.
:return: Returns a numpy.ndarray of the pad.
:rtype: numpy.ndarray
"""
return seed.integers(0, 256, size, dtype=numpy.uint8)
def apply_pad(data: bytes, pad: numpy.ndarray) -> numpy.ndarray:
"""
This is the function that encrypts the data.
:param bytes data: This is the data to encrypt.
:param numpy.ndarray pad: This is the pad to encrypt the data with.
:return: Returns the encrypted data.
:rtype: numpy.ndarray
"""
return bytearray(data) ^ pad
if verbose:
print('{blue}[v] {grey}Creating file... ({green}{path}{grey}){reset}'.format(blue=blue, grey=grey,
green=green, reset=reset,
path=encrypted_file_path))
file_size = os.path.getsize(file_path)
max_size = 1024 * 1024 * 1024
# This is the maximum size (in bytes) per chunk. This can be higher based on your RAM.
# THE FILE MUST BE DECRYPTED WITH THE SAME CHUNK SIZE YOU ENCRYPTED IT WITH.
with open(encrypted_file_path, 'wb') as encrypted_file:
try:
encrypted_file.write((decryption_key + '\n').encode())
# The newline is used to separate the decryption key from the rest of the file.
rng = numpy.random.default_rng(seed=int.from_bytes(bytes((password + decryption_key).encode()), 'big'))
# This randomizes the outcome of the encryption in a reversible way.
if verbose:
print('{blue}[v] {grey}Encrypting file... ({cyan}{size} bytes{grey}){reset}'.format(blue=blue,
grey=grey,
cyan=cyan,
reset=reset,
size=file_size))
start = time.perf_counter() # This starts a timer to time the encryption.
for _ in range(int(file_size / max_size)):
encrypted_file.write(apply_pad(decrypted_file.read(max_size), generate_pad(rng, max_size)))
excess_size = file_size % max_size
encrypted_file.write(apply_pad(decrypted_file.read(excess_size), generate_pad(rng, excess_size)))
end = time.perf_counter()
if verbose:
seconds = end - start
speed = round(file_size / seconds)
message = '{blue}[v] {grey}Encrypted in {seconds} seconds. ({cyan}{speed:,} bytes per second{grey})'
print((message + reset).format(blue=blue, grey=grey, cyan=cyan, seconds=round(seconds, 2),
speed=speed))
except Exception as exception:
encrypted_file.close()
os.remove(encrypted_file_path) # This removes the partially encrypted file.
decrypted_file.close()
raise exception
if verbose:
print('{blue}[v] {grey}Closing files...{reset}'.format(blue=blue, grey=grey, reset=reset))
encrypted_file.close()
decrypted_file.close()
if verbose:
print('{blue}[v] {grey}Cleaning up...{reset}'.format(blue=blue, grey=grey, reset=reset))
os.remove(file_path) # This removes the decrypted file.
print('Encrypted as: {green}{name}{reset}'.format(green=green, reset=reset, name=encrypted_file_name))
if verbose:
print('{blue}[v] {green}Done!{reset}'.format(blue=blue, green=green, reset=reset))
print()
def decrypt(file_path: typing.Union[str, None] = None, password: typing.Union[str, None] = None,
verbose: bool = False) -> None:
"""
This function decrypts a file encrypted with the `encrypt` function.
:param file_path: This is the file path for the file to be decrypted. If none is provided, you will be prompted for
one. Defaults to None.
:type file_path: str or None
:param password: This is the password used to decrypt the file. If none is provided, you will be prompted for one.
Defaults to None.
:type password: str or None
:param bool verbose: If this is set to True, this will print what the function is doing at each step. Otherwise,
those print statements are hidden. Defaults to False.
"""
if not file_path:
if verbose:
print('{blue}[v] {yellow}No file provided.{reset}'.format(blue=blue, yellow=yellow, reset=reset))
file_path = input('{bold}Enter the path of the file to decrypt: {reset}'.format(bold=bold, reset=reset))
elif verbose:
print('{blue}[v] {green}File provided.{reset}'.format(blue=blue, green=green, reset=reset))
while True:
if verbose:
print('{blue}[v] {grey}Checking file...{reset}'.format(blue=blue, grey=grey, reset=reset))
if os.path.isfile(file_path) and os.path.exists(file_path) and file_path.endswith('.MOTP'):
if verbose:
print('{blue}[v] {green}File is valid.{reset}'.format(blue=blue, green=green, reset=reset))
break
else:
print('{red}File is invalid.{reset}'.format(red=red, reset=reset))
file_path = input('{bold}Enter the path of the file to decrypt: {reset}'.format(bold=bold, reset=reset))
decrypted_file_name = os.path.splitext(os.path.basename(file_path))[0]
decrypted_file_path = file_path.rstrip(os.path.basename(file_path)) + decrypted_file_name
# This generates the file path used to store the decrypted file.
if os.path.exists(decrypted_file_path):
print('{red}[!] File {green}{path} {red}already exists.{reset}\n'.format(red=red, green=green, reset=reset,
path=decrypted_file_path))
return
if verbose:
print('{blue}[v] {grey}Opening file...{reset}'.format(blue=blue, grey=grey, reset=reset))
with open(file_path, 'rb') as encrypted_file:
if verbose:
print('{blue}[v] {grey}Building decryption key...{reset}'.format(blue=blue, grey=grey, reset=reset))
decryption_key = encrypted_file.readlines(1)[0].decode().rstrip()
try:
float(decryption_key)
except ValueError:
print('{red}[!] Decryption key is invalid or destroyed.{reset}'.format(red=red, reset=reset))
return
if not password:
if verbose:
print('{blue}[v] {yellow}No password provided.{reset}'.format(blue=blue, yellow=yellow, reset=reset))
try:
password = dialog.askstring("Password", 'Enter the password to decrypt the file:', show='*')
if not password:
print('{red}Operation canceled.{reset}'.format(red=red, reset=reset))
exit()
except ModuleNotFoundError: # Please contact me if this happens
password = input('{bold}Enter the password to decrypt the file: {reset}'.format(bold=bold, reset=reset))
elif verbose:
print('{blue}[v] {green}Password provided.{reset}'.format(blue=blue, green=green, reset=reset))
def generate_pad(seed, size: int) -> numpy.ndarray:
"""
This generates a pseudorandom pad for the decryption.
:param seed: This is the default_rng seed to set the random outcome
:type seed: <class 'numpy.random._generator.Generator'>
:param int size: This is the size of the pad.
:return: Returns a numpy.ndarray of the pad.
:rtype: numpy.ndarray
"""
return seed.integers(0, 256, size, dtype=numpy.uint8)
def apply_pad(data: bytes, pad: numpy.ndarray) -> numpy.ndarray:
"""
This is the function that decrypts the data.
:param bytes data: This is the data to decrypt.
:param numpy.ndarray pad: This is the pad to decrypt the data with.
:return: Returns the decrypted data.
:rtype: numpy.ndarray
"""
return bytearray(data) ^ pad
if verbose:
print('{blue}[v] {grey}Creating file... ({green}{path}{grey}){reset}'.format(blue=blue, grey=grey,
green=green, reset=reset,
path=decrypted_file_path))
file_size = os.path.getsize(file_path) - len(decryption_key) - 1
max_size = 1024 * 1024 * 1024
# This is the maximum size (in bytes) per chunk. This can be higher based on your RAM.
# THE FILE MUST BE DECRYPTED WITH THE SAME CHUNK SIZE YOU ENCRYPTED IT WITH.
with open(decrypted_file_path, 'wb') as decrypted_file:
try:
rng = numpy.random.default_rng(seed=int.from_bytes(bytes((password + decryption_key).encode()), 'big'))
# This sets the random outcome to reverse the encryption.
if verbose:
print('{blue}[v] {grey}Decrypting file... ({cyan}{size} bytes{grey}){reset}'.format(blue=blue,
grey=grey,
cyan=cyan,
reset=reset,
size=file_size))
start = time.perf_counter() # This starts a timer to time the decryption.
for _ in range(int(file_size / max_size)):
decrypted_file.write(apply_pad(encrypted_file.read(max_size), generate_pad(rng, max_size)))
excess_size = file_size % max_size
decrypted_file.write(apply_pad(encrypted_file.read(excess_size), generate_pad(rng, excess_size)))
end = time.perf_counter()
if verbose:
seconds = end - start
speed = round(file_size / seconds)
message = '{blue}[v] {grey}Decrypted in {seconds} seconds. ({cyan}{speed:,} bytes per second{grey})'
print((message + reset).format(blue=blue, grey=grey, cyan=cyan, seconds=round(seconds, 2),
speed=speed))
except Exception as exception:
decrypted_file.close()
os.remove(decrypted_file_path) # This removes the partially decrypted file.
encrypted_file.close()
raise exception
if verbose:
print('{blue}[v] {grey}Closing files...{reset}'.format(blue=blue, grey=grey, reset=reset))
decrypted_file.close()
encrypted_file.close()
if verbose:
print('{blue}[v] {grey}Cleaning up...{reset}'.format(blue=blue, grey=grey, reset=reset))
os.remove(file_path) # This removes the encrypted file.
print('Decrypted as: {green}{name}{reset}'.format(green=green, reset=reset, name=decrypted_file_name))
if verbose:
print('{blue}[v] {green}Done!{reset}'.format(blue=blue, green=green, reset=reset))
print()
def destroy(file_path: typing.Union[str, None] = None, verbose: bool = False) -> None:
"""
This function destroys the decryption key in an encrypted file, making it impossible to decrypt. This does not
delete the file. The key could be recovered via brute force. May cause MemoryError for large files.
:param file_path: This is the path of the file to have the key of destroyed.
:type file_path: str or None
:param bool verbose: If this is set to True, this will print what the function is doing at each step. Otherwise,
those print statements are hidden. Defaults to False.
"""
if not file_path:
if verbose:
print('{blue}[v] {yellow}No file provided.{reset}'.format(blue=blue, yellow=yellow, reset=reset))
file_path = input(
'{bold}Enter the path of the file to destroy the key for: {reset}'.format(bold=bold, reset=reset))
elif verbose:
print('{blue}[v] {green}File provided.{reset}'.format(blue=blue, green=green, reset=reset))
while True:
if verbose:
print('{blue}[v] {grey}Checking file...{reset}'.format(blue=blue, grey=grey, reset=reset))
if os.path.isfile(file_path) and os.path.exists(file_path) and file_path.endswith('.MOTP'):
if verbose:
print('{blue}[v] {green}File is valid.{reset}'.format(blue=blue, green=green, reset=reset))
break
else:
print('{red}File is invalid.{reset}'.format(red=red, reset=reset))
file_path = input(
'{bold}Enter the path of the file to destroy the key for: {reset}'.format(bold=bold, reset=reset))
if verbose:
print('{blue}[v] {grey}Opening file...{reset}'.format(blue=blue, grey=grey, reset=reset))
with open(file_path, 'rb') as encrypted_file:
if verbose:
print('{blue}[v] {grey}Finding decryption key...{reset}'.format(blue=blue, grey=grey, reset=reset))
decryption_key = encrypted_file.readlines(1)[0].decode().rstrip()
try:
float(decryption_key)
except ValueError:
print('{red}[!] Decryption key is invalid or already destroyed.{reset}'.format(red=red, reset=reset))
return
decryption_key_length = len(decryption_key)
data = b'\n' + encrypted_file.read()
encrypted_file.close()
file_size = os.path.getsize(file_path) - decryption_key_length - 1
with open(file_path, 'wb') as encrypted_file:
try:
if verbose:
print(
'{blue}[v] {grey}Writing to file... ({cyan}{size} bytes{grey}){reset}'.format(blue=blue, grey=grey,
cyan=cyan,
reset=reset,
size=file_size))
start = time.perf_counter() # This starts a timer to time the writing.
encrypted_file.write(data)
end = time.perf_counter()
if verbose:
seconds = end - start
speed = round(file_size / seconds)
message = '{blue}[v] {grey}Wrote in {seconds} seconds. ({cyan}{speed:,} bytes per second{grey}){reset}'
print(message.format(blue=blue, grey=grey, cyan=cyan, reset=reset, seconds=round(seconds, 2),
speed=speed))
except Exception as exception:
encrypted_file.close()
raise exception
encrypted_file.close()
if verbose:
print('{blue}[v] {green}Done!{reset}'.format(blue=blue, green=green, reset=reset))
print()