2
var path = require('path');
var fs = require('fs');
var crypto = require('crypto');

var algorithm = 'aes-256-ctr';
var password = 'xxxxx';

var dir = '/Users/antkong/some/path';
var file = 'somefile.json';

var clearTextPath = path.join(dir, file);
var cipher = crypto.createCipheriv(algorithm, password);
var readStream = fs.createReadStream(clearTextPath);
var writeStream = fs.createWriteStream(path.join(dir, file + '.encrypted'));
readStream.pipe(cipher).pipe(writeStream);

然后我得到了这个错误:

internal/crypto/cipher.js:139
  this._handle.initiv(cipher, toBuf(key), toBuf(iv));
               ^

TypeError: IV must be a buffer
    at new Cipheriv (internal/crypto/cipher.js:139:16)
    at Object.createCipheriv (crypto.js:98:10)

我的节点版本是 9.11.1

我已经验证源文件存在。

为什么失败了?它在旧版本的节点中工作(早于版本 8.x)

4

2 回答 2

0

看看这个:

var IV = new Buffer(crypto.randomBytes(12)); 

// 这需要为 'aes-256-gcm' 的 12 字节缓冲区

于 2019-11-02T14:30:34.540 回答
0

初始化向量参数未在createCipheriv方法中传递。

var IV = new Buffer(crypto.randomBytes(16)); 
var cipher = crypto.createCipheriv(algorithm, password, IV);
于 2018-05-30T09:43:09.963 回答