尝试使用crypto-js和nodej进行解密 [英] attempting to decrypt using crypto-js and nodejs

查看:573
本文介绍了尝试使用crypto-js和nodej进行解密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在微控制器和nodejs tcp服务器之间来回传递。微控制器形成具有传感器数据的json字符串。然后,微控制器将json字符串发送到WiFi模块。然后,WiFi模块在将加密数据发送到nodejs tcp服务器之前,使用AES256和32位十六进制字符加密数据。



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



为了测试目的,我想将加密数据和解密数据输出到控制台。但是我不确定如何做到这一点。我尝试输出数据,但是我收到空白数据。例如,控制台应该读取以下内容:
192.168.1.14:30001> some-json-string除非我收到192.168.1.14:30001>



旧代码:

  //我删除了旧的代码来缩小这个帖子并消除任何混乱。 

编辑

我正在使用由NodeJS提供的内置加密模块。我收到的错误是:


crypto.js:292 var ret = this._binding.final();
^ TypeError:error:06065064:数字信封例程:EVP_DecryptFinal_ex:坏解密

在Decipher.Cipher.final(crypto.js:292:27)

在解密(C:\Users\joes\Desktop\encrypt\tcp.js:18:24)

在Socket。 (C:\Users\joes\Desktop\encrypt\tcp.js:44:23)

在Socket.emit(events.js:95:17)

在Socket。 (_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)


代码:

  //加载TCP库
net = require('net');

//加载加密模块
var crypto = require(crypto);

函数加密(key,data){
var cipher = crypto.createCipher('aes256',key);
var crypted = cipher.update(data,'utf-8','hex');
crypted + = cipher.final('hex');

return crypted;
}

函数解密(密钥,数据){
var decipher = crypto.createDecipher('aes256',key);
var decryptpted = decipher.update(data,'hex','utf-8');
decrypted + = decipher.final('utf-8');

返回解密;
}

//跟踪聊天客户端
var clients = [];

//启动TCP服务器
net.createServer(function(socket){

//识别此客户端
socket.name =套接字。 remoteAddress +:+ socket.remotePort

//将这个新客户端放在列表中
clients.push(socket);

//发送一个好的欢迎消息并宣布
socket.write(Welcome+ socket.name +\\\
);
broadcast(socket.name +加入chat\\\
,socket);

//处理来自客户端的传入消息
socket.on('data',function(data){
var key = new Buffer('85CE6CCF67FBBAA8BB13479C3A6E084D','hex') ;

//尝试使用上述密钥解密数据
var decryptptedText = decrypt(key,data);
//console.log(\"解密文本:+ decrypt (key,encrypt(key,'{resTemp:82.19,roomTemp:98,ph:58,ec:700}>')));

广播(socket.name +>+ decryptptedText,socket);
//console.log(data);
});

//将客户端从列表中n它留下
socket.on('end',function(){
clients.splice(clients.indexOf(socket),1);
broadcast(socket.name +left the chat.\\\
);
});
//向所有客户端发送消息
函数广播(消息,发件人){
clients.forEach(function(client){
//不想发送它发送者
如果(客户端===发件人)返回;
client.write(message);
});
//将其记录到服务器输出
process.stdout.write(message)
}

})。listen(5000);

//在服务器的终端上放一个友好的消息。
console.log(在5000端口运行的聊天服务器);

数据应该是一个缓冲对象,并包含一个json字符串,例如:{resTemp: 82.19,roomTemp:98,ph:58,ec:700}>

>有意用于微控制器和WiFi模块之间的数据流控制。我将在处理json字符串之前删除'>'。

解决方案

使用内置 crypto 模块几乎是正确的。值得注意的是,在 encrypt()中有一个错字,密钥需要是一个缓冲区。这是我使用的:

  var crypto = require('crypto'); 

函数加密(key,data){
var cipher = crypto.createCipher('aes256',key);
var crypted = cipher.update(data,'utf-8','hex');
crypted + = cipher.final('hex');

return crypted;
}

函数解密(密钥,数据){
var decipher = crypto.createDecipher('aes256',key);
var decryptpted = decipher.update(data,'hex','utf-8');
decrypted + = decipher.final('utf-8');

返回解密;
}

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

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

//输出:'hello world'


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.

解决方案

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'

这篇关于尝试使用crypto-js和nodej进行解密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆