bcgit / pc-dart

Pointy Castle - Dart Derived Bouncy Castle APIs
MIT License
237 stars 122 forks source link

decrypt huge aes-ctr encrypted file #157

Open MetaiR opened 2 years ago

MetaiR commented 2 years ago

hi, first of all I am pretty new in flutter stuff so please forgive me if my question seems dumb to u

I want to decrypt huge files (the file size is somewhere between 500MB ~ 1GB). I am using this code to read file as stream and decrypt it but I don't know if this is ok or not and need some help if it can be achieved differently:

this is my code:

Future<bool> decryptStream(String input) async {
    try {
      final iv = EncryptPlugin.IV(Uint8List.fromList(hex.decode('hexed-iv')));
      final key = EncryptPlugin.Key.fromUtf8('my-secret-key');
      final ctr = pc.CTRStreamCipher(pc.AESEngine())
        ..init(false, pc.ParametersWithIV(pc.KeyParameter(key.bytes), iv.bytes));

      final outputFile = File('${privateAppDir.path}/op/$input.mp4');
      final output = outputFile.openWrite(mode: FileMode.writeOnlyAppend);

      await File(privateAppDir.path + '/' + input).openRead()
        .map((List<int> data) => Uint8List.fromList(data))
        .map((Uint8List data) => ctr.process(data))
        .map((Uint8List data) => List.from(data))
        .pipe(output);

      return true;
    } catch (exc) {
      print(exc);
      return false;
    }
  }

please guide me