Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AES encrypt is very slow. #44

Open
kriit24 opened this issue Nov 8, 2023 · 2 comments
Open

AES encrypt is very slow. #44

kriit24 opened this issue Nov 8, 2023 · 2 comments

Comments

@kriit24
Copy link

kriit24 commented Nov 8, 2023

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
Copy link

eviltik commented Jan 21, 2024

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

@kriit24
Copy link
Author

kriit24 commented Jan 22, 2024

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants