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)
答案 0 :(得分:0)
初始化向量参数未在createCipheriv方法中传递。
var IV = new Buffer(crypto.randomBytes(16));
var cipher = crypto.createCipheriv(algorithm, password, IV);
答案 1 :(得分:0)
看看这个:
var IV = new Buffer(crypto.randomBytes(12));
//这需要是一个12字节的缓冲区,用于'aes-256-gcm'