Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/36.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
Json 正在尝试使用crypto js和nodejs解密_Json_Node.js_Encryption_Aes - Fatal编程技术网

Json 正在尝试使用crypto js和nodejs解密

Json 正在尝试使用crypto js和nodejs解密,json,node.js,encryption,aes,Json,Node.js,Encryption,Aes,我在微控制器和nodejs tcp服务器之间来回通信。微控制器与传感器数据形成json字符串。然后,微控制器将json字符串发送到WiFi模块。然后,WiFi模块使用AES256加密数据,将32个字符的十六进制字符作为密钥,然后再将加密数据发送到nodejs tcp服务器 nodejs TCP服务器使用Google Code Crypto JS的Crypto JS模块形式 出于测试目的,我想将加密数据和解密数据输出到控制台。然而,我不确定如何做到这一点。我试图输出数据,但收到的是空白数据。例如,

我在微控制器和nodejs tcp服务器之间来回通信。微控制器与传感器数据形成json字符串。然后,微控制器将json字符串发送到WiFi模块。然后,WiFi模块使用AES256加密数据,将32个字符的十六进制字符作为密钥,然后再将加密数据发送到nodejs tcp服务器

nodejs TCP服务器使用Google Code Crypto JS的Crypto JS模块形式

出于测试目的,我想将加密数据和解密数据输出到控制台。然而,我不确定如何做到这一点。我试图输出数据,但收到的是空白数据。例如,控制台的内容应如下所示: 192.168.1.14:30001>一些json字符串,除了我收到192.168.1.14:30001>

旧代码:

  // I removed the old code to shrink this post and to remove any confusion.
编辑
我现在使用的是NodeJS提供的内置加密模块。我收到的错误是:

crypto.js:292 var ret=this._binding.final(); ^TypeError:错误:06065064:数字信封例程:EVP_DecryptFinal_ex:错误解密
在Decipher.Cipher.final(crypto.js:292:27)上
解密时(C:\Users\joes\Desktop\encrypt\tcp.js:18:24)
在插座上。(C:\Users\joes\Desktop\encrypt\tcp.js:44:23)
在Socket.emit(events.js:95:17)
在插座上。(_stream_readable.js:748:14)
在Socket.emit(events.js:92:17)
在emitReadable(_stream_readable.js:410:10)
在emitReadable(_stream_readable.js:406:5)
在readableAddChunk(_stream_readable.js:168:9)
在Socket.Readable.push(_stream_Readable.js:130:10)

代码:

// Load the TCP Library
net = require('net');

// Load the Crypto Module
var crypto = require("crypto");

function encrypt(key, data) {
    var cipher = crypto.createCipher('aes256', key);
    var crypted = cipher.update(data, 'utf-8', 'hex');
    crypted += cipher.final('hex');

    return crypted;
}

function decrypt(key, data) {
    var decipher = crypto.createDecipher('aes256', key);
    var decrypted = decipher.update(data, 'hex', 'utf-8');
    decrypted += decipher.final('utf-8');

    return decrypted;
}

// Keep track of the chat clients
var clients = [];

// Start a TCP Server
net.createServer(function (socket) {

// Identify this client
socket.name = socket.remoteAddress + ":" + socket.remotePort

// Put this new client in the list
clients.push(socket);

// Send a nice welcome message and announce
socket.write("Welcome " + socket.name + "\n");
broadcast(socket.name + " joined the chat\n", socket);

// Handle incoming messages from clients.
socket.on('data', function (data) {
  var key = new Buffer('85CE6CCF67FBBAA8BB13479C3A6E084D', 'hex');

  // Attempt to decrypt data with the above key
  var decryptedText = decrypt(key, data);
  //console.log("Decrypted Text: " + decrypt(key, encrypt(key, '{"resTemp":"82.19","roomTemp":98,"ph":58,"ec":700}>')));

  broadcast(socket.name + "> " + decryptedText, socket);
  //console.log(data);
});

// Remove the client from the list when it leaves
socket.on('end', function () {
  clients.splice(clients.indexOf(socket), 1);
  broadcast(socket.name + " left the chat.\n");
});
// Send a message to all clients
function broadcast(message, sender) {
  clients.forEach(function (client) {
  // Don't want to send it to sender
  if (client === sender) return;
  client.write(message);
});
// Log it to the server output too
process.stdout.write(message)
}

}).listen(5000);

// Put a friendly message on the terminal of the server.
console.log("Chat server running at port 5000\n");
数据应该是一个缓冲对象并包含一个json字符串,例如:{“resTemp”:“82.19”,“roomTemp”:98,“ph”:58,“ec”:700}>

“>”用于微控制器和wifi模块之间的数据流控制。在处理json字符串之前,我将删除“>”。

使用内置
加密模块的代码几乎是正确的。值得注意的是,
encrypt()
中有一个输入错误,密钥需要是一个缓冲区。以下是我使用的:

var crypto = require('crypto');

function encrypt(key, data) {
    var cipher = crypto.createCipher('aes256', key);
    var crypted = cipher.update(data, 'utf-8', 'hex');
    crypted += cipher.final('hex');

    return crypted;
}

function decrypt(key, data) {
    var decipher = crypto.createDecipher('aes256', key);
    var decrypted = decipher.update(data, 'hex', 'utf-8');
    decrypted += decipher.final('utf-8');

    return decrypted;
}

var key = new Buffer('85CE6CCF67FBBAA8BB13479C3A6E084D', 'hex');

decrypt(key, encrypt(key, 'hello world'));

// outputs: 'hello world'

当node.js有内置模块时,为什么要使用第三方库?@mscdex,我尝试使用内置加密模块。然而,我一直收到一个错误。我现在是AFK,所以我不能重现错误。好吧,如果你能重现内置的
crypto
错误,把它贴在这里(连同使用的代码一起),这样我们就可以看到错误。哪里定义了
decrypt()
?数据是什么样子的?是缓冲区还是别的什么?我又编辑了我的代码。我错误地遗漏了一些重要的代码。对象数据(我认为是一个js对象)应该输出:{“resTemp”:“82.19”,“roomTemp”:98,“ph”:58,“ec”:700}>这个“>”是专门用于数据流控制的,在我处理json字符串之前会被删除。代码工作正常。我试图更改您的示例以解密发送到我的nodejs TCP服务器的数据流,但未成功。我收到一个错误。我用新代码和新错误消息编辑了我的原始帖子。为了解决我的问题,我停用了wifi模块加密。然后我将未加密的json字符串发送到tcp服务器,并输出明文和长度。json字符串的长度是一个常量。我还对json字符串进行了加密,并输出了长度为128的常量。然后我解密了加密的json字符串,没有任何问题。我认为wifi模块加密有问题。我会联系销售商,看看是否有错误或者我做的不对。当我发送加密数据时,长度总是不一致。我不认为wifi模块有缺陷。我认为我遇到的问题是如何处理流控制,我认为传入的数据正在转换为ASCII字符,而不是保持十六进制格式。我还可以做什么来调试此问题?