Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 下载时加密zip文件夹,然后使用Node.js解密?_Javascript_Node.js_Encryption_Cryptography_Node Crypto - Fatal编程技术网

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

Javascript 下载时加密zip文件夹,然后使用Node.js解密?,javascript,node.js,encryption,cryptography,node-crypto,Javascript,Node.js,Encryption,Cryptography,Node Crypto,所以我有一个应用程序可以下载一个zip文件夹,里面有一堆音频文件。我想在下载时加密该zip文件,然后能够在应用程序中的某个其他点解密该zip文件,以便将其提取出来。我有使用Node的加密包和AES-256以及CTR模式进行加密和解密的基本逻辑,但我似乎无法在过程结束时获得可用的zip文件 这是我用来下载zip文件的http.get请求 http.get(fileURI).on('response', function(res) { res.on('data', function(chun

所以我有一个应用程序可以下载一个zip文件夹,里面有一堆音频文件。我想在下载时加密该zip文件,然后能够在应用程序中的某个其他点解密该zip文件,以便将其提取出来。我有使用Node的加密包和AES-256以及CTR模式进行加密和解密的基本逻辑,但我似乎无法在过程结束时获得可用的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();
    })
})
因此,get请求在下载数据块时对其进行加密,并将其发送到对特定文件打开的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);
    }
}
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);
    }
}
然后我有一个函数,它创建一个readStream,试图解密这个文件,它如下所示:

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);
    }
}
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);
    }
}
然后我的实际解密函数如下所示:

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);
    }
}
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无法打开文件夹错误”。我真的不知道在这方面我在做什么,所以任何帮助都会很感激,我很迷茫,哈哈。

节点加密API的级别仍然很低,可能会出现很多错误。例如,您可能希望向加密数据添加HMAC以确保其完整性

我的建议是使用libnaude或类似的Javascript绑定,这样您就可以获得更高级的API。我能找到的本地绑定没有给人留下最成熟的印象,也没有使用过时的libnaid版本


因此,我的建议是同意,因为它已经接受了专业审计。API也非常简单。

这里的问题是,您要获取文件的块,分别加密它们,然后将它们写入磁盘。最终得到的是一个文件的一堆不同的加密部分,而不是一个加密文件。您可能希望在此处利用节点的流功能:

consthttp=require('http');
const{createWriteStream,createReadStream}=require('fs');
const{createCipheriv,createDecipheriv,randomBytes}=require('crypto');
const{pipeline}=require('stream');
const{promisify}=require('util');
const pipelineAsync=promisify(管道);
const downloadAndEncryptFile=async(uri,路径)=>{
const response=等待新承诺((解决、拒绝)=>{
http.get(uri).on('response',resolve.).on('error',reject);
});
const secret=randomBytes(32);
常数iv=随机字节(16);
等待管道同步(
答复,,
createCipheriv(“aes-256-ctr”,机密,iv),
createWriteStream(路径),
);
返回{secret:secret.toString('base64'),iv:iv.toString('base64'),path};
};
const decryptFile=async({path,secret,iv})=>{
const secretBuf=Buffer.from(secret,'base64');
const ivBuf=Buffer.from(iv,'base64');
等待管道同步(
createReadStream(路径),
createDecipheriv('aes-256-ctr',秘密、体外受精),
createWriteStream(`${path}-已解密`),
);
};
下载加密文件('http://www.google.com/“,”加密路径“)
。然后(解密文件);
为了进行加密,我们:

  • 发出请求并接收响应对象,该对象是可读流
  • 通过一个密码传递整个响应
  • 通过管道将密码的输出传输到文件
  • 通过利用流,我们可以保持较低的内存占用,甚至可以非常有效地加密和解密大型文件


    上面的代码片段已准备好运行(在节点v12.18.3上测试)。

    我同意您的建议,但这根本不能回答问题!