recherche

Maison  >  Questions et réponses  >  le corps du texte

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));
});
怪我咯怪我咯2796 Il y a quelques jours687

répondre à tous(2)je répondrai

  • 黄舟

    黄舟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));
    });

    répondre
    0
  • ringa_lee

    ringa_lee2017-04-17 14:44:51

    为啥你在加密的时候不分段加密呢……这样解密的时候也就可以分段解密了。

    répondre
    0
  • Annulerrépondre