brix / crypto-js

JavaScript library of crypto standards.
Other
15.85k stars 2.39k forks source link

When I use large files for encryption, it will crash. Small files can be encrypted normally #335

Open peter100u opened 3 years ago

peter100u commented 3 years ago

When encrypting large files, running this line(CryptoJS.lib.WordArray.create(fileReader.result)) of code will crash

export function encryptionFile(file, callback) {
console.log('peter encryption  file ', file, file.type);
let fileReader = new FileReader();
fileReader.readAsArrayBuffer(file);
fileReader.onload = () => {
    try {
        console.log('peter big size ' + fileReader.result);
        // 下面这行大文件会崩溃,如何做分片
        // down line crash when file very big size
        let content = CryptoJS.lib.WordArray.create(fileReader.result);
        let decrypted = CryptoJS.AES.encrypt(content, key, {
            mode: CryptoJS.mode.ECB,
            padding: CryptoJS.pad.Pkcs7
        });
        const result = convertWordArrayToUint8Array(decrypted.ciphertext);
        let fileDecBlob = new Blob([result]);
        callback(fileDecBlob, result);
    } catch (e) {
        console.log("peter fail", e);
    }
};
 }
KanathanZhou commented 3 years ago

Yes you have to split the large file into chunks and I found this post super helpful

https://stackoverflow.com/a/62824083