Home » Javascript » Encrypt and decrypt video file in JavaScript using CryptoJS

Encrypt and decrypt video file in JavaScript using CryptoJS

Posted by: admin August 19, 2018 Leave a comment

Questions:

I would like to know how to encrypt and decrypt a video file with JavaScript. I have tried to do it with the Crypto-js library but I can’t reproduce it after decrypting it. In the following code I try to take the input video to encrypt it and after decrypting it play it from HTML and download the file:

 input.on('change', function (e) {
      let file = e.target.files[0];
      let reader = new FileReader();
      reader.onload = function () {
        let fileLeida = reader.result;
        let videoEncriptado = CryptoJS.AES.encrypt(fileLeida, getCookie('clave')).toString();
        let videoDesencriptado = CryptoJS.AES.decrypt(videoEncriptado, getCookie('clave')).toString(CryptoJS.enc.Utf8);
        var videoNode = document.getElementsByTagName('video')[0];
        let blob = new Blob([videoDesencriptado], {type: "video/mp4"});
        let url = URL.createObjectURL(blob);
        let element = document.createElement('a');
        videoNode.setAttribute('href', url);
        videoNode.play();
        element.setAttribute('href', url);
        element.setAttribute('download', 'Blurred Bokeh Video 2.mp4');
        element.style.display = 'none';
        document.body.appendChild(element);
        element.click();
        document.body.removeChild(element);

      };
      reader.readAsText(file);
});

Thank you in advance.

Answers: