How to Perform File encryption with Flutter and Dart How to Perform File encryption with Flutter and Dart dart dart

How to Perform File encryption with Flutter and Dart


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');