cipher.js TypeError: IV must be a buffer
cipher.js TypeError: IV must be a buffer
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)
createCipheriv 方法中未传递初始化向量参数。
var IV = new Buffer(crypto.randomBytes(16));
var cipher = crypto.createCipheriv(algorithm, password, IV);
看看这个:
var IV = new Buffer(crypto.randomBytes(12));
// 这需要是 'aes-256-gcm'
的 12 字节缓冲区
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)
createCipheriv 方法中未传递初始化向量参数。
var IV = new Buffer(crypto.randomBytes(16));
var cipher = crypto.createCipheriv(algorithm, password, IV);
看看这个:
var IV = new Buffer(crypto.randomBytes(12));
// 这需要是 'aes-256-gcm'
的 12 字节缓冲区