-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_handler.py
More file actions
113 lines (91 loc) · 2.72 KB
/
client_handler.py
File metadata and controls
113 lines (91 loc) · 2.72 KB
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
import socket
import rsa
import base64
from Crypto.PublicKey.RSA import importKey
import Crypto
from Crypto.PublicKey import RSA
from Crypto.Signature import PKCS1_v1_5
from Crypto.Hash import SHA256
import hashlib
from encryption import encrypt, decrypt, simple_encrypt, simple_decrypt
from login_gui import Login
from dir_handler import receive_files_and_write
from binascii import hexlify, unhexlify
import time
import random
HOST = '127.0.0.1'
PORT = 8787
f = open("server_pub_key", "r")
SERVER_PUB_KEY = importKey(f.read())
f.close()
f = open("priv_key")
PRV_KEY = importKey(f.read())
f.close()
DH_KEY = ""
def handshaking_protocol(s, user_name, password,count):
global DH_KEY
credentials = '{},{}'.format(user_name, password)
credentials = bytes(credentials, "utf-8")
cipher = SERVER_PUB_KEY.encrypt(credentials, 64)[0]
base64txt = base64.b64encode(cipher)
s.send(base64txt)
received = base64.b64decode(s.recv(4128)).decode().split(",")
print(received)
message = received[0]
signature = (int(received[1][1:]),)
h = SHA256.new(message.encode()).digest()
verification = SERVER_PUB_KEY.verify(h, signature)
if verification == False:
s.close()
sign = PRV_KEY.sign(h, "")
#DH parameters
p = PRV_KEY.decrypt(int(received[3]))
g = PRV_KEY.decrypt(int(received[4]))
X = PRV_KEY.decrypt(int(received[5]))
p = int(p)
g = int(g)
X = int(X)
b = random.randint(0,p)
Y = (g**b) % p
print(p)
print(g)
print(X)
print(Y)
DH_KEY = SHA256.new(str( (X**b) % p).encode()).digest()
print(base64.b64encode(DH_KEY))
message = "{},{},{}".format(message, sign, Y)
base64txt = base64.b64encode(message.encode())
s.send(base64txt)
response = s.recv(1024).decode()
if 'Unauthoritized: Wrong password or user name' in response and count < 2:
Login("hatalı kullanıcı adı ya da şifre")
login_procedure(count+1)
response = s.recv(1024).decode()
exit()
print(response)
message = "getall"
cipher = encrypt(DH_KEY, message)
s.send(cipher)
response = receive_files_and_write(s,user_name, DH_KEY)
if response == 1:
login_procedure(0)
else:
exit()
def login_procedure(count):
try:
f = open("user_data.txt", "r")
except:
Login("")
f = open("user_data.txt", "r")
key = f.readline()[:-1]
user_named = f.readline()[:-1]
password = f.readline()
password = simple_decrypt(key, password,0)
s = socket.socket()
s.connect((HOST, PORT))
accepted = s.recv(1024).decode()
if accepted == "Connection accepted\n":
handshaking_protocol(s, user_named, password,count)
else:
s.close()
login_procedure(0)