将CryptoJS库与jQuery-file-upload集成

时间:2015-09-03 13:05:36

标签: javascript jquery file-upload blueimp cryptojs

我想通过blueimp将CryptoJS库与jQuery-file-upload小部件集成。我想为文件上传实施静默端加密,并询问this问题但没有回答。

我目前正在执行以下程序。但是上传的文件不是我在js中创建的加密文件。我哪里弄错了?或者有更好的方法吗?

我知道data.files [index_number]给出了存储为blob的文件。我想用相同的加密版本替换该文件。所以我做了

    var reader = new window.FileReader();
    var myFile = data.files[0];

    //create a blob
    var inputBlob = myFile.slice(0,myFile.size);
    reader.readAsText(inputBlob);

    reader.onload(function() {
        var strToEncrypt = reader.result();

        //get key and iv
        var key = CryptoJS.enc.Base64.parse("my_key");
        var iv = CryptoJS.enc.Base64.parse("my_iv");

        //encrypt
        var encryptedObject = CryptoJS.AES.encrypt(strToEncrypt , key, { iv: iv });

        //extract and stringify ciphertext from encrypted file object
        var encryptedString = CryptoJS.enc.Base64.stringify(encryptedObject.ciphertext);

        //do this procedure to convert to blob
        var byteChars = atob(encryptedString);
        var byteNumbers = new Array(byteChars.length);
        for (var i = 0; i < byteChars.length; i++) {
             byteNumbers[i] = byteChars.charCodeAt(i);
        }
        var byteArray = new Uint8Array(byteNumbers);
    }
        var outputBlob = new Blob([byteArray], {type: "file"});

        //function to create a file from blob
        function blobToFile(theBlob, fileName){
        //A Blob() is almost a File() - it's just missing the two properties below which we will add now
                    theBlob.lastModifiedDate = new Date();
                    theBlob.name = fileName;
                    return theBlob;
        }

        //make the final file
        var outputFile = blobToFile(outputBlob, "encr_file");

        //replace original file with this file
        data.files[0] = outputFile;

请帮忙。

0 个答案:

没有答案