"use strict"; var fs = require(‘fs’); var crypto = require(‘crypto’); var Readable = require(‘stream’).Readable; module.exports = { index: function (router) { var rstream = fs.createReadStream( __dirname + ‘/…/public/video/video02.mp4’); rstream.setEncoding(‘utf8’); var rs = new Readable; var cipher = crypto.createCipheriv('des-cbc’, '01234567’, ‘12345678’); cipher.setAutoPadding(true); rstream.on('data’, function(chunk){ var ciph = cipher.update(chunk, 'utf8’, ‘hex’); ciph += cipher.final(‘hex’); var buffer = new Buffer(ciph); console.log(buffer); rs.push(buffer); }); rstream.on('end’, function(){ rs.push(null); }); rs.pipe(process.stdout); return; router.renderResponse(); } };
为什么这样加密会报错,只执行了一次加密,程序就挂了
把 cipher.update(chunk, 'utf8’, ‘hex’); 换成cipher.update(chunk, 'binary’, ‘hex’); 试一下。 另外,建议你调整一下代码结构,把主函数, 文件读取,加密, 分成不同的function,也容易定位错误。
Crypto参考文章:http://blog.fens.me/nodejs-crypto/