-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchatserver.py
114 lines (100 loc) · 3.51 KB
/
chatserver.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
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
import socket
from rsa import *
print("\nCryptography Chatting Room\n")
s = socket.socket()
host = socket.gethostname()
ip = socket.gethostbyname(host)
port = 1234
s.bind((host, port))
print(host, "(", ip, ")\n")
s.listen(1)
print("\nWaiting for incoming connections...\n")
conn, addr = s.accept()
print("Received connection from ", addr[0], "(", addr[1], ")\n")
print("Client Connected to the chat room\nEnter 'e' to exit chat room\n")
p = int(input("Enter A Large Prime Number: "))
q = int(input("Enter A Large Prime Number (Not same as ALREADY ENTERED!!): "))
public, private = generate_keypair(p, q)
print("Your public key is ", public, " and your private key is ", private)
pub0 = str(public[0])
conn.send(pub0.encode())
pub1 = str(public[1])
conn.send(pub1.encode())
cpub0 = conn.recv(1024)
cpub0 = int(cpub0.decode())
cpub1 = conn.recv(1024)
cpub1 = int(cpub1.decode())
cpub = (cpub0, cpub1)
print("Shared Public Key Is: ", cpub)
while True:
# Encrypting Message
print("Enter 1 to Encrypt with Public Key")
print("Enter 2 to Encrypt with Private Key")
print("Enter 3 to Encrypt with Digital Signature")
eop = int(input("Your Option: "))
# Using Sender's Public Key To Encrypt
if eop == 1:
message = input(str("Me : "))
bye = message
if message == 'e':
message = "Left chat room!"
enc = encrypt(cpub, message)
print("Encryted Message: ", ''.join(map(lambda z: str(z), enc)))
enc = str(enc)
conn.send(enc.encode())
if bye == "[e]":
print("You Left The Chat Room!!\n")
break
# Using Private Key To Encrypt
elif eop == 2:
message = input(str("Me : "))
bye = message
if message == 'e':
message = "Left chat room!"
enc = encrypt(private, message)
print("Encryted Message: ", ''.join(map(lambda z: str(z), enc)))
enc = str(enc)
conn.send(enc.encode())
if bye == 'e':
print("You Left The Chat Room!!\n")
break
# Using Both Keys to Encrypt
elif eop == 3:
message = input(str("Me : "))
bye = message
if message == 'e':
message = "Left chat room!"
sgn = encrypt(private, message)
print("Encryption With Private Key: ", ''.join(map(lambda z: str(z), sgn)))
sgn = str(sgn)
enc = encrypt(cpub, sgn)
enc = str(enc)
lstm = s2l(enc)
print("Encrypted Message: ", ''.join(map(lambda z: str(z), lstm)))
conn.send(enc.encode())
if bye == 'e':
print("You Left The Chat Room!!\n")
break
# Decrypting Message
message = conn.recv(1024)
message = message.decode()
lstm = s2l(message)
print("Client", ":", ''.join(map(lambda z: str(z), lstm)))
print("Enter 1 to Decrypt using Public Key")
print("Enter 2 to Decrypt using Private Key")
print("Enter 3 to Decrypt using Both Keys")
dop = int(input("Your Option: "))
# Using Sender's Public Key To Decrypt
if dop == 1:
dec = decrypt(cpub, lstm)
print("Decrypted Message: ", dec)
# Using Private Key To Decrypt
elif dop == 2:
dec = decrypt(private, lstm)
print("Decrypted Message: ", dec)
# Using Both Keys to Decrypt
elif dop == 3:
dec = decrypt(private, lstm)
lstm = s2l(dec)
dec = decrypt(cpub, lstm)
print("Decrypted Message: ", dec)