【问题标题】:Error in crypto deciphering密码破译错误
【发布时间】:2023-03-05 07:55:01
【问题描述】:

为什么下面的代码会在加密中抛出 DecipherFinal 错误 -

  var crypto = require('crypto');
  c=new Date;
  x= (c.getTime()+"."+c.getMilliseconds()).toString()+".uIn";
  key = 'sevsolut'
        , plaintext = x
        , cipher = crypto.createCipher('aes-256-cbc', key)
        , decipher = crypto.createDecipher('aes-256-cbc', key);
  cipher.update(plaintext, 'utf8', 'base64');
  var encryptedPassword = cipher.final('base64')

  decipher.update(encryptedPassword, 'base64', 'utf8');
  var decryptedPassword = decipher.final('utf8');

  console.log('encrypted :', encryptedPassword);
  console.log('decrypted :', decryptedPassword);

【问题讨论】:

  • c=new Date 应该是c = new Date()
  • 为什么要加密密码而不是散列密码?
  • @pfried 括号在 JS 中实例化对象时是可选的,只要您不需要传递参数即可。
  • @robertklep 每天都有新东西 :D 但我认为为了更好地阅读它会有所帮助
  • @Mchl 它的原型代码..它不是用于加密密码...

标签: javascript node.js cryptography node-modules node-crypto


【解决方案1】:

您需要从更新中获取输出:

var crypto = require('crypto');
c=new Date();
x= (c.getTime()+"."+c.getMilliseconds()).toString()+".uIn";
key = "sevsolut"
      , plaintext = x
      , cipher = crypto.createCipher('aes-256-cbc', key)
      , decipher = crypto.createDecipher('aes-256-cbc', key);
var encryptedPassword = cipher.update(plaintext, 'utf8', 'base64');
encryptedPassword += cipher.final('base64')

var decryptedPassword = decipher.update(encryptedPassword, 'base64', 'utf8');
decryptedPassword += decipher.final('utf8');

console.log('encrypted :', encryptedPassword);
console.log('decrypted :', decryptedPassword);

【讨论】:

  • @chris..它工作得很好:) ..但是想指出一件事..有问题的代码似乎非常适合特定长度的字符串(尝试使用一个小的简单字符串)..只有当它超过一定长度时,我想我们必须采用添加更新步骤..你能解释一下吗?
  • 不确定你的意思。更新返回加密内容,并且可以在流式传输时使用新数据多次调用 - final 返回任何剩余的加密内容。
  • 我的意思是说使用 x= "simple" 作为明文输入..然后问题中的代码完美运行..但是如果 x= 一些大字符串..那么问题中的代码失败并且我们必须按照你的例子进行......
  • 加密是逐块完成的。如果您没有为块添加足够的内容,更新函数将不会返回任何内容。
猜你喜欢
  • 1970-01-01
  • 2023-03-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-22
  • 2017-04-02
相关资源
最近更新 更多