簡體   English   中英

將加密的消息發送到socket.io房間

[英]Emit encrypted message to socket.io room

我嘗試使用AES加密協議(通過RSA發送AES密鑰到客戶端)發送加密數據,並且在有很多用戶向房間發送數據后,我無法在客戶端解密消息。 這與AES密鑰問題有關。 每當我從客戶端向服務器發送消息時,服務器都會生成新的AES密鑰,因此,每次客戶端發出的消息都會使用不同的密鑰。 之后,我面對了我的問題。 如何將AES加密消息發送到socket.io房間?

有一些代碼

服務器:

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);
});

通過此代碼,我只能使用一個套接字連接,因此加密僅對他有效,而其他人則無法對其解密。

我還嘗試使用RSA加密,並將每個連接的套接字發送AES密鑰到房間,但是結果是相同的。

碼:

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM