-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenclib.py
257 lines (213 loc) · 9.44 KB
/
enclib.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
import datetime, re
from time import time
from os import path
from random import choice
from base64 import b85encode, b64encode as b64enc, b64decode as b64dec
from hashlib import sha512
from zlib import compress, decompress
from multiprocessing import Pool, cpu_count
from binascii import a2b_base64, b2a_base64
# enc 10.0.1 - CREATED BY RAPIDSLAYER101 (Scott Bree)
block_size = 1000000 # modifies the chunking size
b64set = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
b96set = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/¬`!\"£$%^&*()- =[{]};:'@#~\\|,<.>?"
conv_dict = {v: k for v, k in zip(range(96), b96set)}
conv_dict_back = {v: k for k, v in conv_dict.items()}
def hex_gens(num):
return "".join([choice(b96set) for x in range(int(num))])
def to_hex(base_fr, base_to, hex_to_convert):
decimal = 0
power = len(hex_to_convert)-1
for digit in hex_to_convert:
decimal += conv_dict_back[digit]*base_fr**power
power -= 1
hexadecimal = ""
while decimal > 0:
hexadecimal = conv_dict[decimal % base_to]+hexadecimal
decimal = decimal // base_to
return hexadecimal
def to_number(hex_to_convert):
return "".join([str(conv_dict_back[x]) for x in hex_to_convert])
def get_hex_base(hex_to_check): # this is only a guess
for i in range(96):
if to_hex(i+2, i+2, hex_to_check) == hex_to_check:
return i+2
def pass_to_seed(password, salt):
salt = sha512(sha512(b85encode(salt.encode())).hexdigest().encode()).hexdigest()
inp = f"{salt[:64]}{password}{salt[64:]}"
return to_hex(16, 96, sha512(sha512(b85encode(inp.encode())).hexdigest().encode()).hexdigest())
def seed_to_alpha(seed): # this function requires 129 numbers
alpha_gen = b64set
counter, alpha = [0, ""]
while len(alpha_gen) > 0:
counter += 2
value = int(str(seed)[counter:counter+2]) << 1
while value > len(alpha_gen)-1:
value = value // 2
if len(str(seed)[counter:]) < 2:
alpha += alpha_gen
alpha_gen = alpha_gen.replace(alpha_gen, "")
else:
chosen = alpha_gen[value]
alpha += chosen
alpha_gen = alpha_gen.replace(chosen, "")
return alpha
def seed_to_data(seed):
return seed_to_alpha(int(to_hex(96, 16, seed), 36)), to_hex(10, 96, str(int(to_hex(96, 16, seed), 36)))
def b64pad(master_key):
while len(master_key) % 4 != 0:
master_key += "="
return master_key
def shift_gen(amount, shift_num):
shift_value = ""
while len(shift_value) < amount:
shift_num1 = sha512(shift_num.encode()).digest()
shift_num = b64enc(shift_num1).decode()[:-2]
shift_value += shift_num+b64enc(shift_num1[::-1]).decode()[:-2]
return shift_value
def shifter(plaintext, shift_num, al, forwards):
alx3 = [x for x in al*3]
pln_txt = plaintext.replace("=", "")
if forwards:
return a2b_base64(b64pad("".join([alx3[al.index(x)+ord(y)] for x, y in zip(pln_txt, shift_num)])+"z"*8))
else:
return decompress(b64dec(b64pad("".join([alx3[al.index(x)-ord(y)] for x, y in zip(pln_txt, shift_num)]))))
def get_file_size(file):
file_size_kb = path.getsize(file)/1024
if file_size_kb > 9999:
file_size_mb = file_size_kb/1024
if file_size_mb > 2999:
return f"{round(file_size_mb/1024,2)}GB"
else:
return f"{round(file_size_mb,2)}MB"
else:
return f"{round(file_size_kb,2)}KB"
def encrypt_block(enc, data, alpha, block_seed, send_end=None):
shift_num = shift_gen(int(block_size*1.4), str(int(to_hex(96, 10, str(block_seed)), 36)))
if enc.lower() in ["e", "en", "enc", "encrypt"]:
if type(data) != bytes:
data = data.encode()
block = shifter(b64enc(compress(data, 9)).decode(), shift_num, alpha, True)
else:
block = shifter(data, shift_num, alpha, False)
try:
block = block.decode()
except UnicodeDecodeError:
pass
if send_end:
send_end.send(block)
else:
return block
def block_process(blk):
blk = b64pad(b2a_base64(blk).decode())
return blk[:-12]+blk[-12:].replace("zzzzzzzw", "z"*8).replace("\n", "")
def encrypt(enc, text, alpha, shift_seed, salt, join_dec=None):
if enc.lower() in ["e", "en", "enc", "encrypt", "d", "de", "dec", "decrypt"]:
if enc.lower() in ["e", "en", "enc", "encrypt"]:
e_chunks = [text[i:i+block_size] for i in range(0, len(text), block_size)]
else:
if type(text) == list:
e_chunks = [block_process(block) for block in text]
else:
text = block_process(text)
if type(text) == bytes:
e_chunks = text.split(b" ")
if type(text) == str:
e_chunks = text.split(" ")
if len(e_chunks) == 1:
shift_seed = str(int(to_hex(96, 10, str(shift_seed)), 36))
if enc.lower() in ["e", "en", "enc", "encrypt"]:
if type(text) != bytes:
text = text.encode()
plaintext = b64enc(compress(text, 9)).decode()
return shifter(plaintext, shift_gen(len(plaintext), shift_seed), alpha, True)
else:
output_end = shifter(text, shift_gen(len(text), shift_seed), alpha, False)
try:
return output_end.decode()
except UnicodeDecodeError:
return output_end
else:
print(f"Launching {len(e_chunks)} threads")
block_seeds = []
for i in range(len(e_chunks)+1):
shift_seed = pass_to_seed(shift_seed, salt)
block_seeds.append(shift_seed)
pool = Pool(cpu_count())
result_objects = [pool.apply_async(encrypt_block, args=(enc, e_chunks[x], alpha, block_seeds[x]))
for x in range(0, len(e_chunks))]
pool.close()
if join_dec:
d_data = b""
for x in result_objects:
new_data = x.get()
try:
d_data += new_data
except TypeError:
d_data = ""
d_data += new_data
else:
d_data = [x.get() for x in result_objects]
pool.join()
return d_data
else:
print("ENCRYPTION CANCELLED! Enc type not in 'e', 'en', 'enc', 'encrypt', 'd', 'de', 'dec', 'decrypt'")
def encrypt_key(text, key, salt):
alpha, shift_num = seed_to_data(pass_to_seed(key, salt))
return encrypt("enc", text, alpha, shift_num, salt)
def encrypt_file(enc, file, key, salt, file_output):
start = time()
if enc.lower() in ["e", "en", "enc", "encrypt", "d", "de", "dec", "decrypt"]:
if path.exists(file):
file_name = file.split("/")[-1].split(".")[:-1] # file_type = file.split("/")[-1].split(".")[-1:]
print(f"{file_name} is {get_file_size(file)}, should take {round(path.getsize(file)/32345903.7288, 2)}s")
alpha, shift_num = seed_to_data(pass_to_seed(key, salt))
if enc.lower() in ["e", "en", "enc", "encrypt"]:
with open(file, 'rb') as hash_file:
result_list = encrypt(enc, hash_file.read(), alpha, shift_num, salt)
with open(file_output, "wb") as f:
for e_block in result_list:
f.write(b" ")
f.write(e_block)
print(f"ENCRYPTION COMPLETE OF {get_file_size(file)} IN {round(time()-start, 2)}s")
else:
with open(file, "rb") as hash_file:
d_data = encrypt(enc, hash_file.read().split(b" ")[1:], alpha, shift_num, salt)
if type(d_data[0]) == bytes:
with open(f"{file_output}", "wb") as f:
for block in d_data:
f.write(block)
if type(d_data[0]) == str:
with open(f"{file_output}", "w", encoding="utf-8") as f:
for block in d_data:
f.write(block.replace("\r", ""))
print(f"DECRYPTION COMPLETE OF {get_file_size(file)} IN {round(time()-start, 2)}s")
else:
return "File not found"
else:
print("ENCRYPTION CANCELLED! Enc type not in 'e', 'en', 'enc', 'encrypt', 'd', 'de', 'dec', 'decrypt'")
def decrypt_key(e_text, key, salt):
alpha1, shift_num = seed_to_data(pass_to_seed(key, salt))
return encrypt("dec", e_text, alpha1, shift_num, salt, "join_dec")
def search(data, filter_fr, filter_to):
m = re.search(f"""{filter_fr}(.+?){filter_to}""", str(data))
if m:
return m.group(1)
else:
return None
def get_links(data):
return ('\n'.join((re.findall(r'(https?://[^\s]+)', str(data))))) + \
('\n'.join((re.findall(r'(http?://[^\s]+)', str(data)))))
def round_tme(dt=None, round_to=30):
if not dt:
dt = datetime.datetime.now()
seconds = (dt.replace(tzinfo=None)-dt.min).seconds
return dt+datetime.timedelta(0, (seconds+round_to/2)//round_to*round_to-seconds, -dt.microsecond)
def hash_a_file(file):
hash_ = sha512()
with open(file, 'rb') as hash_file:
buf = hash_file.read(262144)
while len(buf) > 0:
hash_.update(buf)
buf = hash_file.read(262144)
return to_hex(16, 96, hash_.hexdigest())