[英]How can I encrypt video file using Dart?
我正在嘗試使用 dart 加密視頻剪輯。 我已經測試了這個 java 代碼https://stackoverflow.com/a/9496626/8511016並希望使用 dart 做同樣的事情。
這是我找到的解決方案。 希望能幫助到你。 記得在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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.