-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathClient_2.py
More file actions
137 lines (106 loc) · 3.03 KB
/
Client_2.py
File metadata and controls
137 lines (106 loc) · 3.03 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
""" Script for Tkinter GUI chat client. """
import tkinter
from socket import AF_INET, socket, SOCK_STREAM
from threading import Thread
def generateKeys():
e = d = N = 0
p = 5099
q = 4759
N = p * q
phiN = (p - 1) * (q - 1)
while True:
e = 1013
if (isCoPrime(e, phiN)):
break
d = modularInv(e, phiN)
return e, d, N
def isCoPrime(p, q):
return gcd(p, q) == 1
def gcd(p, q):
while q:
p, q = q, p % q
return p
def egcd(a, b):
s = 0; old_s = 1
t = 1; old_t = 0
r = b; old_r = a
while r != 0:
quotient = old_r // r
old_r, r = r, old_r - quotient * r
old_s, s = s, old_s - quotient * s
old_t, t = t, old_t - quotient * t
# return gcd, x, y
return old_r, old_s, old_t
def modularInv(a, b):
gcd, x, y = egcd(a, b)
if x < 0:
x += b
return x
def decrypt(d, N, cipher):
msg = ""
parts = cipher.split()
for part in parts:
if part:
c = int(part)
msg += chr(pow(c, d, N))
return msg
def encrypt(e, N, msg):
cipher = ""
for c in msg:
m = ord(c)
cipher += str(pow(m, e, N)) + " "
return cipher
e, d, N = generateKeys()
def receive():
""" Handles receiving of messages. """
while True:
try:
msg = (sock.recv(1024)).decode()
dec = decrypt(d, N, msg)
msg_list.insert(tkinter.END, dec)
except OSError: # Possibly client has left the chat.
break
def send(event=None):
""" Handles sending of messages. """
msg = my_msg.get()
enc = encrypt(e, N, msg)
print("Encrypted message", enc)
my_msg.set("") # Clears input field.
sock.send(enc.encode())
if msg == "#quit":
sock.close()
top.quit()
def on_closing(event=None):
""" This function is to be called when the window is closed. """
my_msg.set("#quit")
send()
top = tkinter.Tk()
top.title("Simple Cypher Client")
messages_frame = tkinter.Frame(top)
my_msg = tkinter.StringVar() # For the messages to be sent.
my_msg.set("")
scrollbar = tkinter.Scrollbar(messages_frame) # To navigate through past messages.
msg_list = tkinter.Listbox(messages_frame, height=15, width=70, yscrollcommand=scrollbar.set)
scrollbar.pack(side=tkinter.RIGHT, fill=tkinter.Y)
msg_list.pack(side=tkinter.LEFT, fill=tkinter.BOTH)
msg_list.pack()
messages_frame.pack()
button_label = tkinter.Label(top, text="Enter Message:")
button_label.pack()
entry_field = tkinter.Entry(top, textvariable=my_msg, foreground="Red")
entry_field.bind("<Return>", send)
entry_field.pack()
send_button = tkinter.Button(top, text="Send", command=send)
send_button.pack()
quit_button = tkinter.Button(top, text="Quit", command=on_closing)
quit_button.pack()
top.protocol("WM_DELETE_WINDOW", on_closing)
HOST = "127.0.0.1"
PORT = 5000
BUFSIZ = 1024
ADDR = (HOST, PORT)
sock = socket(AF_INET, SOCK_STREAM)
sock.connect(ADDR)
receive_thread = Thread(target=receive)
receive_thread.start()
tkinter.mainloop() # Starts GUI execution.