10

I need to make a feasibility study for an app I am trying to build. I chose Flutter because it allows me to quickly create mobile apps.

My application will be storing voice messages in the form of audio files. It can be an mp3 or any other audio format.

To make it readable by the receiver only, I need to encrypt the file using maybe AES or e2e encryption.

Is it possible to encrypt files with Dart in my Flutter app?

TylerH
  • 20,799
  • 66
  • 75
  • 101
coolbeatz71
  • 928
  • 2
  • 10
  • 22
  • 2
    You can encrypt a file using Dart. A file is just a sequence of bytes, which is exactly what stream and block ciphers work on. (Block ciphers must be used in a 'mode': for example, CTR, GCM, CCM, etc.) The design of crypto systems (key management, key material derivation, etc.) is outside of the scope of SO questions, however. – Richard Heap Oct 10 '19 at 15:54
  • 2
    https://pub.dev/packages/encrypt – rstrelba Oct 10 '19 at 16:52
  • 1
    Probably better to recommend PointyCastle (a port of BouncyCastle) which operates on `UInt8List`, i.e. byte arrays instead. However, you may want to consider using native / Java code for this particular part, because encryption may not be as speedy as you expect. Even then, you can of course first try Dart. – Maarten Bodewes Oct 10 '19 at 16:56

1 Answers1

16

Finally found something. I tried multiple options including the encrypt package, but all were dead ends. I finally found this package It can encrypt files using AES all you need is the path to the file. It is well documented. I believe its best to create a class and add functions for encrypt and decrypt as I have did below.

import 'dart:io';
import 'package:aes_crypt/aes_crypt.dart';

class EncryptData {
  static String encrypt_file(String path) {
    AesCrypt crypt = AesCrypt();
    crypt.setOverwriteMode(AesCryptOwMode.on);
    crypt.setPassword('my cool password');
    String encFilepath;
    try {
      encFilepath = crypt.encryptFileSync(path);
      print('The encryption has been completed successfully.');
      print('Encrypted file: $encFilepath');
    } catch (e) {
      if (e.type == AesCryptExceptionType.destFileExists) {
        print('The encryption has been completed unsuccessfully.');
        print(e.message);
      }
      else{
        return 'ERROR';
      }
    }
    return encFilepath;
  }

  static String decrypt_file(String path) {
    AesCrypt crypt = AesCrypt();
    crypt.setOverwriteMode(AesCryptOwMode.on);
    crypt.setPassword('my cool password');
    String decFilepath;
    try {
      decFilepath = crypt.decryptFileSync(path);
      print('The decryption has been completed successfully.');
      print('Decrypted file 1: $decFilepath');
      print('File content: ' + File(decFilepath).path);
    } catch (e) {
      if (e.type == AesCryptExceptionType.destFileExists) {
        print('The decryption has been completed unsuccessfully.');
        print(e.message);
      }
      else{
        return 'ERROR';
      }
    }
    return decFilepath;
  }
}


Now you can use it like

encrypted_file_path = EncryptData.encrypt_file('your/file/path');
Hosea varghese
  • 301
  • 3
  • 15
  • 1
    I don't really get from the package description, whether this flutter library will work on its own on any device or does one have to install a separate encryption app from them? – w461 Nov 02 '20 at 11:06
  • 2
    With this technique, you need to store the decrypted file in the memory. This will allow anyone to copy it, or read with another music player. I am looking for a way to decrypt byte by byte and read on the go on my media player – coolbeatz71 Nov 16 '20 at 21:32
  • use this package https://pub.dev/packages/aes_crypt_null_safe/versions/2.0.0 – Ahmed Raafat Jul 13 '23 at 08:19