通过管道使用node.js解密AES256

时间:2016-06-30 22:27:13

标签: node.js encryption

我正在尝试使用node.js的crypt模块解密一些文件,这些文件是由另一个在非标准库中使用openssl库的程序加密的。非标准我的意思是盐的轮数和位置不同于openssl使用的默认值。因此,我首先提取盐,然后在尝试进行实际解密之前在结果描述上创建一个ReadStream。

我有两个例程。第一个使用decrypt.updatedecrypt.final来执行解密。我能够以这种方式解密文件。第二个使用pipe来执行解密。当我尝试使用它时,我收到此错误:

我尝试运行代码时遇到的错误是:

  

数字包络例程:EVP_DecryptFinal_ex:错误的最终块长度

工作和失败功能如下。引用的“binary_concat”函数相当于二进制字符串的a+b - 在我发现a+b无法正常工作之前,我花了几个小时的调试时间。

function do_decrypt_works(infile,password,salt) {
    var outfile = fs.createWriteStream("/tmp/test.out")
    var text = fs.readFileSync(infile_filename).slice(8) // hack since we aren't using infile in this case
    var rounds = 28
    data00 = binary_concat(password,salt,"")

    var hash1 = do_rounds(data00)
    var hash1a = binary_concat(hash1,password,salt)
    var hash2 = do_rounds(hash1a,password,salt)
    var hash2a = binary_concat(hash2,password,salt)
    var hash3 = do_rounds(hash2a,password,salt)

    var key = binary_concat(hash1,hash2,"")
    var iv = hash3

    var decrypt = crypto.createDecipheriv('aes-256-cbc', key, iv)

    var content = decrypt.update(text, "binary", "binary");
    content += decrypt.final("binary");
}

function do_decrypt_fails(infile,password,salt) {
    var outfile = fs.createWriteStream("/tmp/test.out")
    var rounds = 28
    data00 = binary_concat(password,salt,"")

    var hash1 = do_rounds(data00)
    var hash1a = binary_concat(hash1,password,salt)
    var hash2 = do_rounds(hash1a,password,salt)
    var hash2a = binary_concat(hash2,password,salt)
    var hash3 = do_rounds(hash2a,password,salt)

    var key = binary_concat(hash1,hash2,"")
    var iv = hash3

    var decrypt = crypto.createDecipheriv('aes-256-cbc', key, iv)
    infile.pipe(decrypt).pipe(outfile)
}

根据documentationcreateDeciphercreateDecipheriv都会返回一个类Decipher的实例,该实例可以与上述任何一种技术一起使用。

来源:

First

Second

Third

1 个答案:

答案 0 :(得分:0)

是的,你正在循环(i = 1; i < rounds; i++)但是接着只取前三个摘要(结果的前48个字节,结果长于48个字节,18个循环通过循环一遍又一遍地连接到结果)。

无论如何,通过我对算法的阅读,你在错误的地方循环。它看起来像你显示的第一个函数可以正常工作,只需要将迭代计数添加到散列中,如下所示:

function md5(data, count) {
  var hash = crypto.createHash('md5');
  hash.update(data);
  for i = 0; i < count; i++ // PSEUDO-CODE !!!
      hash.update(hash)     // PSEUDO-CODE re-hash the hash
  return new Buffer(hash.digest('hex'), 'hex');
}