-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
82 lines (73 loc) · 2.46 KB
/
server.py
File metadata and controls
82 lines (73 loc) · 2.46 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
import socket
import time
class RSA:
def __init__(self):
self.p = 239
self.q = 263
self.n = self.p * self.q
self.totient = (self.p-1)*(self.q-1)
self.privateKey = ""
self.publicKey = 0
def setPublicKey(self,publicKey):
self.publicKey = publicKey
def getPublicKey(self):
totient = self.totient
i = 2
while i < totient and i > 1:
if self.gcd(i,totient) == 1:
self.setPublicKey(i)
publicKey = i
break
i += 1
return totient, publicKey
def egcd(self,a, b):
if a == 0:
return (b, 0, 1)
g, y, x = self.egcd(b % a, a)
return (g, x - (b // a) * y, y)
def getPrivateKey(self,a, m):
g, x, y = self.egcd(a, m)
if g != 1:
raise Exception('modular inverse does not exist')
return x % m
def gcd(self,x,y):
while(y):
x, y = y, x % y
return x
if __name__ == '__main__':
port = 4999
address = '127.0.0.1'
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.bind((address,port))
print("Listening on the port: ")
s.listen(5)
conn,addr = s.accept()
print("connection established with : ",addr)
Server = RSA()
print("Prime Numbers Selected are: ",Server.p,Server.q)
print("N is: ",Server.n)
print("Totient is:",Server.totient)
gen_st = time.time()
totient, publicKey = Server.getPublicKey()
privateKey = Server.getPrivateKey(publicKey,totient)
if privateKey < 0: privateKey += Server.totient
print("Public Key pair is(N,e): ",Server.n,publicKey)
print("Private Key pair is(N,d):",Server.n,privateKey)
time.sleep(0.004)
gen_entime = time.time()
print(f"rsa generation time : {(gen_entime-gen_st)}")
messageToSend = str(publicKey) + ":" + str(Server.n)
conn.send(messageToSend.encode('ASCII'))
while True:
data = conn.recv(1024).decode('ASCII')
print("Message recieved was: ",data)
print("**********Decrypting********")
decrypt_starttime = time.time()
if type(data):
cipherText = int(data)
privateKey = int(privateKey)
plainText = (cipherText**privateKey) % Server.n
print("PlainText was: ",plainText)
decrypt_endtime = time.time()
print(f"decryption time : {(decrypt_endtime-decrypt_starttime)}")
break