如何从十六进制编码模数创建RSA密钥并在node.js中加密

时间:2015-12-09 14:14:54

标签: node.js encryption rsa

我在node.js中找到RSA加密的方法,我想处理大数字,所以我使用它:https://github.com/eschnou/node-bignumber

我要做的只是加密,模数和指数。我不需要做解密。无论如何,我有一个错误,但我不知道什么是错的。任何人都可以知道这是错的吗?感谢。

代码

require("node-bignumber");
var nvalue="ad6eb61316ff805e9c94667ab04aa45aa3203eef71ba8c12afb353a5c7f11657e43f5ce4483d4e6eca46af6b3bde4981499014730d3b233420bf3ecd3287a2768da8bd401f0abd7a5a137d700f0c9d0574ef7ba91328e9a6b055820d03c98d56943139075d";
var evalue="010001";
var encpw="";
var rsa = new RSAKey;

function encryptMessage() {
    var message = "All your bases are belong to us.";
    rsa.setPublic(evalue, nvalue);
    encpw.value = rsa.encrypt(message);
    console.log(encpw);
}

encryptMessage();

错误

ReferenceError: RSAKey is not defined
    at Object.<anonymous> (c:\Users\win\Desktop\untitled\juntae.js:5:15)
    at Module._compile (module.js:435:26)
    at Object.Module._extensions..js (module.js:442:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:311:12)
    at Function.Module.runMain (module.js:467:10)
    at startup (node.js:136:18)
    at node.js:963:3

enter image description here

1 个答案:

答案 0 :(得分:2)

您应该能够以这种方式从模数和公共指数加载RSA公钥:

var rsa = require("node-bignumber");

var n = "ad6eb61316ff805e9c94667ab04aa45aa3203eef71ba8c12afb353a5c7f11657e43f5ce4483d4e6eca46af6b3bde4981499014730d3b233420bf3ecd3287a2768da8bd401f0abd7a5a137d700f0c9d0574ef7ba91328e9a6b055820d03c98d56943139075d";
var e = "010001";
var pub = new rsa.Key();
pub.setPublic(n, e);

var message = "Message";
var encrypted = pub.encrypt(message);

console.log(encrypted);

RSA中的公钥由模数和公共指数组成,因为在加密期间需要这两个值。