-
Notifications
You must be signed in to change notification settings - Fork 0
/
server222.py
62 lines (46 loc) · 1.59 KB
/
server222.py
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
import socket
from Crypto.PublicKey import RSA
from Crypto import Random
import commands
from Crypto.Cipher import PKCS1_OAEP
import sys
import ast
def generate_key():
random_value = Random.new().read
generate_key.private_key = RSA.generate(1024,random_value)
generate_key.public_key = generate_key.private_key.publickey().exportKey()
def main():
sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
host = socket.gethostbyname(socket.gethostname())
port = 8000
encrypt_str = "encrypted_message="
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind((host,port))
sock.listen(4)
print("Starting Server...")
(c, (addr,ip)) = sock.accept()
print("Got Connections from > " , str(addr))
generate_key()
while 1:
data = c.recv(1024)
data = data.replace("\r\n", '')
if data == "Client: OK":
c.send("public_key=" + generate_key.public_key + "\n")
print("Public key sent to client")
elif encrypt_str in data :
data = data.replace(encrypt_str, '')
print("Received Encrypted Message \n" + str(data))
encrypted = eval(data)
decrypted = generate_key.private_key.decrypt(encrypted)
c.send("server: OK")
print("decrypted message : " + decrypted)
sys.exit()
elif data == "QUIT" :
print("Client asked to close the server : ")
break
elif len(data) == 0:
break
c.send("Server stopped\n")
sock.close()
if __name__ == "__main__":
main()