Node js验证密码消息

问题描述 投票:0回答:1

我从客户端向服务器发送请求,服务器使用令牌将响应返回给客户端和控制器。然后,客户端返回令牌并使用一些额外信息(客户端IP,时间和自己的区块链地址)对令牌进行签名。之后,客户端使用自己的公钥发送已签名的信息。一切都很好,直到这里。

现在,控制器接收到带有签名信息和公共密钥的消息。尝试使用即将到来的公钥和已经拥有的消息来验证此签名信息。

这里是客户零件代码:

  var message = token + "," + client.address().address + "," + time + "," + my_public;
  var message_buf = Buffer.from(message);
  const sign = crypto.createSign('SHA256');
  sign.write(message_buf);
  sign.end();
  const signature = sign.sign(my_private, 'hex');
  var sign_pub = signature.toString() + "," + my_public.toString();
  var sign_pub_buf = Buffer.from(sign_pub);       
  console.log("sign_pub = ", sign_pub_buf);
  client.send(sign_pub_buf, sdn_port, host, function(error){
      if(error){
          client.close();
      }
      else{
          console.log('Sign+Public_K has been sent to SDN  !!!');
      }

这里是控制器的零件代码:

udpsocket_sdn.on('message', function(msg, rinfo) {
    console.log('Data received from CLIENT : ' ,msg);
    var sig_pub = msg.toString().split(",");
    var sig = sig_pub[0];
    var pub = sig_pub[1];
    console.log("sig = ", sig);
    console.log("pub = ", pub);
    var message = token + "," + rinfo.address + "," + time + "," + pub;
    var message_buf = Buffer.from(message);
    const verify = crypto.createVerify('SHA256');
    verify.write(message_buf);
    verify.end();
    var isGood = verify.verify(pub, sig, 'hex');
    if(isGood){   
        console.log('All Good');          
    }
    else {
         console.log('Nope !');
    }    
}
javascript node.js cryptojs verify
1个回答
0
投票

确定,我已修复。现在正在工作。

这里是客户零件代码:

const { privateKey, publicKey } = crypto.generateKeyPairSync('rsa', {
    modulusLength: 2048,
});
  
var my_public = publicKey.export({type: 'spki', format: 'pem'});
var my_private = privateKey;
var time = 100;


client.on('message',function(msg, info){
    if(data_cnt == 1){
        console.log('Random number received from SERVER !');
        data_cnt++;
        var token_tot = msg.toString().split(",")
        usr1_pub_pem = token_tot[0];
        token = token_tot[1];
        var message = token + "," + ip + "," + time + "," + Buffer.from(my_public);
        console.log("Client address = ", ip)
        const sign = crypto.createSign('SHA256');
        sign.write(message);
        sign.end();
        const signature = sign.sign(my_private, 'hex');      
        client.send(signature, sdn_port, host, function(error){
            if(error){
                client.close();
            }
            else{
                console.log('Signature has been sent to SDN  !!!');     
                client.send(my_public, sdn_port, host, function(error){
                    if(error){
                        client.close();
                    }
                    else{
                        console.log('Public Key has been sent to SDN  !!!');
                    }
                });                              
            }
        });                
    }
});

现在,控制器部分:

var time = 100;
udpsocket_sdn.on('message', function(msg, rinfo) {
    count_sdn = count_sdn + 1;
    if(count_sdn == 1){
        console.log('Signature data received from CLIENT !!');
        sig_pub = msg.toString();
    } 
    else if (count_sdn == 2){
        console.log('Public key data received from CLIENT !! ');
        pub = msg;
        var message = token + "," + rinfo.address + "," + time + "," + pub;
        console.log("Client address = ", rinfo.address)
        const verify = crypto.createVerify('SHA256');
        verify.write(message);
        verify.end();
        var isGood = verify.verify(pub, sig_pub, 'hex');
        if(isGood){   
            console.log('All Good');          
        }
        else {
            console.log('Nope !');
        }          
    }
});
© www.soinside.com 2019 - 2024. All rights reserved.