Skip to content
47 changes: 18 additions & 29 deletions Client_1.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,22 @@
""" Script for Tkinter GUI chat client. """

import tkinter
from socket import AF_INET, socket, SOCK_STREAM
from threading import Thread

def generateKeys():
#finds N and phi of N
def RSA():
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)
e = 1013
#find d using e and phi
d = inverse(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
Expand All @@ -42,13 +30,15 @@ def egcd(a, b):
# return gcd, x, y
return old_r, old_s, old_t

def modularInv(a, b):
#d is the inverse of e and phi
def inverse(a, b):
gcd, x, y = egcd(a, b)

if x < 0:
x += b
return x

#decrypts each character
def decrypt(d, N, cipher):
msg = ""
parts = cipher.split()
Expand All @@ -58,35 +48,34 @@ def decrypt(d, N, cipher):
msg += chr(pow(c, d, N))
return msg

#encrypts each character
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()
e, d, N = RSA()


def receive():
""" Handles receiving of messages. """
while True:
try:
msg = (sock.recv(1024)).decode()
dec = decrypt(d, N, msg)
print(dec)
msg_list.insert(tkinter.END, dec)
msg = sock.recv(BUFSIZ).decode("utf8")
msg_list.insert(tkinter.END, msg)
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(enc)

print("Encrypted message", enc)
dec = decrypt(d, N, enc)
my_msg.set("") # Clears input field.
sock.send(enc.encode())
sock.send(dec.encode())
if msg == "#quit":
sock.close()
top.quit()
Expand Down
47 changes: 17 additions & 30 deletions Client_2.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,22 @@
""" Script for Tkinter GUI chat client. """

import tkinter
from socket import AF_INET, socket, SOCK_STREAM
from threading import Thread

def generateKeys():
#finds N and phi of N
def RSA():
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)
e = 1013
#find d using e and phi
d = inverse(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
Expand All @@ -42,13 +30,15 @@ def egcd(a, b):
# return gcd, x, y
return old_r, old_s, old_t

def modularInv(a, b):
#d is the inverse of e and phi
def inverse(a, b):
gcd, x, y = egcd(a, b)

if x < 0:
x += b
return x

#decrypts each character
def decrypt(d, N, cipher):
msg = ""
parts = cipher.split()
Expand All @@ -58,37 +48,34 @@ def decrypt(d, N, cipher):
msg += chr(pow(c, d, N))
return msg

#encrypts each character
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()
e, d, N = RSA()


def receive():
""" Handles receiving of messages. """
while True:
try:
#msg = sock.recv(BUFSIZ).decode("utf8")
msg = (sock.recv(1024)).decode()
dec = decrypt(d, N, msg)
print(dec)
msg_list.insert(tkinter.END, dec)
msg = sock.recv(BUFSIZ).decode("utf8")
msg_list.insert(tkinter.END, msg)
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(enc)

print("Encrypted message", enc)
dec = decrypt(d, N, enc)
my_msg.set("") # Clears input field.
sock.send(enc.encode())
sock.send(dec.encode())
if msg == "#quit":
sock.close()
top.quit()
Expand Down
61 changes: 59 additions & 2 deletions Client_3.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,60 @@
from socket import AF_INET, socket, SOCK_STREAM
from threading import Thread

#finds N and phi of N
def RSA():
e = d = N = 0
p = 5099
q = 4759
N = p * q
phiN = (p - 1) * (q - 1)
e = 1013
#find d using e and phi
d = inverse(e, phiN)

return e, d, N

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

#d is the inverse of e and phi
def inverse(a, b):
gcd, x, y = egcd(a, b)

if x < 0:
x += b
return x

#decrypts each character
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

#encrypts each character
def encrypt(e, N, msg):
cipher = ""
for c in msg:
m = ord(c)
cipher += str(pow(m, e, N)) + " "
return cipher

e, d, N = RSA()


def receive():
""" Handles receiving of messages. """
Expand All @@ -17,8 +71,11 @@ def receive():
def send(event=None):
""" Handles sending of messages. """
msg = my_msg.get()
enc = encrypt(e, N, msg)
print("Encrypted message", enc)
dec = decrypt(d, N, enc)
my_msg.set("") # Clears input field.
sock.send(bytes(msg, "utf8"))
sock.send(dec.encode())
if msg == "#quit":
sock.close()
top.quit()
Expand Down Expand Up @@ -67,4 +124,4 @@ def on_closing(event=None):

receive_thread = Thread(target=receive)
receive_thread.start()
tkinter.mainloop() # Starts GUI execution.
tkinter.mainloop() # Starts GUI execution.
61 changes: 59 additions & 2 deletions Client_4.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,60 @@
from socket import AF_INET, socket, SOCK_STREAM
from threading import Thread

#finds N and phi of N
def RSA():
e = d = N = 0
p = 5099
q = 4759
N = p * q
phiN = (p - 1) * (q - 1)
e = 1013
#find d using e and phi
d = inverse(e, phiN)

return e, d, N

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

#d is the inverse of e and phi
def inverse(a, b):
gcd, x, y = egcd(a, b)

if x < 0:
x += b
return x

#decrypts each character
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

#encrypts each character
def encrypt(e, N, msg):
cipher = ""
for c in msg:
m = ord(c)
cipher += str(pow(m, e, N)) + " "
return cipher

e, d, N = RSA()


def receive():
""" Handles receiving of messages. """
Expand All @@ -17,8 +71,11 @@ def receive():
def send(event=None):
""" Handles sending of messages. """
msg = my_msg.get()
enc = encrypt(e, N, msg)
print("Encrypted message", enc)
dec = decrypt(d, N, enc)
my_msg.set("") # Clears input field.
sock.send(bytes(msg, "utf8"))
sock.send(dec.encode())
if msg == "#quit":
sock.close()
top.quit()
Expand Down Expand Up @@ -67,4 +124,4 @@ def on_closing(event=None):

receive_thread = Thread(target=receive)
receive_thread.start()
tkinter.mainloop() # Starts GUI execution.
tkinter.mainloop() # Starts GUI execution.
Loading