因此,我一直試圖使用節點node-rsa和javascript與jsencrypt來創建一個網站(用於分配),其中javascript客戶端獲取由公鑰生成的公鑰服務器(node-rsa)加密用戶輸入的消息(jsencrypt),將其發送到服務器並獲取服務器解密(node-rsa)。密鑰的生成工作,加密工作,但解密沒有。當我開始節點腳本我做了以下的加密...嘗試解密私鑰消息時發生node-rsa錯誤
var NodeRSA = require('node-rsa');
var myDecrypter = new NodeRSA({b: 512});
當客戶端請求的鍵(我用快遞)以下爲然。
app.get('/getPublicKey', function(req, res){
var publicKeyJson = {"Key": ""};
console.log(myDecrypter.exportKey('public'));
publicKeyJson.Key = myDecrypter.exportKey('public');
res.json(JSON.stringify(publicKeyJson));
});
客戶端然後保存像這樣的關鍵...
var myEncrypter = new JSEncrypt();
var myJson = "";
$.getJSON("getPublicKey", function(data) {
myJson = JSON.parse(data).Key;
setKey();
});
function setKey() {
myEncrypter.setPublicKey(myJson);
}
當我到加密併發送客戶端我做這個消息......
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);
}
當服務器收到消息時,會發生這種情況,這是我得到錯誤的地方。
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);
});
我得到的是錯誤...
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)
然而在這裏是它變得有趣,上面有我的私鑰來獲取錯誤信息......
-----BEGIN RSA PRIVATE KEY-----
MIIBOgIBAAJBAIhdx31QICGN1LKRW4WngeL3RtzPh7cEHmhFJB8m4bQUSTcSi4eg
sUvMeZkWyaF9gOxtZKzk5TI6q+8hg8TY6S8CAwEAAQJASds423cVH/c4NsqhXh8e
KvYwjBFeeNIjQegIq1KctbHmKNM5MMb4jnDqdY/S5XHHS22EGvLNheLgV8tlRjwG
UQIhANpNmbl215eOsGPJ0jqz1XPMBrO35V6I3P04kvr66R1JAiEAn+oL0jtAFETR
4PRfenye5MAu9US3V5MoDN8xUoEvKrcCIQDQT2ZWNNIrHAyzXB2QyJPxqInoqp1j
5QPDWl3ewtj5iQIgY3E1nKw/stsA8LTGUvMAFBv2l4r9wDXAaBC7KSUwYY0CIAj4
0gA9etDbPm3H/XDwK4WXs9mXkKroyxewkWoOoAw/
-----END RSA PRIVATE KEY-----
和發送給客戶的公鑰是...
-----BEGIN PUBLIC KEY-----
MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAIhdx31QICGN1LKRW4WngeL3RtzPh7cE
HmhFJB8m4bQUSTcSi4egsUvMeZkWyaF9gOxtZKzk5TI6q+8hg8TY6S8CAwEAAQ==
-----END PUBLIC KEY-----
條的加密消息(計算器)是...
XDViV0InCSnpyBxbNu5Herut0JYSsp87buvhzM4g2f9z3khIx2zA8Ou0Uq0TtmqtvBBVtZi5wZbcS6em/vB78g==
有趣的是,當我用demo上jsencrypt網站,輸入我的私鑰以及加密消息我得到正確解密的消息。
所以我的問題是...
我在做什麼毛病我節點-RSA解密???
如果您需要更多信息/代碼,請將其置於下面的評論中。
可能是你的例子b/c複雜。讓它更簡單,並提供給我們一個失敗的jsfiddle.net – Oleander