【发布时间】:2014-08-07 00:42:42
【问题描述】:
我在微控制器和 nodejs tcp 服务器之间来回通信。微控制器形成一个带有传感器数据的 json 字符串。然后微控制器将 json 字符串发送到 WiFi 模块。然后 WiFi 模块使用 AES256 以 32 个字符的十六进制字符作为密钥对数据进行加密,然后将加密的数据发送到 nodejs tcp 服务器。
nodejs TCP 服务器正在使用 googlecode Crypto-JS 的 Crypto-JS 模块形式。
出于测试目的,我想将加密数据和解密数据输出到控制台。但是我不确定如何做到这一点。我试图输出数据,但我收到空白数据。例如,控制台应该是这样的: 192.168.1.14:30001> some-json-string 除了我收到 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(); ^ 类型错误:错误: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 字符串之前,我将删除“>”。
【问题讨论】:
-
node.js 内置了
crypto模块,为什么还要使用第三方库? -
@mscdex,我尝试使用内置的加密模块。但是,我一直收到错误消息。我目前处于 AFK 状态,因此无法重现该错误。
-
好吧,如果你能重现内置的
crypto错误,请在此处发布(连同使用的代码),以便我们查看问题所在。 -
decrypt()在哪里定义?data是什么样的?是缓冲区还是别的什么? -
我再次编辑了我的代码。我错误地遗漏了一些重要的代码。对象数据(我认为是一个 js 对象)应该输出: {"resTemp":"82.19","roomTemp":98,"ph":58,"ec":700}> ">" 是为了数据流控制,将在我处理 json 字符串之前被删除。
标签: json node.js encryption aes