Skip to content

Commit c291f65

Browse files
Encryption
1 parent 6e3729c commit c291f65

1 file changed

Lines changed: 22 additions & 14 deletions

File tree

one.py

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import hashlib
66
from hurry.filesize import size
77
import pyfiglet
8+
from cryptography.fernet import Fernet
89
def warn_md5():
910
print(f"Warning! MD5s do not match! {sent_hash}/{new_hash}")
1011
print("This means that either the transfer went wrong, or the file ")
@@ -31,13 +32,18 @@ def warn_md5():
3132
print(pyfiglet.figlet_format("A Programmer"))
3233
sys.exit()
3334
if sys.argv[1] == 'transmit':
35+
default_key = '5LabvIZ3MFAxk0IgJnTjwyHbWXVZdoPQcEzLeLL9IHE='.encode()
36+
f = Fernet(default_key)
37+
3438
SEP = "<SEP>"
3539
BUFFER_SIZE = 4096
3640
port = 9001
3741
host = sys.argv[2]
3842
filename = sys.argv[3]
43+
if host == 'magic@narnia' and filename == 'unicorns':
44+
print("ERROR: Cannot transfer unicorns: missing dependency: Magic")
45+
sys.exit()
3946
filesize = os.path.getsize(filename)
40-
4147
s = socket.socket()
4248

4349

@@ -49,26 +55,29 @@ def warn_md5():
4955
dat = f'{filename}{SEP}{filesize}{SEP}{Hash}'
5056
# while len(dat.encode()) < 4096:
5157
# dat = dat+'0'
52-
data = dat
53-
print(data)
54-
print(len(data.encode()))
58+
data = f.encrypt(dat.encode())
5559
s.send(data.encode())
5660

5761

5862
progress = tqdm.tqdm(range(filesize), f'Sending {filename}', unit='B', unit_scale=True, unit_divisor=1024)
59-
with open(filename, "rb") as f:
63+
with open(filename, "rb") as fi:
6064
for _ in progress:
61-
bytes_read = f.read(BUFFER_SIZE)
65+
bytes_read = fi.read(BUFFER_SIZE)
66+
byte_size = len(bytes_read)
67+
bytes_read = f.encrypt(bytes_read)
6268
if not bytes_read:
6369
break
6470
s.sendall(bytes_read)
65-
progress.update(len(bytes_read))
71+
progress.update(byte_size)
6672
s.close()
6773

6874

6975

7076
elif sys.argv[1] == 'receive':
7177
global sent_hash
78+
default_key = '5LabvIZ3MFAxk0IgJnTjwyHbWXVZdoPQcEzLeLL9IHE='.encode()
79+
f = Fernet(default_key)
80+
print(f.encrypt("hello WORLD".encode()))
7281
SERVER_HOST = '0.0.0.0'
7382
SERVER_PORT = 9001
7483
BUFFER_SIZE = 4096
@@ -83,27 +92,25 @@ def warn_md5():
8392
print(f"([*] Connection established!")
8493

8594

86-
received = client_socket.recv(BUFFER_SIZE).decode()
95+
received = f.decrypt(client_socket.recv(BUFFER_SIZE)).decode()
8796
print(f"Received {len(received.encode())} bytes")
8897
raw = received.split(SEP)
8998
print(raw)
9099
while '' in raw:
91100
raw.remove('')
92101
print(len(raw))
93-
filename = raw[0]
102+
filename = [0]
94103
filesize = raw[1]
95104
sent_hash = raw[2]
105+
f = Fernet(raw[3])
96106
filename = filename.replace('0','')
97107

98108
filename = os.path.basename(filename)
99109
saveas = filename
100110
filesize = int(filesize)
101111
if filesize >= 524288000:
102112
v = input(f'{filename} seems to be a large file. ({size(filesize)}) Download? [Y/n]')
103-
if v.lower() == 'y':
104-
x = 1
105-
del x
106-
elif v.lower() == 'n':
113+
if v.lower() == 'n':
107114
print("User abort!")
108115
sys.exit()
109116

@@ -113,14 +120,15 @@ def warn_md5():
113120
print(saveas)
114121
with open(saveas, "wb") as f:
115122
for _ in progress:
116-
bytes_read = client_socket.recv(BUFFER_SIZE)
123+
bytes_read = f.decrypt(client_socket.recv(BUFFER_SIZE))
117124
if not bytes_read:
118125
break
119126
f.write(bytes_read)
120127
progress.update(len(bytes_read))
121128
client_socket.close()
122129
s.close()
123130
progress.close()
131+
del f
124132
new_hash = hashlib.md5(open(filename,'rb').read()).hexdigest()
125133
if not new_hash == sent_hash:
126134
warn_md5()

0 commit comments

Comments
 (0)