Search code examples
javascriptexpressaesnode-crypto

Speeding up AES decryption in Node js


I wanted to create web page on which would be displayed data, previously decrypted on server. On server in app.js all of data from one folder is read and then decrypted.

var http = require('http');
var path = require('path');
var express = require('express');
var fs = require('fs');
var app = express();
var CryptoJS = require("crypto-js");
app.set('view engine', 'ejs');

var bytes = [];

var markers = fs.readdirSync("views/images");

for (var i = 0; i < markers.length ; ++i) {
bytes[i] = fs.readFileSync("views/images/" + 
markers[i]).toString('utf8');
};

Than data is decrypted and send to page

app.get('/index', function(req, res) {
app.use(express.static(__dirname + '/views'));
try{
      for (var i = 0; i < markers.length ; ++i) {
      bytes[i] = CryptoJS.AES.decrypt(markers[i],Rf3hgf93).toString(CryptoJS.enc.Utf8);                       
       };
      res.render('index',{bytes:bytes});

      }catch (err){
         res.render('index',{bytes:''});
         console.log("error");
      };

  });

The thing is, it takes about 30 seconds to decrypt all of this files and send to client. There are about 35 decrypted txt files(each about 5mb). I know that node js is single-threaded and there is no concurrency. So, how can I speed up decrypting process ? Should I use Java/Python instead of node js, as far as I am concerned java is the most suitable language for this process because of multi-threading and concurrency.


Solution

  • crypto-js is a pure JavaScript AES implementation. It is meant to run in a browser as well as in NodeJs.

    For a pure NodeJs application use the built-in crypto API, which is native and as such much faster.