如何使用Flutter和Dart执行文件加密 [英] How to Perform File encryption with Flutter and Dart

查看:898
本文介绍了如何使用Flutter和Dart执行文件加密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道在这里问我的问题是否合适。我只需要为我要构建的应用程序进行可行性研究。我之所以选择Flutter,是因为我允许快速创建移动应用。

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.

我的应用将以音频文件的形式存储语音消息。它可以是mp3或音频格式。

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

要使其仅可由接收者读取,我需要使用可能是AES或e2e加密的文件进行加密。

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

我需要知道在flutter应用程序中是否可以使用Dart加密文件。如果可能的话,我想获得有用的资源。

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.

推荐答案

最终找到了一些东西。我尝试了包括加密包在内的多个选项,但都无济于事。我终于找到了此程序包它可以使用AES加密文件,您所需要的只是路径到文件。有据可查。我相信最好创建一个类并添加用于加密和解密的函数,如下所示。

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;
  }
}


现在您可以像

encrypted_file_path = EncryptData.encrypt_file('your/file/path');

这篇关于如何使用Flutter和Dart执行文件加密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆