尝试使用crypto-js和nodejs进行解密

我在一个微控制器和一个nodejs tcp服务器之间来回传递。 微控制器与传感器数据形成一个json串。 微控制器然后将jsonstring发送到WiFi模块。 然后WiFi模块使用32位hex字符的AES256作为密钥encryption数据,然后将encryption数据发送到nodejs tcp服务器。

nodejs TCP服务器正在使用googlecode Crypto-JS的Crypto-JS模块forms。

出于testing目的,我想输出encryption的数据和解密的数据到控制台。 但是我不确定如何做到这一点。 我试图输出数据,但我收到空白的数据。 例如,控制台应该读取类似于: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提供的内置encryption模块。 我收到的错误是:

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)
在sockets。 (C:\用户\乔斯\桌面\encryption\ tcp.js:44:23)
在Socket.emit(events.js:95:17)
在sockets。 (_stream_readable.js:748:14)
在Socket.emit(events.js:92:17)
在emitReadable_(_stream_readable.js:410:10)
在emitReadable(_stream_readable.js:406:5)
at 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"); 

数据应该是一个缓冲对象,并包含一个JSONstring,例如:{“resTemp”:“82.19”,“roomTemp”:98,“ph”:58,“ec”:700}>
“>”有意用于微控制器和wifi模块之间的数据stream量控制。 我将在处理jsonstring之前删除'>'。

使用内置crypto模块的代码几乎是正确的。 值得注意的是在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'