代码之家  ›  专栏  ›  技术社区  ›  Anthony Kong

cipher.js typeerror:iv必须是缓冲区

  •  2
  • Anthony Kong  · 技术社区  · 7 年前
    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

    我已经确认源文件存在。

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

    1 回复  |  直到 7 年前
        1
  •  0
  •   Ashok JayaPrakash    7 年前

    初始化向量参数未传入 createCipheriv 方法。

    var IV = new Buffer(crypto.randomBytes(16)); 
    var cipher = crypto.createCipheriv(algorithm, password, IV);
    
    推荐文章