“抛出错误(”意外数据类型“);”在RSA加密期间?

时间:2018-06-27 23:38:43

标签: javascript node.js encryption certificate rsa

我正在尝试使用中的nodeRSA库编写一些加密代码 Node.js:https://github.com/rzcoder/node-rsa 现在,我正在运行事件发射器以测试我的加密方法。但是,当我通过侦听器进行测试时,我不断收到错误消息:

1 Listner(s) listening to decrypt event
listener1 executed.
Listener1 will not listen now.
listener2 executed.
1 Listener(s) listening to encrypt event
listener3 executed.
1 Listener(s) listening to signing event
listener4 executed.
undefined
C:\Users\wyin\Documents\GitHub\votechain_node\node_modules\node-rsa\src\NodeRSA.js:375
            throw Error("Unexpected data type");
            ^

Error: Unexpected data type
    at NodeRSA.module.exports.NodeRSA.$getDataForEncrypt (C:\Users\wyin\Documents\GitHub\votechain_node\node_modules\node-rsa\src\NodeRSA.js:375:19)
    at NodeRSA.module.exports.NodeRSA.verify (C:\Users\wyin\Documents\GitHub\votechain_node\node_modules\node-rsa\src\NodeRSA.js:341:41)
    at verification (C:\Users\wyin\Documents\GitHub\votechain_node\MultithreadSupportForCSR.js:21:16)
    at EventEmitter.listener4 (C:\Users\wyin\Documents\GitHub\votechain_node\MultithreadSupportForCSR.js:62:12)
    at EventEmitter.emit (events.js:182:13)
    at Object.<anonymous> (C:\Users\wyin\Documents\GitHub\votechain_node\MultithreadSupportForCSR.js:90:14)
    at Module._compile (internal/modules/cjs/loader.js:702:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
    at Module.load (internal/modules/cjs/loader.js:612:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:551:12)

我的代码如下:

const cluster = require('cluster');
const http = require('http');
const numCPUs = require('os').cpus().length;
let events = require('events');
const crypto = require('crypto');
const readline = require('readline');
const sign = crypto.createSign('SHA256');
const NodeRSA = require('node-rsa');

let eventEmitter = new events.EventEmitter();
let key = new NodeRSA({});
key.generateKeyPair(2048);



function signing(message) {
    key.sign(message);
}

function verification(signature, message) {
    return key.verify(message, signature);
}


/*let rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
  });

  rl.question('What do you think of Node.js? ', (answer) => {
    // TODO: Log the answer in a database
    console.log(`Thank you for your valuable feedback: ${answer}`);

    rl.close();
  });*/ // for reading user input later
let message1 = Buffer.from("Hi, this is just the first test").toString('base64');
console.log(message1);
let temp = "random";

let listener1 = function() {
    console.log('listener1 executed.');
    temp = key.encrypt(message1, "base64");
    //console.log(temp);
}

let listener2 = function() {
    console.log('listener2 executed.');
    temp = key.decrypt(temp, "base64");
    //console.log(temp);

}

let listener3 = function() {
    console.log('listener3 executed.');
    temp = signing(temp);
    //console.log(temp);
}

let listener4 = function() {
    console.log('listener4 executed.');
    console.log(typeof temp);
    temp = verification(message1.toString('base64'), temp);
}

eventEmitter.addListener('encrypt', listener1);

eventEmitter.on('decrypt', listener2);
eventEmitter.on('sign', listener3);
eventEmitter.on('validate', listener4);
let eventListeners = require('events').EventEmitter.listenerCount(eventEmitter, 'decrypt');
console.log(eventListeners + " Listner(s) listening to decrypt event");

// Fire the connection event 
eventEmitter.emit('encrypt');

// Remove the binding of listner1 function
eventEmitter.removeListener('connection', listener1);
console.log("Listener1 will not listen now.");

// Fire the connection event 
eventEmitter.emit('decrypt');

eventListeners = require('events').EventEmitter.listenerCount(eventEmitter, 'decrypt');
console.log(eventListeners + " Listener(s) listening to encrypt event");

eventEmitter.emit("sign");
eventListeners = require('events').EventEmitter.listenerCount(eventEmitter, 'sign');
console.log(eventListeners + " Listener(s) listening to signing event");

eventEmitter.emit("validate");
eventListeners = require('events').EventEmitter.listenerCount(eventEmitter, 'validate');
console.log(eventListeners + " Listener(s) listening to validation event");


console.log("Program Ended.");

我在做什么错了?

0 个答案:

没有答案