【问题标题】:How can I encrypt video file using Dart?如何使用 Dart 加密视频文件?
【发布时间】:2020-05-15 19:00:27
【问题描述】:

我正在尝试使用 dart 加密视频剪辑。我已经测试了这个 java 代码https://stackoverflow.com/a/9496626/8511016 并且想做同样的事情,但是使用 dart。

【问题讨论】:

    标签: java flutter encryption dart media


    【解决方案1】:

    这是我找到的解决方案。希望能帮助到你。记得将包encryption package添加到pubspec.yaml

    import 'dart:convert';
    import 'dart:io';
    
    import 'package:encrypt/encrypt.dart';
    
    main() {
    
      perfomEncryptionTasks();
    }
    
    perfomEncryptionTasks() async {
      await encryptFile();
      await decryptFile();
    }
    
    encryptFile() async {
      File inFile = new File("video.mp4");
      File outFile = new File("videoenc.aes");
    
      bool outFileExists = await outFile.exists();
    
      if(!outFileExists){
        await outFile.create();
      }
    
      final videoFileContents = await inFile.readAsStringSync(encoding: latin1);
    
      final key = Key.fromUtf8('my 32 length key................');
      final iv = IV.fromLength(16);
    
      final encrypter = Encrypter(AES(key));
    
      final encrypted = encrypter.encrypt(videoFileContents, iv: iv);
      await outFile.writeAsBytes(encrypted.bytes);
    }
    
    decryptFile() async {
      File inFile = new File("videoenc.aes");
      File outFile = new File("videodec.mp4");
    
      bool outFileExists = await outFile.exists();
    
      if(!outFileExists){
        await outFile.create();
      }
    
      final videoFileContents = await inFile.readAsBytesSync();
    
      final key = Key.fromUtf8('my 32 length key................');
      final iv = IV.fromLength(16);
    
      final encrypter = Encrypter(AES(key));
    
      final encryptedFile = Encrypted(videoFileContents);
      final decrypted = encrypter.decrypt(encryptedFile, iv: iv);
    
      final decryptedBytes = latin1.encode(decrypted);
      await outFile.writeAsBytes(decryptedBytes);
    
    }
    

    【讨论】:

    • 这个答案可以处理大型视频文件吗?大约 700 MB?
    • 它给了我一个错误,说无法创建 outFile。你有同样的错误吗?
    猜你喜欢
    • 2012-03-18
    • 2016-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-29
    • 2013-04-14
    • 2012-01-01
    • 1970-01-01
    相关资源
    最近更新 更多