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

Decrypt a file in dart is taking an eternity to decrypt #126

Closed
marcojr opened this issue Apr 6, 2020 · 6 comments
Closed

Decrypt a file in dart is taking an eternity to decrypt #126

marcojr opened this issue Apr 6, 2020 · 6 comments
Assignees
Labels
question Further information is requested wontfix This will not be worked on

Comments

@marcojr
Copy link

marcojr commented Apr 6, 2020

Give this simple scenario...

import 'package:dio/dio.dart';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:encrypt/encrypt.dart' as encrypt;
import 'package:path_provider/path_provider.dart';

    final dir = await getApplicationDocumentsDirectory();
    String newDir = '${dir.path}/myDir';
    await Directory(newDir).create(recursive: true);
    await Dio().download('https://www.marcojr.co.uk/tmp/encrypted.mp3', '${dir.path}/encrypted.mp3');
    final File encodedMp3File = File('${dir.path}/encrypted.mp3');
    final receivedData = await encodedMp3File.readAsBytes();
    final key1 = encrypt.Key.fromUtf8("12345678901234567890123456789012");
    final iv = encrypt.IV.fromLength(16);
    final encrypter = encrypt.Encrypter(encrypt.AES(key1, mode: encrypt.AESMode.cbc));
    debugPrint('Starting decryption... ${DateTime.now()}');
    final decrypted = encrypter.decryptBytes(encrypt.Encrypted(receivedData), iv: iv);
    debugPrint('Finished decryption... ${DateTime.now()}');

This is taking more than 10 minutes to decrypt !

I/flutter ( 4949): Starting decryption... 2020-04-06 14:16:47.041209
I/flutter ( 4949): Finished decryption... 2020-04-06 14:26:59.547053

For comparison effect, I wrote this very same code using nodeJS and this took just miliseconds.

const axios = require('axios');
const fs = require('fs');
const crypto = require('crypto');
axios({
    method: "get",
    url: "https://www.marcojr.co.uk/tmp/encrypted.mp3",
    responseType: "stream"
}).then(async function (response) {
    var writeStream = fs.createWriteStream("./encrypted.mp3");
    response.data.pipe(writeStream);
    writeStream.on('finish', function (err) {
        console.log('start decryption ' + new Date());
        const encryptedFile = fs.readFileSync('./encrypted.mp3');
        const x_iv = crypto.randomBytes(16);
        const decipher = crypto.createDecipheriv('aes-256-cbc', "12345678901234567890123456789012", x_iv);
        const result = Buffer.concat([decipher.update(encryptedFile), decipher.final()]);
        fs.writeFileSync('decrypted.mp3',result);
        console.log('finished decrypt... ' + new Date());
      });
});
@leocavalcante leocavalcante self-assigned this Apr 6, 2020
@leocavalcante leocavalcante added question Further information is requested wontfix This will not be worked on labels Apr 6, 2020
@leocavalcante
Copy link
Owner

DartVM isn't suited for such task.
Related: #105 #81 #76 #40

@wujek-srujek
Copy link

wujek-srujek commented May 10, 2020

Why do you say Dart is not suited for this? I don't want to sound rude or anything, I am curious, I am just beginning with Dart (with Flutter), and want to learn.

Actually, I have just tested it with https://pub.dev/packages/cryptography and while this lib (but really PointyCastle) took 40 seconds for a 1.3 mb image (encryption and decryption alike, using AES-CBC-256), the 'cryptography' takes about 50ms. I am astonished, but can verify the the encrypted / decrypted data is correct (I'm comparing against sample data from yet another, C++ based, library).

@leocavalcante
Copy link
Owner

How cool, there is a great alternative then, nice!

@devNamanG
Copy link

Why do you say Dart is not suited for this? I don't want to sound rude or anything, I am curious, I am just beginning with Dart (with Flutter), and want to learn.

Actually, I have just tested it with https://pub.dev/packages/cryptography and while this lib (but really PointyCastle) took 40 seconds for a 1.3 mb image (encryption and decryption alike, using AES-CBC-256), the 'cryptography' takes about 50ms. I am astonished, but can verify the the encrypted / decrypted data is correct (I'm comparing against sample data from yet another, C++ based, library).

Hey @wujek-srujek , i would be really greatful if you could share that encryption that you did with the Cryptography package, i too want to implement that, because, i am afraid to say, the encrypt package does encrypt files very slowly, i tested it for a pdf file of size 12.3 mb and that took 9-9.5 minutes from reading and writing, i tested that read/write only took about < 1.5 secs. I am a newbie to encryption and really would be grateful if you share some insights on how you acheived it with cryptography package. Thank you.

@wujek-srujek
Copy link

wujek-srujek commented May 23, 2021

Hi @namangor04 , as far as I remember (I don't have the code any more) I didn't do anything special, just followed the docs, you can see an example here: https://pub.dev/documentation/cryptography/latest/cryptography/AesCbc-class.html.

Note: you should probably use AES-GCM (the example above is AES-CBC) as it is more secure, but I specifically needed to use CBC.

@devNamanG
Copy link

Hi @wujek-srujek , thank you for your response, I did implement that as you said, with AES-GCM and it works like a charm, thank you for your assistance. Really Appreciate your assistance. I tested it out and was able to read, encrypt, decrypt, save the same file in <7 seconds. I would still use this package for strings maybe, but thank you for your assistance brother, you saved me a lot of time (as I am on a tight 5 day deadline for the current project).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

4 participants