[英]Emit encrypted message to socket.io room
I try to send encrypted data by using AES encryption protocol (send AES-key by RSA to client) and after I emit data to the room with many users I'm unable to decrypt messages on client side. 我尝试使用AES加密协议(通过RSA发送AES密钥到客户端)发送加密数据,并且在有很多用户向房间发送数据后,我无法在客户端解密消息。 This connected with AES-key issue. 这与AES密钥问题有关。 Every time when I send message from client-side to server, server generate new AES-key, so each time messages which were emitted by client it use different keys. 每当我从客户端向服务器发送消息时,服务器都会生成新的AES密钥,因此,每次客户端发出的消息都会使用不同的密钥。 And after that I face with my problem. 之后,我面对了我的问题。 What should I do to send AES encrypted message to the socket.io room? 如何将AES加密消息发送到socket.io房间?
There is some code 有一些代码
Server: 服务器:
socket.on('send', function (data) {
// Data decryption using AES
let decryptedMessage = aesWrapper.decrypt(aesKey, data.message);
let decryptedRoom = aesWrapper.decrypt(aesKey, data.room);
data['message'] = decryptedMessage;
data['room'] = decryptedRoom;
// Trying to parse each user with unique AES key
io_s.in(decryptedRoom).clients((error, clients) => {
if (error) {
console.log(error);
} else {
clients.forEach(client => {
let ecryptedMessage = aesWrapper.createAesMessage(aesKey, data['message']);
let ecryptedRoom = aesWrapper.createAesMessage(aesKey, data['room']);
let dataNew = { type: data.type, message: ecryptedMessage, room: ecryptedRoom }
console.log(dataNew);
socket.to(client).emit('message', dataNew);
});
}
})
console.log(socket.id);
});
By this code I work only with one socket connection, so encryption work only for him and others couldn't decrypt it. 通过此代码,我只能使用一个套接字连接,因此加密仅对他有效,而其他人则无法对其解密。
I also tried to use RSA encryption and send AES-key for every connected sockets to the room, but result was the same. 我还尝试使用RSA加密,并将每个连接的套接字发送AES密钥到房间,但是结果是相同的。
Code: 码:
const newAesKey = aesWrapper.generateKey();
let encryptedAesKey = rsaWrapper.encrypt(rsaWrapper.clientPub, (newAesKey.toString('base64')));
socket.to(decryptedRoom).emit('send key from server to client', encryptedAesKey);
socket.on('aes client encrypted message', () => {
let ecryptedMessage = aesWrapper.createAesMessage(newAesKey, data['message']);
console.log(ecryptedMessage);
let ecryptedRoom = aesWrapper.createAesMessage(newAesKey, data['room']);
let dataNew = { type: data.type, message: ecryptedMessage, room: ecryptedRoom, nickname: data.nickname }
console.log(dataNew);
socket.to(decryptedRoom).emit('message', dataNew);
})
我已经通过添加数组变量解决了这个问题,在这里我为每个用户保存了aes-keys。
keysUsers[socket.id] = aesKey;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.