久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

嘗試使用私鑰解密消息時出現 node-rsa 錯誤

node-rsa errors when trying to decrypt message with private key(嘗試使用私鑰解密消息時出現 node-rsa 錯誤)
本文介紹了嘗試使用私鑰解密消息時出現 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 錯誤的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Using discord.js to detect image and respond(使用 discord.js 檢測圖像并響應)
Check if user ID exists in Discord server(檢查 Discord 服務器中是否存在用戶 ID)
Guild Member Add does not work (discordjs)(公會成員添加不起作用(discordjs))
Creating my first bot using REPLIT but always error Discord.JS(使用 REPLIT 創建我的第一個機器人,但總是錯誤 Discord.JS)
How do I code event/command handlers for my Discord.js bot?(如何為我的 Discord.js 機器人編寫事件/命令處理程序?)
How to find a User ID from a Username in Discord.js?(如何從 Discord.js 中的用戶名中查找用戶 ID?)
主站蜘蛛池模板: 亚洲乱码国产乱码精品精98午夜 | 久久激情视频 | 国产精品毛片av一区 | 亚洲精品乱码久久久久久蜜桃91 | 国产99免费视频 | 毛片一区 | 久久精品国产亚洲 | 一区二区中文字幕 | 天堂亚洲网 | 日本一区二区三区四区 | 国产乱码精品一品二品 | jvid精品资源在线观看 | 在线观看视频h | 国产成人午夜精品影院游乐网 | 看一级毛片| 视频一区在线 | 国产欧美在线播放 | 免费视频久久 | 美女久久 | 亚洲精品第一页 | 久久精品在线播放 | 国产jizz女人多喷水99 | 欧美一级在线观看 | 中文字幕国产视频 | 久久国内 | 国产精品免费在线 | 精品一区二区久久久久久久网站 | 久久不卡区 | 久久久久久综合 | 国产精品污www一区二区三区 | 亚洲福利在线观看 | 日韩欧美国产精品一区 | 欧美激情久久久 | 欧美一级片在线看 | 国产精品一区二区不卡 | 中文字幕一区二区三区乱码在线 | 91网站视频在线观看 | 精产国产伦理一二三区 | 亚洲a视频 | 成人性生交大片免费看中文带字幕 | 视频一区二区三区四区五区 |