-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChaes.py
358 lines (287 loc) · 15 KB
/
Chaes.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
import json
import base64
from Crypto.Cipher import ChaCha20_Poly1305
from Crypto.Random import get_random_bytes
import gcm
import beaupy
from beaupy.spinners import *
from pystyle import Colors, Colorate
import binascii
import os
def banner():
banner = """
▄▀▄▄▄▄ ▄▀▀▄ ▄▄ ▄▀▀█▄ ▄▀▀█▄▄▄▄ ▄▀▀▀▀▄
█ █ ▌ █ █ ▄▀ ▐ ▄▀ ▀▄ ▐ ▄▀ ▐ █ █ ▐
▐ █ ▐ █▄▄▄█ █▄▄▄█ █▄▄▄▄▄ ▀▄
█ █ █ ▄▀ █ █ ▌ ▀▄ █
▄▀▄▄▄▄▀ ▄▀ ▄▀ █ ▄▀ ▄▀▄▄▄▄ █▀▀▀
█ ▐ █ █ ▐ ▐ █ ▐ ▐
▐ ▐ ▐ ▐
Made by Ori#6338 | @therealOri_ | https://github.com/therealOri
"""
colored_banner = Colorate.Horizontal(Colors.purple_to_blue, banner, 1)
return colored_banner
chacha_header = b"ChaCha real smooth~ dada da dada da"
def encrypt(plaintext, eKey, esalt):
#AES
data_enc = gcm.stringE(enc_data=plaintext, key=eKey)
data_enc = bytes(data_enc, 'utf-8')
#ChaCha
cipher = ChaCha20_Poly1305.new(key=esalt)
cipher.update(chacha_header)
ciphertext, tag = cipher.encrypt_and_digest(data_enc)
jk = [ 'nonce', 'header', 'ciphertext', 'tag' ]
jv = [ base64.b64encode(x).decode('utf-8') for x in (cipher.nonce, chacha_header, ciphertext, tag) ]
result = json.dumps(dict(zip(jk, jv)))
result_bytes = bytes(result, 'utf-8')
b64_result = base64.b64encode(result_bytes)
final_result = base64_to_hex(b64_result)
return final_result
# I couldn't be asked to make my life harder by editing the above function just to handle if a user has an encryption key to use already.
# So I just copy and pasted the same function and allowd it to take an extra parameter. You are more than welcome to contribue/help.
def encrypt_v2(plaintext, aesKey, chaKey):
#AES
data_enc = gcm.stringE(enc_data=plaintext, key=aesKey)
data_enc = bytes(data_enc, 'utf-8')
#ChaCha
cipher = ChaCha20_Poly1305.new(key=chaKey)
cipher.update(chacha_header)
ciphertext, tag = cipher.encrypt_and_digest(data_enc)
jk = [ 'nonce', 'header', 'ciphertext', 'tag' ]
jv = [ base64.b64encode(x).decode('utf-8') for x in (cipher.nonce, chacha_header, ciphertext, tag) ]
result = json.dumps(dict(zip(jk, jv)))
result_bytes = bytes(result, 'utf-8')
b64_result = base64.b64encode(result_bytes)
final_result = base64_to_hex(b64_result)
return final_result
def decrypt(dKey, json_input, dsalt):
try:
b64 = json.loads(json_input)
jk = [ 'nonce', 'header', 'ciphertext', 'tag' ]
jv = {k:base64.b64decode(b64[k]) for k in jk}
cipher = ChaCha20_Poly1305.new(key=dsalt, nonce=jv['nonce'])
cipher.update(jv['header'])
plaintext = cipher.decrypt_and_verify(jv['ciphertext'], jv['tag'])
except (ValueError, KeyError):
print("Incorrect decryption")
return None
#aes decrypt
decrypted_message = gcm.stringD(dcr_data=plaintext, key=dKey)
return decrypted_message
# Convert base64 string to hex
def base64_to_hex(base64_string):
decoded_bytes = base64.b64decode(base64_string)
hex_string = binascii.hexlify(decoded_bytes)
return hex_string.decode()
# Convert hex string to base64
def hex_to_base64(hex_string):
hex_bytes = bytes.fromhex(hex_string)
base64_string = base64.b64encode(hex_bytes)
return base64_string.decode()
if __name__ == '__main__':
gcm.clear()
while True:
main_options = ["Encrypt?", "Decrypt?", "Exit?"]
print(f'{banner()}\n\nWhat would you like to do?\n-----------------------------------------------------------\n')
main_option = beaupy.select(main_options, cursor_style="#ffa533")
if not main_option:
gcm.clear()
exit("Keyboard Interuption Detected!\nGoodbye <3")
if main_options[0] in main_option:
gcm.clear()
while True:
enc_options = ["Encrypt message?", "Encrypt file?", "Back?"]
print(f'{banner()}\n\nDo you want to encrypt a message or a file?\n-----------------------------------------------------------\n')
enc_option = beaupy.select(enc_options, cursor_style="#ffa533")
if not enc_option:
gcm.clear()
break
if enc_options[0] in enc_option:
gcm.clear()
message = beaupy.prompt("Message to encrypt").encode()
if beaupy.confirm("Do you have an encryption key to use already?"):
eKey = beaupy.prompt("Encryption Key")
key_and_salt = eKey.split(":")
salt_1 = key_and_salt[1]
key_0 = key_and_salt[0]
salt = base64.b64decode(salt_1)
key = base64.b64decode(key_0)
chaCrypt = encrypt_v2(message, key, salt)
gcm.clear()
input(f'Here is your encrypted message: {chaCrypt}\n\nPress "enter" to contine...')
gcm.clear()
else:
salt = get_random_bytes(32)
key_data = beaupy.prompt("Data for key gen").encode()
gcm.clear()
eKey = gcm.keygen(key_data) #Returns bytes and will return "None" if what's provided is less than 100 characters.
#Go back to main menu and continue
if not eKey:
continue
save_me = base64.b64encode(eKey) #for saving eKey to decrypt later.
bSalt = base64.b64encode(salt)
master_key = f"{save_me.decode()}:{bSalt.decode()}"
input(f'Save this key so you can decrypt later: {master_key}\n\nPress "enter" to contine...')
gcm.clear()
chaCrypt = encrypt(message, eKey, salt)
gcm.clear()
input(f'Here is your encrypted message: {chaCrypt}\n\nPress "enter" to contine...')
gcm.clear()
if enc_options[1] in enc_option:
gcm.clear()
file_path = beaupy.prompt("File to encrypt.").replace('\\', ' ').strip()
hex_format = "0123456789abcdefABCDEF"
BUFFER_SIZE = 65536 # 64KB buffer size
try:
with open(file_path, 'r') as rd:
while True:
chunk = rd.read(BUFFER_SIZE)
if not chunk:
break
data_check = chunk
except:
with open(file_path, encoding='latin-1') as rd:
while True:
chunk = rd.read(BUFFER_SIZE)
if not chunk:
break
data_check = chunk
if all(c in hex_format for c in data_check if c.isalnum()):
gcm.clear()
input('The file you have provided is already encrypted.\n\nPress "enter" to continue...')
gcm.clear()
continue
if file_path.endswith('.locked'):
gcm.clear()
input('The file you have provided already has the ".locked" extension.\n\nPress "enter" to continue...')
gcm.clear()
continue
else:
with open(file_path, 'rb') as rf:
file_data = rf.read()
if beaupy.confirm("Do you have an encryption key to use already?"):
eKey = beaupy.prompt("Encryption Key")
if not eKey:
continue
key_and_salt = eKey.split(":")
salt_1 = key_and_salt[1]
key_0 = key_and_salt[0]
salt = base64.b64decode(salt_1)
key = base64.b64decode(key_0)
spinner = Spinner(ARC, "Encrypting data... (this may take awhile)")
spinner.start()
chaCrypt = encrypt_v2(file_data, key, salt)
with open(file_path, 'w', buffering=4096*4096) as fw:
fw.write(chaCrypt)
os.rename(file_path, file_path.replace(file_path, f'{file_path}.locked'))
spinner.stop()
input(f'File has been successfully encrypted!\n\nPress "enter" to continue...')
gcm.clear()
continue
else:
salt = get_random_bytes(32)
key_data = beaupy.prompt("Data for key gen").encode()
gcm.clear()
eKey = gcm.keygen(key_data)
if not eKey:
continue
save_me = base64.b64encode(eKey)
bSalt = base64.b64encode(salt)
master_key = f"{save_me.decode()}:{bSalt.decode()}"
input(f'Save this key so you can decrypt later: {master_key}\n\nPress "enter" to contine...')
gcm.clear()
spinner = Spinner(ARC, "Encrypting data... (this may take awhile)")
spinner.start()
chaCrypt = encrypt(file_data, eKey, salt)
with open(file_path, 'w', buffering=4096*4096) as fw:
fw.write(chaCrypt)
os.rename(file_path, file_path.replace(file_path, f'{file_path}.locked'))
spinner.stop()
input(f'File has been successfully encrypted!\n\nPress "enter" to continue...')
gcm.clear()
continue
if enc_options[2] in enc_option:
gcm.clear()
break
if main_options[1] in main_option:
gcm.clear()
while True:
dcr_options = ["Decrypt message?", "Decrypt file?", "Back?"]
print(f'{banner()}\n\nDo you want to decrypt a message or a file?\n-----------------------------------------------------------\n')
dcr_option = beaupy.select(dcr_options, cursor_style="#ffa533")
if not dcr_option:
gcm.clear()
break
if dcr_options[0] in dcr_option:
#Get key and message
dKey = beaupy.prompt("Encryption Key")
dMessage = beaupy.prompt("Encrypted Message")
enc_message = hex_to_base64(dMessage)
#Decode message and get salt and key after splitting on ":" to make a list.
json_input = base64.b64decode(enc_message)
key_and_salt = dKey.split(":")
salt = key_and_salt[1]
key = key_and_salt[0]
salt = base64.b64decode(salt)
key = base64.b64decode(key)
#Decrypt data.
cha_aes_crypt = decrypt(key, json_input, salt)
gcm.clear()
input(f'Here is your decrypted message: {cha_aes_crypt.decode()}\n\nPress "enter" to contine...')
gcm.clear()
continue
if dcr_options[1] in dcr_option:
file_path = beaupy.prompt("File to decrypt").replace('\\', ' ').strip()
hex_format = "0123456789abcdefABCDEF"
BUFFER_SIZE = 65536 # 64KB buffer size
try:
with open(file_path, 'r') as rd:
while True:
chunk = rd.read(BUFFER_SIZE)
if not chunk:
break
data_check = chunk
except:
with open(file_path, encoding='latin-1') as rd:
while True:
chunk = rd.read(BUFFER_SIZE)
if not chunk:
break
data_check = chunk
if not all(c in hex_format for c in data_check if c.isalnum()):
gcm.clear()
input('The file you have provided does not match encrypted format - (hexadecimal).\n\nPress "enter" to continue...')
gcm.clear()
continue
if file_path.endswith(".locked"):
dKey = beaupy.prompt("Encryption Key")
with open(file_path, 'r') as fr:
file_data = fr.read()
enc_message = hex_to_base64(file_data)
json_input = base64.b64decode(enc_message)
key_and_salt = dKey.split(":")
salt = key_and_salt[1]
key = key_and_salt[0]
salt = base64.b64decode(salt)
key = base64.b64decode(key)
spinner = Spinner(ARC, "Decrypting data... (this may take awhile)")
spinner.start()
cha_aes_crypt = decrypt(key, json_input, salt)
with open(file_path, 'wb', buffering=4096*4096) as fw:
fw.write(cha_aes_crypt)
os.rename(file_path, file_path.replace('.locked', ''))
spinner.stop()
input(f'File has been successfully decrypted!\n\nPress "enter" to continue...')
gcm.clear()
continue
else:
gcm.clear()
input('The file you have provided does not have the ".locked" extension.\n\nPress "enter" to continue...')
gcm.clear()
continue
if dcr_options[2] in dcr_option:
gcm.clear()
break
if main_options[2] in main_option:
gcm.clear()
exit("Goodbye! <3")