How can I encrypt video file using Dart?

独自空忆成欢 提交于 2020-04-30 06:28:19

问题


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.


回答1:


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

}


来源:https://stackoverflow.com/questions/59981337/how-can-i-encrypt-video-file-using-dart

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!