无法使用 nodejs 加密在第二个程序中验证签名
Can't verify signatuire in 2nd program using nodejs crypto
我有两个 node-js 程序。我想在第一个程序 (node1.js) 中签署一个数字数据并在第二个程序 (node2.js) 中验证它。但它没有发生。我认为这可能是因为我在 write() 中发送参数的方式不正确。请帮忙。谢谢
node1.js
function between(min, max) {
return Math.floor(
Math.random() * (max - min) + min
)
}
var no = between(10, 100);
var crypto = require('crypto');
const node1PrivateKey = "-----BEGIN PRIVATE KEY-----\nMH4CAQAwEAYHKoZIzj0CAQYFK4EEAAMEZzBlAgEBBB4BfZ9QrDb9rRZB+sz5rkcK8VhUAJyJRj6KlOlEVYChQAM+AARylnQB587rYJlx/BvMIdtmLoMfzrCcYzrSMWxSX0tseCpunG03pBnE1mDuIo8lCnxm0kZYC4PUrr9r4f8=\n-----END PRIVATE KEY-----";
//var node1PublicKey = new Buffer("MFIwEAYHKoZIzj0CAQYFK4EEAAMDPgAEGrIPBS4+TSe1rKuuIcGItuYjAgVmIaILaa0MTIcTb7sth05hYtWKwfJA48ZTFQi5iBCH1RqCZyq+nZmB", "hex");
//keypair is correct and generated using other program
const sign = crypto.createSign('SHA256');
sign.write('no');
sign.end();
const signature = sign.sign(node1PrivateKey, 'hex');
module.exports = {
node1_no: no,
node1_signature: signature
}
node2.js
var crypto = require('crypto');
const node1PublicKey = "-----BEGIN PUBLIC KEY-----\nMFIwEAYHKoZIzj0CAQYFK4EEAAMDPgAEGrIPBS4+TSe1rKuuIcGItuYjAgVmIaILaa0MTIcTb7sth05hYtWKwfJA48ZTFQi5iBCH1RqCZyq+nZmB\n-----END PUBLIC KEY-----";
var node1 = require('./node1.js');
console.log(node1.node1_no);
console.log(node1.node1_signature);
const verify = crypto.createVerify('SHA256');
verify.write('node1.node1_no');
verify.end();
console.log(verify.verify(node1PublicKey, node1.node1_signature, 'hex'));
它 returns 错误。
预期结果为 TRUE。当我使用相同的变量名 ('no') 它 returns TRUE.
终于解决了。问题是 write()
函数只接受字符串。但我发送的是整数值。
现在,我将整数转换为字符串并将值传递给 write()
函数并且它起作用了。
node1.js
function between(min, max) {
return Math.floor(
Math.random() * (max - min) + min
)
}
var no = between(10, 100);
var no_str = veh.toString();
var crypto = require('crypto');
const node1PrivateKey = "-----BEGIN PRIVATE KEY-----\nMH4CAQAwEAYHKoZIzj0CAQYFK4EEAAMEZzBlAgEBBB4BfZ9QrDb9rRZB+sz5rkcK8VhUAJyJRj6KlOlEVYChQAM+AARylnQB587rYJlx/BvMIdtmLoMfzrCcYzrSMWxSX0tseCpunG03pBnE1mDuIo8lCnxm0kZYC4PUrr9r4f8=\n-----END PRIVATE KEY-----";
//var node1PublicKey = new Buffer("MFIwEAYHKoZIzj0CAQYFK4EEAAMDPgAEGrIPBS4+TSe1rKuuIcGItuYjAgVmIaILaa0MTIcTb7sth05hYtWKwfJA48ZTFQi5iBCH1RqCZyq+nZmB", "hex");
const sign = crypto.createSign('SHA256');
sign.write('no_str');
sign.end();
const signature = sign.sign(node1PrivateKey, 'hex');
module.exports = {
node1_no: no,
node1_signature: signature
}
second.js
var crypto = require('crypto');
const node1PublicKey = "-----BEGIN PUBLIC KEY-----\nMFIwEAYHKoZIzj0CAQYFK4EEAAMDPgAEGrIPBS4+TSe1rKuuIcGItuYjAgVmIaILaa0MTIcTb7sth05hYtWKwfJA48ZTFQi5iBCH1RqCZyq+nZmB\n-----END PUBLIC KEY-----";
var node1 = require('./node1.js');
var no_string = node1.node1_veh
const verify = crypto.createVerify('SHA256');
verify.write('no_string');
verify.end();
console.log(verify.verify(node1PublicKey, node1.node1_signature, 'hex'));
我有两个 node-js 程序。我想在第一个程序 (node1.js) 中签署一个数字数据并在第二个程序 (node2.js) 中验证它。但它没有发生。我认为这可能是因为我在 write() 中发送参数的方式不正确。请帮忙。谢谢
node1.js
function between(min, max) {
return Math.floor(
Math.random() * (max - min) + min
)
}
var no = between(10, 100);
var crypto = require('crypto');
const node1PrivateKey = "-----BEGIN PRIVATE KEY-----\nMH4CAQAwEAYHKoZIzj0CAQYFK4EEAAMEZzBlAgEBBB4BfZ9QrDb9rRZB+sz5rkcK8VhUAJyJRj6KlOlEVYChQAM+AARylnQB587rYJlx/BvMIdtmLoMfzrCcYzrSMWxSX0tseCpunG03pBnE1mDuIo8lCnxm0kZYC4PUrr9r4f8=\n-----END PRIVATE KEY-----";
//var node1PublicKey = new Buffer("MFIwEAYHKoZIzj0CAQYFK4EEAAMDPgAEGrIPBS4+TSe1rKuuIcGItuYjAgVmIaILaa0MTIcTb7sth05hYtWKwfJA48ZTFQi5iBCH1RqCZyq+nZmB", "hex");
//keypair is correct and generated using other program
const sign = crypto.createSign('SHA256');
sign.write('no');
sign.end();
const signature = sign.sign(node1PrivateKey, 'hex');
module.exports = {
node1_no: no,
node1_signature: signature
}
node2.js
var crypto = require('crypto');
const node1PublicKey = "-----BEGIN PUBLIC KEY-----\nMFIwEAYHKoZIzj0CAQYFK4EEAAMDPgAEGrIPBS4+TSe1rKuuIcGItuYjAgVmIaILaa0MTIcTb7sth05hYtWKwfJA48ZTFQi5iBCH1RqCZyq+nZmB\n-----END PUBLIC KEY-----";
var node1 = require('./node1.js');
console.log(node1.node1_no);
console.log(node1.node1_signature);
const verify = crypto.createVerify('SHA256');
verify.write('node1.node1_no');
verify.end();
console.log(verify.verify(node1PublicKey, node1.node1_signature, 'hex'));
它 returns 错误。 预期结果为 TRUE。当我使用相同的变量名 ('no') 它 returns TRUE.
终于解决了。问题是 write()
函数只接受字符串。但我发送的是整数值。
现在,我将整数转换为字符串并将值传递给 write()
函数并且它起作用了。
node1.js
function between(min, max) {
return Math.floor(
Math.random() * (max - min) + min
)
}
var no = between(10, 100);
var no_str = veh.toString();
var crypto = require('crypto');
const node1PrivateKey = "-----BEGIN PRIVATE KEY-----\nMH4CAQAwEAYHKoZIzj0CAQYFK4EEAAMEZzBlAgEBBB4BfZ9QrDb9rRZB+sz5rkcK8VhUAJyJRj6KlOlEVYChQAM+AARylnQB587rYJlx/BvMIdtmLoMfzrCcYzrSMWxSX0tseCpunG03pBnE1mDuIo8lCnxm0kZYC4PUrr9r4f8=\n-----END PRIVATE KEY-----";
//var node1PublicKey = new Buffer("MFIwEAYHKoZIzj0CAQYFK4EEAAMDPgAEGrIPBS4+TSe1rKuuIcGItuYjAgVmIaILaa0MTIcTb7sth05hYtWKwfJA48ZTFQi5iBCH1RqCZyq+nZmB", "hex");
const sign = crypto.createSign('SHA256');
sign.write('no_str');
sign.end();
const signature = sign.sign(node1PrivateKey, 'hex');
module.exports = {
node1_no: no,
node1_signature: signature
}
second.js
var crypto = require('crypto');
const node1PublicKey = "-----BEGIN PUBLIC KEY-----\nMFIwEAYHKoZIzj0CAQYFK4EEAAMDPgAEGrIPBS4+TSe1rKuuIcGItuYjAgVmIaILaa0MTIcTb7sth05hYtWKwfJA48ZTFQi5iBCH1RqCZyq+nZmB\n-----END PUBLIC KEY-----";
var node1 = require('./node1.js');
var no_string = node1.node1_veh
const verify = crypto.createVerify('SHA256');
verify.write('no_string');
verify.end();
console.log(verify.verify(node1PublicKey, node1.node1_signature, 'hex'));