3

I am communicated back and forth between a micro-controller and a nodejs tcp server. The micro-controller forms a json string with sensor data. The micro-controller then sends the json string to a WiFi module. The WiFi module then encrypts the data using AES256 with 32 character Hexadecimal characters as the key before sending the encrypted data to the nodejs tcp server.

The nodejs TCP server is using the Crypto-JS module form of the googlecode Crypto-JS.

For testing purposes I would like to output the encrypted data and decrypted data to the console. However I am unsure how do accomplish this. I attempted to output the data, but I am receiving blank data. For example, the console should read something like: 192.168.1.14:30001> some-json-string Except I am receiving 192.168.1.14:30001>

Old Code:

 // I removed the old code to shrink this post and to remove any confusion. 

EDIT
I am now using the the built in crypto module supplied by NodeJS. The error I am receiving is:

crypto.js:292 var ret = this._binding.final(); ^ TypeError: error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt
at Decipher.Cipher.final (crypto.js:292:27)
at decrypt (C:\Users\joes\Desktop\encrypt\tcp.js:18:24)
at Socket. (C:\Users\joes\Desktop\encrypt\tcp.js:44:23)
at Socket.emit (events.js:95:17)
at Socket. (_stream_readable.js:748:14)
at Socket.emit (events.js:92:17)
at emitReadable_ (_stream_readable.js:410:10)
at emitReadable (_stream_readable.js:406:5)
at readableAddChunk (_stream_readable.js:168:9)
at Socket.Readable.push (_stream_readable.js:130:10)

Code:

// 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"); 

data should a buffered object and contain a json string, for an example:{"resTemp":"82.19","roomTemp":98,"ph":58,"ec":700}>
The ">" is there intentionally for data flow control between the micro-controller and the wifi module. I will remove the '>' before I process the json string.

7
  • 2
    Why use a third party library when node.js has a built-in crypto module? Commented Aug 7, 2014 at 0:53
  • @mscdex, I tried to use the built in crypto modulr. However, I kept receiving an error. I am AFK at the moment so I can't reproduce the error. Commented Aug 7, 2014 at 1:07
  • 1
    Well, if you can reproduce the built-in crypto error, post it here (along with the code used) so we can see what's wrong. Commented Aug 7, 2014 at 1:17
  • Where is decrypt() defined? What does data look like? Is it a Buffer or something else ? Commented Aug 7, 2014 at 2:30
  • I edited my code again. I by-mistakenly left out some important code. The object data(I think is a js object) should output: {"resTemp":"82.19","roomTemp":98,"ph":58,"ec":700}> The ">" is there intentionally for data flow control and will be removed before I process the json string. Commented Aug 7, 2014 at 2:41

1 Answer 1

5

The code using the built-in crypto module is almost correct. Notably there was a typo in encrypt() and the key needs to be a Buffer. Here's what I used:

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' 
Sign up to request clarification or add additional context in comments.

3 Comments

That code works as expected. I attempted to alter your example to decrypt the data stream that is being sent to my nodejs TCP server without success. I am receiving an error. I edited my original post with the new code and the new error message.
To troubleshoot my issue I deactivated the wifi modules encryption. I then send unencrypted json string to the tcp server and output the clear text and the length. The length of the json string is a constant 52. I also encrypted the json string and output the length which is a constant 128. I then decrypted the encrypted json string without issues. I'm thinking there is an issue with the wifi modules encryption. I will contact the vender and find out if there is a bug or if I'm doing something incorrectly. When i send encrypted data the length is always inconsistent.
I don't think the wifi module is defective. I think the issue that I am running into is how to handle flow control and I think the incoming data is getting converted to ASCII characters instead of staying in hexadecimal format. What else can I do to debug this issue?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.