entronad / crypto-es

A cryptography algorithms library
Other
272 stars 30 forks source link

AES encrypt is very slow. #44

Open kriit24 opened 10 months ago

kriit24 commented 10 months ago

Data below is very slow, it takes about 26 seconds to encrypt 8mb data. I also submited the data as attacment

import React from 'react';
import CryptoES from 'crypto-es';
import Base64 from './base64';

let start = null;
class speedtest{

    static start(txt){

        start = Date.now();

        if( txt !== undefined )
            console.log(txt);
    }

    static end(txt){

        console.log(txt, ((Date.now() - start) / 1000) + " seconds");
    }
}

export default class Ws_crypto {

    static mac_hash = null;

    static sign(data) {

        speedtest.start('CRYPTO');

        if (!Ws_crypto.mac_hash)
            return {'data': null, mac: null};

        let key_string = Ws_crypto.mac_hash;
        let iv_string = CryptoES.lib.WordArray.random(16).toString();
        let mac_string = null;
        let encrypted_string = null;

        speedtest.end('CCC-1');

        let encHex = CryptoES.enc.Hex;
        let aes = CryptoES.AES;
        let Pkcs7 = CryptoES.pad.Pkcs7;

        speedtest.end('CCC-2');

        // the key and iv should be 32 hex digits each, any hex digits you want, but it needs to be 32 on length each
        let key = encHex.parse(key_string);
        let iv = encHex.parse(iv_string);
        try {

            //data = JSON.parse(data);
        } catch (error) {

        }
        let data_string = typeof data == 'object' ? JSON.stringify(data) : data;

        speedtest.end('CCC-3');//above takes 13 seconds total time

        // encrypt the message
        encrypted_string = aes.encrypt(data_string, key, {
            iv: iv,
            padding: Pkcs7,
            mode: CryptoES.mode.CBC
        }).toString();
        speedtest.end('CCC-4');//above takes 26 seconds total time
        mac_string = CryptoES.HmacSHA256(encrypted_string, key).toString(CryptoES.enc.Hex);

        speedtest.end('CCC-5');

        let ret = {'data': data_string, 'mac': Base64.btoa(mac_string + iv_string)};

        speedtest.end('CCC-6');
        return ret;
    }
}

_data.txt

eviltik commented 7 months ago

@kriit24 did you come from crypto-js ? did you try with it ? better or same ?

kriit24 commented 7 months ago

@eviltik i tried with crypto-js and with crypto-es. both are slow with big data set. for now i avoided image string encryption but it would be very good for security