Python聊天与加密模块不解密

2024-04-26 11:44:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我的python聊天来学习sockets是可行的,今天我开始实现加密模块来进行加密聊天。但我想我弄乱了公钥和私钥

当客户机连接到服务器时,他们进行握手以交换公钥。所以客户机有自己的密钥来解密,加上服务器的公钥来加密传出的消息。服务器端:每个客户端都是一个线程,握手后存储自己的公钥来加密传出消息,服务器密钥来解密

工作流是(从我的观点):发送方客户端加密消息,发送到服务器,服务器用自己的私钥解密,服务器用自己的公钥加密消息到所有其他客户端。最后,收件人客户端使用服务器pubkey解密消息

当使用send_allsend_all_no_room方法向所有人发送消息时,我面临的问题是。有时消息被正确解密,但大多数情况下没有正确解密

在哪一点上我丢失了正确的钥匙

这里有serverclientcomms(send、receive方法)

考虑到我只实现了send_allsend_all_no_room函数的自定义发送、接收、加密和解密方法。例如send_private_msg不起作用


Tags: 模块方法no服务器send消息客户端客户机
1条回答
网友
1楼 · 发布于 2024-04-26 11:44:33

问题出在send_all函数中:

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)

for循环的每次迭代都使用相同的client。同样的问题也存在于send_all_no_room函数中

固定代码:

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)

相关问题 更多 >