AES加密后的文件如何部分解密?
如果整个文件加解密我知道怎么做。如下所示。但是现在需求是只解密部分的文件。比如1G的文件只解密最后1M。请问怎么做?
加密算法现在用的是AES-256-CTR
var crypto = require('crypto');
var fs = require('fs-extra');
var encrypt = crypto.createCipher('AES-256-CTR', 'segmentfault');
var decrypt = crypto.createDecipher('AES-256-CTR', 'segmentfault');
var encryptName = 'encrypt.txt';
var decryptName = 'decrypt.txt';
fs.createReadStream('decrypt.js').pipe(encrypt).pipe(fs.createWriteStream(encryptName)).on('finish', () => {
fs.createReadStream(encryptName).pipe(decrypt).pipe(fs.createWriteStream(decryptName));
});
黄舟2017-04-17 14:44:51
Problem solved.
Use this module https://npm.taobao.org/packag...
var crypto = require('crypto');
var fs = require('fs-extra');
var encryptName = 'encrypt.txt';
var decryptName = 'decrypt.txt';
var cryptoAesCtr = require("crypto-aes-ctr");
var key = new Buffer("8fbf35890fc3e0d5bc615cb091f16e8d40cfe3d4223cd68b11e2e7204d890210", "hex");
var iv = new Buffer("4395a3ded2f0040835d437cc9fa7a7dc", "hex");
fs.createReadStream('decrypt.js').pipe(encrypt).pipe(fs.createWriteStream(encryptName)).on('finish', () => {
//假设跳过3个AES块,每个AES块是16字节
var aesBlockSize = 16;
var counter = 3;
fs.createReadStream(encryptName, {start: aesBlockSize * counter}).pipe(cryptoAesCtr.createStream(key, iv, counter)).pipe(fs.createWriteStream(decryptName));
});
ringa_lee2017-04-17 14:44:51
Why don’t you encrypt in segments when encrypting... Then you can decrypt in segments when decrypting?