【问题标题】:Node js Verify Crypto MessageNode js 验证加密消息
【发布时间】:2020-05-16 22:09:19
【问题描述】:

我从客户端向服务器发送一个请求,服务器用一个令牌响应客户端和控制器。然后,客户端接收回令牌并使用一些额外信息(客户端 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】:

    好的,我修好了。它现在正在工作。

    这是客户端部分代码:

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

    【讨论】:

      猜你喜欢
      • 2018-01-28
      • 1970-01-01
      • 2022-01-06
      • 2014-11-21
      • 1970-01-01
      • 2021-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多