Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Perform File encryption with Flutter and Dart

I don't know if it is right to ask my question here. I just need to make a feasibility study for an App I am trying to build. I chose Flutter because I allow to quickly create mobile apps.

My application will be storing voice messages in forms of audio files. It can be an mp3 or an audio format.

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

I need to know if it is possible to encrypt files with Dart in my flutter app. If it is possible, I would like to get useful resources.

I tried to search for this topic, but I can only find articles about encrypting string or text files.

like image 387
coolbeatz71 Avatar asked Oct 10 '19 14:10

coolbeatz71


People also ask

How do I encrypt a file?

Right-click (or press and hold) a file or folder and select Properties. Select the Advanced button and select the Encrypt contents to secure data check box. Select OK to close the Advanced Attributes window, select Apply, and then select OK.

How do you play encrypted video on flutter?

AnswerYour videos will need to be encrypt(by your app obviously) so they could only be played by the app which have the encryption key (so your app) to decrypt them. By doing so only your app will be able to display and play the encrypted videos.


1 Answers

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');
like image 58
Hosea varghese Avatar answered Sep 21 '22 13:09

Hosea varghese