Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript CryptoJs没有解密我的NodeJS服务器上的URL_Javascript_Node.js_Cryptojs - Fatal编程技术网

Javascript CryptoJs没有解密我的NodeJS服务器上的URL

Javascript CryptoJs没有解密我的NodeJS服务器上的URL,javascript,node.js,cryptojs,Javascript,Node.js,Cryptojs,我正在将API调用从前端转发到后端。我使用密码短语“somekey”使用CryptoJS.AES加密API调用 我的相关客户代码是 var host = 'http://localhost:3000' $('.send-button').click(function(){ var request = $('.request-input').val(); var encryptedRequest = CryptoJS.AES.encrypt(request, 'somekey');

我正在将API调用从前端转发到后端。我使用密码短语“somekey”使用CryptoJS.AES加密API调用

我的相关客户代码是

var host = 'http://localhost:3000'

$('.send-button').click(function(){
  var request = $('.request-input').val();
  var encryptedRequest = CryptoJS.AES.encrypt(request, 'somekey');
  console.log(encryptedRequest.toString())  
  var decryptedRequest = CryptoJS.AES.decrypt(encryptedRequest, 'somekey');
  console.log('Decrypted Request: ' + decryptedRequest.toString());
  handleRequest(encryptedRequest.toString());
});

var handleRequest = function(request){
    $.ajax({
        type: "GET",
        url: host + '/requests?call=' + request,
        success: function(data) {
        var rawJSON = JSON.stringify(data, null, 2);
        editor.setValue(rawJSON);
        },
        dataType: 'json'
   });
}
var port = 3000;
var serverUrl = "127.0.0.1";

var http = require("http");
var path = require("path");
var fs = require("fs");
var express = require("express");
var CryptoJs = require("crypto-js");
var app = express(); 

app.get('/requests', function(req, res) {
    console.log('REQUEST: ' + req);
    var call = req.query.call;
    console.log(call)
    console.log("To send: " + CryptoJs.AES.decrypt(call, 'somekey'));
}); 
相关的服务器端代码是

var host = 'http://localhost:3000'

$('.send-button').click(function(){
  var request = $('.request-input').val();
  var encryptedRequest = CryptoJS.AES.encrypt(request, 'somekey');
  console.log(encryptedRequest.toString())  
  var decryptedRequest = CryptoJS.AES.decrypt(encryptedRequest, 'somekey');
  console.log('Decrypted Request: ' + decryptedRequest.toString());
  handleRequest(encryptedRequest.toString());
});

var handleRequest = function(request){
    $.ajax({
        type: "GET",
        url: host + '/requests?call=' + request,
        success: function(data) {
        var rawJSON = JSON.stringify(data, null, 2);
        editor.setValue(rawJSON);
        },
        dataType: 'json'
   });
}
var port = 3000;
var serverUrl = "127.0.0.1";

var http = require("http");
var path = require("path");
var fs = require("fs");
var express = require("express");
var CryptoJs = require("crypto-js");
var app = express(); 

app.get('/requests', function(req, res) {
    console.log('REQUEST: ' + req);
    var call = req.query.call;
    console.log(call)
    console.log("To send: " + CryptoJs.AES.decrypt(call, 'somekey'));
}); 
我一直遇到的问题是,当我解密它时,它要么没有得到原始URL,反而返回一堆jibberish。这方面的一个例子是

Encryption: U2FsdGVkX1/NRbZkyP60pPu3Cb9IcQ4b9n4zJkExp2LNR3O1EdEpqHLNACnYuatN

Decryption: 68747470733a2f2f6e6577736170692e6f72672f76312f61727469636c6573
或者。。。它只是不返回任何内容,并且显示为空

理想情况下,我想要这样的东西

Encryption: U2FsdGVkX1/NRbZkyP60pPu3Cb9IcQ4b9n4zJkExp2LNR3O1EdEpqHLNACnYuatN

Decryption: https://newsapi.org/v1/articles 

有人能看出我错了什么吗?

这里有一个正在工作的JSFIDEL:

加密结果为Base64字符串,而解密的字符串为十六进制。要获取“消息”,您需要将其转换为Utf8:decryptedRequest.toString(CryptoJS.enc.Utf8)

以下是代码的相关部分:

var request = "testing decryption";
var encryptedRequest = CryptoJS.AES.encrypt(request, 'somekey');
console.log(encryptedRequest)  
var decryptedRequest = CryptoJS.AES.decrypt(encryptedRequest, 'somekey');
var decryptedMessage = decryptedRequest.toString(CryptoJS.enc.Utf8)
console.log('Decrypted Request: ' + decryptedMessage);
下面是一个参考资料链接,详细解释了加密/解密:

更新:更正了一个轻微错误。但还是有同样的问题。代码已被编辑,谢谢!成功了。但是,我现在似乎遇到了性能问题。有时,解密消息需要很长时间,通常,if只返回空行。有时它甚至会出现异常错误:格式错误的UTF-8数据。Received:Received:Received:Received:Received:Received:Received:这是我的节点日志的一个示例。也许你应该将加密请求按原样发送到处理程序中的后端,而不是将其转换为字符串然后发送。