How to Perform File encryption with Flutter and Dart

6,764

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');
Share:
6,764
coolbeatz71
Author by

coolbeatz71

I am passionate about web development, problem-solving, and blockchain technologies. Graduated from the University of Tourism, Technology and Business Studies in Rwanda with a bachelor's degree in Business Information Technology in 2018, I am motivated to constantly improving my skills and change people’s lives through technology Kindly check my Gitlab

Updated on December 13, 2022

Comments

  • coolbeatz71
    coolbeatz71 over 1 year

    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.