Node.js cipher.js TypeError:IV必须是缓冲区
然后我得到了这个错误:Node.js cipher.js TypeError:IV必须是缓冲区,node.js,node-crypto,Node.js,Node Crypto,然后我得到了这个错误: 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(
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);
我的节点版本是9.11.1
我已验证源文件是否存在
为什么失败了?它在旧版本的节点(早于8.x版)中工作。初始化向量参数未传递到方法中 看看这个:
var IV = new Buffer(crypto.randomBytes(16));
var cipher = crypto.createCipheriv(algorithm, password, IV);
//“aes-256-gcm”需要一个12字节的缓冲区。谢谢您的回答!现在我得到一个不同的错误:
无效的密钥长度
。正确的长度应该是多少?这里的聚会可能会迟到,但对于那些遇到这种情况的人来说。无效的密钥长度是因为“密码”太短。aes-256需要32字节key@Slaphead我曾经使用过crypto.randomBytes(32)
,但现在它显示无效的IV长度
。有什么建议吗?
var IV = new Buffer(crypto.randomBytes(16));
var cipher = crypto.createCipheriv(algorithm, password, IV);
var IV = new Buffer(crypto.randomBytes(12));