当前位置: 首页 > 知识库问答 >
问题:

NodeMCU节点。js加密

堵睿范
2023-03-14

我试图在节点之间建立加密通信。js和NodeMCU。经过一些努力,我能够使用node加密。js并在NodeMCU上解密。反之亦然。mscdex的答复有效。因此,我修改了节点。为了他人的利益而编写js代码。谢谢

NodeMCU代码:

crypto = require('crypto');
cipher = crypto.encrypt("AES-CBC", "0123456789abcdef", "some clear text data",'0000000000000000')
print(crypto.toHex(cipher))
//95f27285ba29aeae1e48cfc6e821b0a15a0dd6c5a1e636e10c5497c460ed057b
print(crypto.toBase64(cipher))
//lfJyhboprq4eSM/G6CGwoVoN1sWh5jbhDFSXxGDtBXs=

节点。js工作代码:

var crypto=require('crypto'); //crypto module is required
algorithm = 'aes-128-cbc'; //define algorithm
password = '0123456789abcdef'; //define key
iv='0000000000000000'; // define vector for cbc.. Must be 16 char length
function encrypt(text,opts) {
  if(typeof opts === 'undefined') opts={}
  // if opts is not defined, set empty list
  var cipher = crypto.createCipheriv(algorithm,password,iv)
  // create cipher
  //if setAutoPadding is undefined or set as true set setAutoPadding as true
  if(opts['setAutoPadding'] || typeof opts['setAutoPadding'] !== 'undefined') {
    cipher.setAutoPadding(opts['setAutoPadding']);
    //if encoding is defined, then set it as encoding else set default hex
    if(opts['encoding'] && typeof opts['encoding'] !== 'undefined') {
      var crypted = cipher.update(text,'utf8',opts['encoding'])
      crypted += cipher.final(opts['encoding']);
      return crypted
    } else {
      var crypted = cipher.update(text,'utf8','hex')
      crypted += cipher.final('hex');
      return crypted;
    }
  }

  function decrypt(text,opts) {
    if(typeof opts === 'undefined') opts={}
    // if opts is not defined, set empty list
    var decipher = crypto.createDecipheriv(algorithm,password,iv)
    // create cipher
    //if setAutoPadding is undefined or set as true set setAutoPadding as true
    if(opts['setAutoPadding'] || typeof opts['setAutoPadding'] !== 'undefined') {
      decipher.setAutoPadding(opts['setAutoPadding']);
    }
    var dec; // define a local variable
    //if encoding is defined, then set it as encoding else set default hex
    if(opts['encoding'] && typeof opts['encoding'] !== 'undefined') {
      dec = decipher.update(text,opts['encoding'],'utf8')
    } else {
      dec = decipher.update(text,'hex','utf8')
    }
    dec += decipher.final('utf8');
    return dec;
  }
  var hw = encrypt("some clear text data",{'encoding':'base64'})
  //encrypt with base64 encoding, padding true
  console.log(hw) // prints base64 encoded encrypted string
  console.log('Node.js-base64: ',decrypt(hw,{'encoding':'base64'}))
  // outputs some clear text data
  hw = encrypt("some clear text data")
  // encrypt default encoding hex, defaule padding true
  console.log(hw) // prints hex encoded encrypted string
  console.log('Node.js-hex: ',decrypt(hw)) // outputs some clear text data
  jw='lfJyhboprq4eSM/G6CGwoVoN1sWh5jbhDFSXxGDtBXs='
  // NodeMCU base64 encoded, padding false encrypted string
  console.log('NodeMCU-base64: ',decrypt(jw, { 'setAutoPadding':false,'encoding':'base64'}));
  // outputs some clear text data
  jw='95f27285ba29aeae1e48cfc6e821b0a15a0dd6c5a1e636e10c5497c460ed057b'
  // nodeMCU, hex encoded, padding false encrypted string
  console.log('NodeMCU-hex: ',decrypt(jw,{'setAutoPadding':false}));
  // outputs some clear text data
  console.log("over")

现在再次进行NodeMCU侧测试:

  cipher=encoder.fromBase64("lfJyhboprq4eSM/G6CGwoYmVZaNMY5xL2kR7V2E1Aho=")
  key="0123456789abcdef"
  print(crypto.decrypt("AES-CBC", key, cipher,'0000000000000000'))
  some clear text data
  cipher=encoder.fromBase64("lfJyhboprq4eSM/G6CGwoVoN1sWh5jbhDFSXxGDtBXs=")
  key="0123456789abcdef"
  print(crypto.decrypt("AES-CBC", key, cipher,'0000000000000000'))
  some clear text data

什么在起作用?

节点。js加密在NodeMCU上被解密,尽管加密的字符串有点不同。

什么不起作用?

NodeMCU的加密字符串没有被node.js.解密,我得到以下错误:

crypto.js:153
var ret = this._handle.final();

错误:错误:0606506D:数字信封例程:EVP_DecryptFinal_ex:DecrypherIV错误(本机)时的错误最终块长度。密码最终(crypto.js:153:26)在对象的decrypt(/home/pi/rampion/nodejs/test2.js:22:19)处。模块中的(/home/pi/rampion/nodejs/test2.js:43:13)_在对象处编译(module.js:413:34)。模块_扩展。。js(module.js:422:10)在模块中。在函数处加载(module.js:357:32)。模块_在函数处加载(module.js:314:12)。单元启动时运行主(module.js:447:10)(node.js:146:18)

该错误是由于mscdex在其回复中强调的原因造成的。

NodeMCU不使用PKCS填充,但节点的加密模块在默认情况下使用/预期使用PKCS填充,因此您需要在调用之前禁用PKCS填充。通过解密进行解密时更新()。设置自动添加(假);呼叫

共有1个答案

傅正阳
2023-03-14

NodeMCU不使用PKCS填充,但是节点的crypto模块在默认情况下使用/期望使用PKCS填充,因此您需要在调用之前禁用PKCS填充。解密时更新()

function decrypt(text){
  var decipher = crypto.createDecipheriv(algorithm,password,iv)
  decipher.setAutoPadding(false);
  var dec = decipher.update(text,'hex','utf8')
  dec += decipher.final('utf8');
  return dec;
}

function decryptB(text){
  var decipher = crypto.createDecipheriv(algorithm,password,iv)
  decipher.setAutoPadding(false);
  var dec = decipher.update(text,'base64','utf8')
  dec += decipher.final('utf8');
  return dec;
}

更改后,您将能够解密NodeMCU代码中已注释的十六进制和base64值。

 类似资料:
  • 我写这个问题的答案是因为我挣扎了很多(可能是因为缺乏经验),迷失在许多不同的加密/解密节点或Python的方法中。 我想也许我的案子能在将来帮助人们。 我需要做的是: 从表单中获取数据,使用加密(node js)对其进行加密。 我选择使用AES加密。 我是这样开始的(我不会经历我尝试过的一切): > 我遵循了本页末尾的例子 在我的案例中给出: 这可能是javascript和coffeescript

  • 我试图实现我的RestAPI的JWE。我遇到了以下实现JWE的节点库。然而,库缺乏关于如何使用JSON Web密钥(JWK)(JSON对象)的留档,这有助于密钥管理模式。JWE留档内容如下: 确定要使用的内容加密密钥值的方法。用于确定CEK值的每个算法使用特定的密钥管理模式。本规范采用的密钥管理模式包括密钥加密、密钥包装、直接密钥协议、密钥协议与密钥包装以及直接加密。 所以我想知道我应该如何将JW

  • 本文向大家介绍js实现点击添加一个input节点,包括了js实现点击添加一个input节点的使用技巧和注意事项,需要的朋友参考一下 代码过于简洁,就不多说废话了,直接奉上:

  • 我试图在文件如下示例:如何为Firebase构建云函数,以便从多个文件部署多个函数?。 特别是,我有一个: 现在在index.ts我可以导入并附加一个用户身份验证侦听器,以便在创建新用户时触发,即: 然而,据我所知,只有实际上按预期工作,并且导出。userEvents似乎不会在创建新用户时触发。 ===================================================

  • 问题内容: 以下代码使我在节点js中出现异常:“需要删除或更新” 由于我指定了更新操作,因此无法解决问题。 问题答案: 节点驱动程序中的语法与外壳程序中的语法不同,这是您使用的语法。 有一个单独的功能

  • 我有两个表有下面的模式,我想从用户表中获取用户名和密码,从信息表中获取全名。 var infoSchema=mongoose.Schema({khatam_id:String,user_id:String,fullname:String,}); var usersSchema=mongoose.Schema({user\u id:String,username:String,password:St