搜尋

首頁  >  問答  >  主體

node.js - AES加密后的文件如何部分解密?

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));
});
怪我咯怪我咯2788 天前680

全部回覆(2)我來回復

  • 黄舟

    黄舟2017-04-17 14:44:51

    問題解決了。
    用這個模組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));
    });

    回覆
    0
  • ringa_lee

    ringa_lee2017-04-17 14:44:51

    為啥你在加密的時候不分段加密呢…這樣解密的時候也就可以分段解密了。

    回覆
    0
  • 取消回覆