下载时加密zip文件夹,然后使用Node.js解密吗?

问题描述 投票:0回答:1

因此,我有一个应用程序可以下载一个包含大量音频文件的zip文件夹。我想在下载时加密该zip文件,然后能够在应用程序的其他位置解密该zip文件,以便可以将其提取。我具有使用Node's Crypto软件包和CTR模式的AES-256进行加密和解密的基本逻辑,但在过程结束时似乎无法获得可用的zip文件。

这是我用于下载zip文件的http.get请求

http.get(fileURI).on('response', function(res) {
    res.on('data', function(chunk) {
        let encryptedChunk = encrypt(chunk);
        writeStream.write(encryptedChunk);
    }).on('end', function() {
        writeStream.end();
    })
})

因此,获取请求会加密下载的每个数据块,并将其发送到对特定文件打开的writeStream,然后在zip下载完成时结束writestream。从某种意义上讲,我无法打开zip文件(Windows无法打开文件夹,弹出错误消息),这似乎已正确加密。

我的加密函数非常基本,在http请求中调用的函数看起来像这样:

function encrypt(data) {
    try {
        let cipher = crypto.createCipher('aes-256-ctr', key); // key is a random string
        let encrypted = Buffer.concat([cipher.update(new Buffer(data, "utf8")), cipher.final()]);
        return encrypted;
    } catch (exception) {
        console.error(exception);
    }
}

然后,我有一个函数创建一个readStream来尝试解密此文件,它看起来像这样:

function decryptzip() {
    // assume there are a bunch of necessary filepaths up here

    let readStream = fs.createReadStream(downloadedZipFilePath);
    let writeStream = fs.createWriteStream(newDecryptedZipFilePath);
    readStream.on('data', (chunk) => {
        let decryptedChunk = decrypt(chunk);
        writeStream.write(decryptedChunk); // the goal of this write stream is to have a usable zip file once it closes
    }).on('finish', () => {
        writeStream.end();
    })
}

然后我的实际解密函数如下所示:

function decrypt(data) {
    try {
        let decipher = crypto.createDecipher('aes-256-ctr', key); // same key used in encrypt
        let decrypted = Buffer.concat([decipher.update(data), decipher.final()]);
        return decrypted;
    } catch (exception) {
        console.error(exception);
    }
}

这两个加密和解密功能都可以使用纯文本查找,但是当我尝试查看里面的内容时,“解密的” zip文件仍然无法使用,并给我“ Windows无法打开文件夹错误”。我真的不知道我在做些什么,所以任何帮助将不胜感激,我很失落。

因此,我有一个应用程序可以下载一个包含大量音频文件的zip文件夹。我想对下载的zip文件进行加密,然后能够在...

javascript node.js encryption cryptography node-crypto
1个回答
0
投票

节点加密API的级别仍然足够低,以至于很多地方可能出错。例如,您可能想将HMAC添加到加密数据中以确保其完整性。

© www.soinside.com 2019 - 2024. All rights reserved.