-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
115 lines (90 loc) · 3.85 KB
/
client.py
File metadata and controls
115 lines (90 loc) · 3.85 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
114
115
#client to send message
import socket
def encrypt(text,c):
result = ""
# traverse text
for i in range(len(text)):
char = text[i]
if (char!=' '):
# Encrypt uppercase characters
if (char.isupper()):
result += chr((ord(char) + c-65) % 26 + 65)
# Encrypt lowercase characters
else:
result += chr((ord(char) + c - 97) % 26 + 97)
elif(char == ' '):
result += chr(ord(' '))
return result
def decrypt(text,d):
c = 26 - d
result = encrypt(text,c)
return result
def extract_keys(key_data):
Key_list = []
d1 = key_data.replace("["," ")
d2 = d1.replace("]"," ")
d3 = d2.replace(",","")
i = 0
while(i<=len(d3)-1):
if i != len(d3)-1:
if d3[i] == '1' and d3[i+1] == ' ':
Key_list.append(int(d3[i]))
elif d3[i] == '1' and d3[i+1] != ' ':
Key_list.append(int(d3[i]+d3[i+1]))
i+=2
if d3[i] == '2' and d3[i+1] == ' ':
Key_list.append(int(d3[i]))
elif d3[i] == '2' and d3[i+1] != ' ':
Key_list.append(int(d3[i]+d3[i+1]))
i+=2
elif d3[i] == '3' and d3[i+1] == ' ':
Key_list.append(int(d3[i]))
elif d3[i] == '4' and d3[i+1] == ' ':
Key_list.append(int(d3[i]))
elif d3[i] == '5' and d3[i+1] == ' ':
Key_list.append(int(d3[i]))
elif d3[i] == '6' and d3[i+1] == ' ':
Key_list.append(int(d3[i]))
elif d3[i] == '7' and d3[i+1] == ' ':
Key_list.append(int(d3[i]))
elif d3[i] == '8' and d3[i+1] == ' ':
Key_list.append(int(d3[i]))
elif d3[i] == '9' and d3[i+1] == ' ':
Key_list.append(int(d3[i]))
i+=1
return Key_list
nk = 0 #new key inde
n = 1 # control flow
key = 4 # initial key
print()
# Enter the Ip address and the port of the server machine to connect to
SERVER = input("Enter the IP address of the server --> ")
PORT = int(input("Enter the port of the server --> "))
while(True):
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect((SERVER, PORT))
#Only run once while initializing the connection
if n == 1:
inp = "GET KEYS"
msg = encrypt(inp,key)
client.sendall(bytes(msg,'UTF-8'))
data = client.recv(2048)
key_data = data.decode()
#Extracting the keys list of Random Keys generated by server
key_list = extract_keys(key_data)
print()
print("Recived Keys",key_list)
print()
print("SECURE CONNECTION HAS BEEN SUCCESSFULLY ESTABLISHED SERVER")
print()
n = 0 #set control flow variable to 0 so that comminication can be started
#Now after the keys have been known the connection is now secure and then start communication
if n == 0:
inp = input("enter the message to send --> ")
key = key_list[nk] #Take one key from the key bunch
msg = encrypt(inp,key) #Encrypt the data with that key
nk += 1 # Increment the key index
client.sendall(bytes(msg,'UTF-8')) # send the data to the server
print("Key value = ",key)
print()
client.close()