如何解密Node.js使用RC4加密的CryptoJS中的文件

时间:2018-12-04 06:49:33

标签: javascript node.js encryption cryptojs node-crypto

我想解密(使用cryptojs)一个加密的mp4文件(使用nodejs crypto rc4)。我无法解密该文件,这是Nodejs的新功能。感谢您的帮助。

下面是在nodejs上用于加密(npm包文件加密器)的代码:

var crypto = require('crypto'),
    fs = require('fs');

var Encryptor = {};

Encryptor.encryptFile = function(inputPath, outputPath, key, options, callback) {

  if(typeof options === 'function') {
    callback = options;
    options = {};
  }

  options = Encryptor.combineOptions(options);

  var keyBuf = new Buffer(key);

  var inputStream = fs.createReadStream(inputPath);
  var outputStream = fs.createWriteStream(outputPath);
  var cipher = crypto.createCipher(options.algorithm, keyBuf);

  inputStream.on('data', function(data) {
    var buf = new Buffer(cipher.update(data), 'binary');
    outputStream.write(buf);
  });

  inputStream.on('end', function() {
    try {
      var buf = new Buffer(cipher.final('binary'), 'binary');
      outputStream.write(buf);
      outputStream.end();
      outputStream.on('close', function() {
        return callback();
      });
    } catch(e) {
      fs.unlink(outputPath);
      return callback(e);
    }
  });
};

a
  

在我的JavaScript代码下面,我正在尝试使用cryptojs

window.resolveLocalFileSystemURL(inputPath, function (file) {
        file.file(function(ffile) {

                          var reader = new FileReader();
                          var imgBlob = new Blob([ffile], { type: 'video/mp4' });
                          reader.onload = function(e) {

                                var text= Encryptor.Utf8ArrayToStr(e.target.result);
                                  var outputString=CryptoJS.RC4.decrypt(text,key,{padding: CryptoJS.pad.NoPadding} );
                                  var buf = new Buffer([outputString],'binary');
                                  var imgBlob = new Blob([outputString], { type: 'video/mp4' });
                                  fileEntry.createWriter(function(fileWriter) {
                                    fileWriter.write(imgBlob);
                                  });
                            //});

                          };

                          reader.onloadend = function(e) {

                                                          var text= Encryptor.Utf8ArrayToStr(e.target.result);//,function(text){
                                  var outputString=CryptoJS.RC4.decrypt(text,key ,{padding: CryptoJS.pad.NoPadding});
                                  var buf = new Buffer([outputString],'binary');
                                  var imgBlob = new Blob([outputString], { type: 'video/mp4' });
                                  fileEntry.createWriter(function(fileWriter) {
                                    fileWriter.write(imgBlob);
                                  });
                            //});
                          };

                          reader.readAsArrayBuffer(ffile);
                      });
                    });

有人知道该怎么做吗?我长期以来一直坚持这一点。

0 个答案:

没有答案