Reputation: 429
My python chat to learn about sockets is working and today I started to implement the Crypto module to make an encrypted chat. But I think I'm messing with public and private keys.
When a client connects to the server, they do a handshake to exchange their public keys. So client has their own keys to decrypt plus server public key to encrypt outgoing messages. And server side: each client it's a thread and after handshake stores its own public key to encrypt outgoing messages and server keys to decrypt.
Workflow is (from my POV): sender client encrypts a message, is send to server, server decrypts with its own privkey, server encrypts the message to all other clients with their own pubkey. Finally recipient client decrypts the message with server pubkey.
The problem I'm facing when sending messages to all, using send_all
and send_all_no_room
methods. Sometimes message is decrypted correctly but most of times is not decrypted correctly.
In which point I'm losing the correct key?
Here there are the server, client, and comms (send, receive methods)
Take into account that I only implemented custom send, receive, encrypt, decrypt methods to send_all
and send_all_no_room
functions. For example send_private_msg
won't work.
Upvotes: 0
Views: 78
Reputation: 43136
The problem is in the send_all
function:
def send_all(self, message):
"""Send to all method, broadcast a message"""
for sock in [client.sock for client in clients]:
if sock != self.sock:
message_encripted = self.encriptar(message,client)
send(sock,message_encripted)
Each iteration of the for
loop uses the same client
. The same problem exists in the send_all_no_room
function.
Fixed code:
def send_all(self, message):
"""Send to all method, broadcast a message"""
for client in clients:
if client is not self:
message_encripted = self.encriptar(message,client)
send(client.sock,message_encripted)
Upvotes: 1