java - 如何使用 Dart 加密视频文件?

标签 java flutter encryption dart media

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

最佳答案

这是我找到的解决方案。希望能帮助到你。记得添加包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);

}

关于java - 如何使用 Dart 加密视频文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59981337/

相关文章:

flutter - 在 Flutter 中添加 3 点弹出菜单 AppBar 的最简单方法

flutter - 当没有上下文可用时,如何从异步执行中显示 SnackBar?

java - android 或 java 中的 MD5 哈希

java - 将 <outputDirectory> 添加到 pom 时类路径为空

Java 应用程序无法在命令行上运行,但在 Eclipse 中正常

objective-c - objective-c 中使用 CBC 模式的 3DES 加密

node.js - : Could not find valid key packet for encryption in key 加密时 Node openpgp错误

java - 扩展抽象类的奇怪行为(练习)

java.lang.NullPointerException : Attempt to invoke virtual method 'android. content.pm.PackageManager

java - 密码套件不可用 - 如何添加/启用? Tomcat SSL