如何使用Dart加密视频文件? [英] How can I encrypt video file using Dart?

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

问题描述

我正在尝试使用dart加密视频剪辑.我已经测试过此Java代码 https://stackoverflow.com/a/9496626/8511016 ,并且希望这样做但使用飞镖.

I am trying to encrypt a video clip using dart. I have tested this java code https://stackoverflow.com/a/9496626/8511016 and would like to do the same but using dart.

推荐答案

这是我找到的解决方案.希望能帮助到你.请记住将包加密包添加到pubspec.yaml

Here is the solution that I found. Hope it helps. Remember to add the package encryption package to 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);

}

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

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