尝试使用私钥解密消息时出现 node-rsa 错误

时间:2022-11-03
本文介绍了尝试使用私钥解密消息时出现 node-rsa 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

所以我一直在尝试将 node 与 node-rsa 和 javascript 一起使用jsencrypt 创建一个网站(用于分配),其中 javascript 客户端获取服务器生成的公钥(node-rsa),对用户输入的消息(jsencrypt)进行加密,将其发送到服务器并让服务器对其进行解密(node-rsa).密钥的生成有效,加密有效,但解密无效.当我启动节点脚本时,我会为加密执行以下操作...

So I've been trying to use node with node-rsa and javascript with jsencrypt to create a website (for an assignment) where the javascript client gets the public key generated by the server (node-rsa), encrypts the message (jsencrypt) that the user has entered, sends it to the server and gets the server to decrypt it (node-rsa). The generation of the keys works, the encryption works however the decryption doesn't. When I start the node script I do the following for the encryption...

var NodeRSA = require('node-rsa');
var myDecrypter = new NodeRSA({b: 512});

当客户端请求密钥(我使用的是 express)时,会运行以下内容.

When the client requests the key (I am using express) the following is ran.

app.get('/getPublicKey', function(req, res){
    var publicKeyJson = {"Key": ""};
    console.log(myDecrypter.exportKey('public'));
    publicKeyJson.Key = myDecrypter.exportKey('public');
    res.json(JSON.stringify(publicKeyJson));
});

客户端然后像这样保存该密钥...

The client then saves that key like this...

var myEncrypter = new JSEncrypt();
var myJson  = "";
$.getJSON( "getPublicKey", function( data ) {
    myJson = JSON.parse(data).Key;
        setKey();
});
function setKey() {
    myEncrypter.setPublicKey(myJson);
}

当我必须在客户端上加密并发送消息时,我会这样做......

When I got to encrypt and send the message on the client I do this...

function messageEncrypt() {
    message = document.getElementById("message").value;
    var encrypted = myEncrypter.encrypt(message);
    myMessage = {"username": "", "userId": 0.0, "message": ""};
    myMessage.username = me.username;
    myMessage.userId = me.userId;
    myMessage.message = encrypted;
    console.log(encrypted);
    $.post("sendMessage", myMessage);
}

当服务器收到一条消息时,就会发生这种情况,这就是我得到错误的地方.

When the server receives a message this is what happens, this is where I get the errors.

app.post('/sendMessage', function(req, res){
    var message = req.body;
    var user = message.username;
    var id = message.userId;
    console.log("What a mess, " + user + " said " + message.message + " what on earth does that mean");
    //This line below errors
    var clearMessage = myDecrypter.decrypt(message.message, 'utf8');
    console.log(user + " said " + clearMessage);
});

我得到的错误是......

The error I get is ...

Error: Error during decryption (probably incorrect key). Original error: Error: error:040A1079:rsa routines:RSA_padding_check_PKCS1_OAEP_mgf1:oaep decoding error
    at Error (native)
    at NodeRSA.module.exports.NodeRSA.$$decryptKey (/home/node_modules/node-rsa/src/NodeRSA.js:295:19)
    at NodeRSA.module.exports.NodeRSA.decrypt (/home/node_modules/node-rsa/src/NodeRSA.js:243:21)
    at /home/securechat/securechat.js:36:36
    at Layer.handle [as handle_request] (/home/node_modules/express/lib/router/layer.js:95:5)
    at next (/home/node_modules/express/lib/router/route.js:131:13)
    at Route.dispatch (/home/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/home/node_modules/express/lib/router/layer.js:95:5)
    at /home/node_modules/express/lib/router/index.js:277:22
    at Function.process_params (/home/node_modules/express/lib/router/index.js:330:12)

然而,有趣的是,为了得到上面的错误消息,我有一个私钥......

Here however is where it gets interesting, to get that error message above I had a private key of...

-----BEGIN RSA PRIVATE KEY-----
MIIBOgIBAAJBAIhdx31QICGN1LKRW4WngeL3RtzPh7cEHmhFJB8m4bQUSTcSi4eg
sUvMeZkWyaF9gOxtZKzk5TI6q+8hg8TY6S8CAwEAAQJASds423cVH/c4NsqhXh8e
KvYwjBFeeNIjQegIq1KctbHmKNM5MMb4jnDqdY/S5XHHS22EGvLNheLgV8tlRjwG
UQIhANpNmbl215eOsGPJ0jqz1XPMBrO35V6I3P04kvr66R1JAiEAn+oL0jtAFETR
4PRfenye5MAu9US3V5MoDN8xUoEvKrcCIQDQT2ZWNNIrHAyzXB2QyJPxqInoqp1j
5QPDWl3ewtj5iQIgY3E1nKw/stsA8LTGUvMAFBv2l4r9wDXAaBC7KSUwYY0CIAj4
0gA9etDbPm3H/XDwK4WXs9mXkKroyxewkWoOoAw/
-----END RSA PRIVATE KEY-----

发送给客户端的公钥是……

and the public key sent to the client was...

-----BEGIN PUBLIC KEY-----
MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAIhdx31QICGN1LKRW4WngeL3RtzPh7cE
HmhFJB8m4bQUSTcSi4egsUvMeZkWyaF9gOxtZKzk5TI6q+8hg8TY6S8CAwEAAQ==
-----END PUBLIC KEY-----

加密的消息(stackoverflow)是...

The encrypted messages (stackoverflow) was ...

XDViV0InCSnpyBxbNu5Herut0JYSsp87buvhzM4g2f9z3khIx2zA8Ou0Uq0TtmqtvBBVtZi5wZbcS6em/vB78g==

有趣的是,当我在 jsencrypt 网站上使用 demo 并输入我的私钥以及加密消息我得到了正确的解密消息.

The interesting thing is that when I used the demo on jsencrypt website and enter my private key as well as the encrypted message I get the correct decrypted message.

所以我的问题是......

So my question is...

我的 node-rsa 解密有什么问题???

What am I doing wrong with my node-rsa decryption???

如果您需要更多信息/代码,请将其放在下面的评论中.

If you need anymore information/code please put it in the comments below.

推荐答案

回答你的问题 @Curious_Programmer 默认 node-rsa 使用 pkcs1_oaep 进行加密和解密,而 jsencrypt 使用 pkcs1.值得庆幸的是 node 可以让你更改加密方案,你需要做的是添加 ...

To answer your question @Curious_Programmer be default node-rsa uses pkcs1_oaep for encryption and decryption while jsencrypt uses pkcs1. Thankfully node lets you change the encryptionScheme, what you need to do is add ...

myDecrypter.setOptions({encryptionScheme: 'pkcs1'});

var myDecrypter = new NodeRSA({b: 512});

一切都会像魅力一样工作,我希望我能帮助你;)

and all will work like a charm, I hoped I helped you ;)

这篇关于尝试使用私钥解密消息时出现 node-rsa 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一条:将游戏分数从客户端更新到服务器数据库的最安全方法?Javascript 下一条:没有 ssl 的 Web 加密 API

相关文章

最新文章