aes_encrypt_file 0.0.8
aes_encrypt_file: ^0.0.8 copied to clipboard
This is a Flutter library used for file encryption, using language C for performance optimization when handling large data.
import 'package:file_encrypter/file_encrypter.dart';
import 'package:flutter/material.dart';
import 'dart:async';
import 'dart:io';
import 'package:aes_encrypt_file/aes_encrypt_file.dart';
import 'package:file_picker/file_picker.dart';
import 'package:path_provider/path_provider.dart';
import 'memory_monitor.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
final String _platformVersion = 'Unknown';
final _aesEncryptFilePlugin = AesEncryptFile();
String? _selectedImagePath;
String? _encryptedPath;
String? _encryptedPathFileEncryptor;
String? _decryptedImagePath;
bool _isEncrypting = false;
bool _isDecrypting = false;
bool _isEncryptingFileEncryptor = false;
bool _isDecryptingFileEncryptor = false;
String _statusMessage = '';
var encryptionKey ='encryptionKey';
var ivStringKey = 'kA7xP2qL9sD4fH1k';
@override
void initState() {
super.initState();
}
// Chọn ảnh từ thư viện
Future<void> _pickImage() async {
try {
FilePickerResult? result = await FilePicker.platform.pickFiles(
type: FileType.image,
allowMultiple: false,
);
if (result != null && result.files.single.path != null) {
setState(() {
_selectedImagePath = result.files.single.path;
_statusMessage = 'Đã chọn ảnh: ${result.files.single.name}';
_decryptedImagePath = null; // Reset decrypted image when new image is selected
});
} else {
setState(() {
_statusMessage = 'Không chọn ảnh nào';
});
}
} catch (e) {
setState(() {
_statusMessage = 'Lỗi khi chọn ảnh: $e';
});
}
}
// Mã hóa ảnh đã chọn
Future<void> _encryptImage() async {
if (_selectedImagePath == null) {
setState(() {
_statusMessage = 'Vui lòng chọn ảnh trước';
});
return;
}
setState(() {
_isEncrypting = true;
_statusMessage = 'Đang mã hóa ảnh...';
});
try {
// Tạo đường dẫn cho file đã mã hóa
final directory = await getApplicationDocumentsDirectory();
final fileName = _selectedImagePath!.split('/').last;
final encryptedPath = '${directory.path}/encrypted_$fileName';
this.encryptionKey = encryptionKey;
final stopwatch = Stopwatch()..start();
final memoryMonitor = MemoryMonitor();
memoryMonitor.start();
// Thực hiện mã hóa
final success = await _aesEncryptFilePlugin.encryptFile(
inputPath: _selectedImagePath!,
outputPath: encryptedPath,
key: encryptionKey,
iv: ivStringKey
);
memoryMonitor.updatePeak();
stopwatch.stop();
final memoryReport = memoryMonitor.getReport();
if (success) {
final encryptedFile = File(encryptedPath);
final encryptedSize = await encryptedFile.length();
setState(() {
_encryptedPath = encryptedPath;
_statusMessage =
'Mã hóa Native thành công!\n'
'Thời gian: ${stopwatch.elapsedMilliseconds}ms\n'
'Kích thước: $encryptedSize bytes\n'
'${memoryReport.toDisplayString()}\n'
'File đã lưu tại: $encryptedPath';
});
} else {
setState(() {
_statusMessage = 'Mã hóa thất bại';
});
}
} catch (e) {
setState(() {
_statusMessage = 'Lỗi khi mã hóa: $e';
});
} finally {
setState(() {
_isEncrypting = false;
});
}
}
// Giải mã bằng pure Dart để kiểm tra
Future<void> _decryptWithDart() async {
if (_encryptedPath == null) {
setState(() {
_statusMessage = 'Vui lòng mã hóa ảnh trước';
});
return;
}
setState(() {
_isDecrypting = true;
_statusMessage = 'Đang giải mã bằng Dart...';
});
try {
final directory = await getApplicationDocumentsDirectory();
final fileName = _selectedImagePath!.split('/').last;
final decryptedPath = '${directory.path}/decrypted_dart_$fileName';
final stopwatch = Stopwatch()..start();
final memoryMonitor = MemoryMonitor();
memoryMonitor.start();
// Thực hiện giải mã bằng pure Dart
final success = await _aesEncryptFilePlugin.decryptFile(
inputPath: _encryptedPath!,
outputPath: decryptedPath,
key: encryptionKey,
iv: ivStringKey
);
memoryMonitor.updatePeak();
stopwatch.stop();
final memoryReport = memoryMonitor.getReport();
if (success) {
// Kiểm tra kích thước file để xác minh
final originalFile = File(_selectedImagePath!);
final decryptedFile = File(decryptedPath);
final originalSize = await originalFile.length();
final decryptedSize = await decryptedFile.length();
setState(() {
_decryptedImagePath = decryptedPath;
_statusMessage =
'Giải mã Native thành công!\n'
'Thời gian: ${stopwatch.elapsedMilliseconds}ms\n'
'File gốc: $originalSize bytes\n'
'File giải mã: $decryptedSize bytes\n'
'${memoryReport.toDisplayString()}\n'
'Đã lưu tại: $decryptedPath';
});
} else {
setState(() {
_statusMessage = 'Giải mã Native thất bại';
});
}
} catch (e) {
setState(() {
_statusMessage = 'Lỗi khi giải mã Native: $e';
});
} finally {
setState(() {
_isDecrypting = false;
});
}
}
// Mã hóa bằng file_encryptor
Future<void> _encryptWithFileEncryptor() async {
if (_selectedImagePath == null) {
setState(() {
_statusMessage = 'Vui lòng chọn ảnh trước';
});
return;
}
setState(() {
_isEncryptingFileEncryptor = true;
_statusMessage = 'Đang mã hóa bằng file_encryptor...';
});
try {
final directory = await getApplicationDocumentsDirectory();
final fileName = _selectedImagePath!.split('/').last;
final encryptedPath = '${directory.path}/encrypted_fe_$fileName';
final stopwatch = Stopwatch()..start();
final memoryMonitor = MemoryMonitor();
memoryMonitor.start();
// Thực hiện mã hóa bằng file_encryptor
encryptionKey = await FileEncrypter.encrypt(
inFileName: _selectedImagePath!,
outFileName: encryptedPath,
// filePath: _selectedImagePath!,
// outputPath: encryptedPath,
// key: encryptionKey,
);
memoryMonitor.updatePeak();
stopwatch.stop();
final memoryReport = memoryMonitor.getReport();
final encryptedFile = File(encryptedPath);
if (await encryptedFile.exists()) {
final encryptedSize = await encryptedFile.length();
setState(() {
_encryptedPathFileEncryptor = encryptedPath;
_statusMessage =
'Mã hóa file_encryptor thành công!\n'
'Thời gian: ${stopwatch.elapsedMilliseconds}ms\n'
'Kích thước: $encryptedSize bytes\n'
'${memoryReport.toDisplayString()}\n'
'File đã lưu tại: $encryptedPath';
});
} else {
setState(() {
_statusMessage = 'Mã hóa file_encryptor thất bại';
});
}
} catch (e) {
setState(() {
_statusMessage = 'Lỗi khi mã hóa file_encryptor: $e';
});
} finally {
setState(() {
_isEncryptingFileEncryptor = false;
});
}
}
// Giải mã bằng file_encryptor
Future<void> _decryptWithFileEncryptor() async {
if (_encryptedPathFileEncryptor == null) {
setState(() {
_statusMessage = 'Vui lòng mã hóa bằng file_encryptor trước';
});
return;
}
setState(() {
_isDecryptingFileEncryptor = true;
_statusMessage = 'Đang giải mã bằng file_encryptor...';
});
try {
final directory = await getApplicationDocumentsDirectory();
final fileName = _selectedImagePath!.split('/').last;
final decryptedPath = '${directory.path}/decrypted_fe_$fileName';
// const encryptionKey = 'mySecretKey12345';
final stopwatch = Stopwatch()..start();
final memoryMonitor = MemoryMonitor();
memoryMonitor.start();
// Thực hiện giải mã bằng file_encryptor
await FileEncrypter.decrypt(
inFileName: _encryptedPathFileEncryptor!,
outFileName: decryptedPath,
key: encryptionKey,
);
memoryMonitor.updatePeak();
stopwatch.stop();
final memoryReport = memoryMonitor.getReport();
final decryptedFile = File(decryptedPath);
if (await decryptedFile.exists()) {
final originalFile = File(_selectedImagePath!);
final originalSize = await originalFile.length();
final decryptedSize = await decryptedFile.length();
setState(() {
_decryptedImagePath = decryptedPath;
_statusMessage =
'Giải mã file_encryptor thành công!\n'
'Thời gian: ${stopwatch.elapsedMilliseconds}ms\n'
'File gốc: $originalSize bytes\n'
'File giải mã: $decryptedSize bytes\n'
'${memoryReport.toDisplayString()}\n'
'Đã lưu tại: $decryptedPath';
});
} else {
setState(() {
_statusMessage = 'Giải mã file_encryptor thất bại';
});
}
} catch (e) {
setState(() {
_statusMessage = 'Lỗi khi giải mã file_encryptor: $e';
});
} finally {
setState(() {
_isDecryptingFileEncryptor = false;
});
}
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('AES Image Encryption Example'),
backgroundColor: Colors.blue,
),
body: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Text(
'Chạy trên: $_platformVersion',
style: const TextStyle(fontSize: 14, color: Colors.grey),
),
const SizedBox(height: 20),
// Nút chọn ảnh
ElevatedButton.icon(
onPressed:
(_isEncrypting ||
_isDecrypting ||
_isEncryptingFileEncryptor ||
_isDecryptingFileEncryptor)
? null
: _pickImage,
icon: const Icon(Icons.image),
label: const Text(
'Chọn Ảnh từ Thư viện',
style: TextStyle(fontSize: 16),
),
style: ElevatedButton.styleFrom(
padding: const EdgeInsets.symmetric(vertical: 16),
backgroundColor: Colors.blue,
foregroundColor: Colors.white,
),
),
const SizedBox(height: 16),
// Nút mã hóa ảnh
ElevatedButton.icon(
onPressed:
(_selectedImagePath != null &&
!_isEncrypting &&
!_isDecrypting &&
!_isEncryptingFileEncryptor &&
!_isDecryptingFileEncryptor)
? _encryptImage
: null,
icon: _isEncrypting
? const SizedBox(
width: 20,
height: 20,
child: CircularProgressIndicator(
strokeWidth: 2,
color: Colors.white,
),
)
: const Icon(Icons.lock),
label: Text(
_isEncrypting ? 'Đang mã hóa...' : 'Mã hóa Ảnh (Native)',
style: const TextStyle(fontSize: 16),
),
style: ElevatedButton.styleFrom(
padding: const EdgeInsets.symmetric(vertical: 16),
backgroundColor: Colors.green,
foregroundColor: Colors.white,
),
),
const SizedBox(height: 16),
// Nút giải mã bằng Dart
ElevatedButton.icon(
onPressed:
(_encryptedPath != null &&
!_isEncrypting &&
!_isDecrypting &&
!_isEncryptingFileEncryptor &&
!_isDecryptingFileEncryptor)
? _decryptWithDart
: null,
icon: _isDecrypting
? const SizedBox(
width: 20,
height: 20,
child: CircularProgressIndicator(
strokeWidth: 2,
color: Colors.white,
),
)
: const Icon(Icons.lock_open),
label: Text(
_isDecrypting
? 'Đang giải mã...'
: 'Giải mã ảnh (Native)',
style: const TextStyle(fontSize: 16),
),
style: ElevatedButton.styleFrom(
padding: const EdgeInsets.symmetric(vertical: 16),
backgroundColor: Colors.orange,
foregroundColor: Colors.white,
),
),
const SizedBox(height: 20),
// Divider để phân tách
const Divider(thickness: 2),
const Text(
'File Encryptor Library (Để so sánh tốc độ)',
style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
textAlign: TextAlign.center,
),
const SizedBox(height: 16),
// Nút mã hóa bằng file_encryptor
ElevatedButton.icon(
onPressed:
(_selectedImagePath != null &&
!_isEncrypting &&
!_isDecrypting &&
!_isEncryptingFileEncryptor &&
!_isDecryptingFileEncryptor)
? _encryptWithFileEncryptor
: null,
icon: _isEncryptingFileEncryptor
? const SizedBox(
width: 20,
height: 20,
child: CircularProgressIndicator(
strokeWidth: 2,
color: Colors.white,
),
)
: const Icon(Icons.lock),
label: Text(
_isEncryptingFileEncryptor
? 'Đang mã hóa...'
: 'Mã hóa Ảnh (file_encryptor)',
style: const TextStyle(fontSize: 16),
),
style: ElevatedButton.styleFrom(
padding: const EdgeInsets.symmetric(vertical: 16),
backgroundColor: Colors.purple,
foregroundColor: Colors.white,
),
),
const SizedBox(height: 16),
// Nút giải mã bằng file_encryptor
ElevatedButton.icon(
onPressed:
(_encryptedPathFileEncryptor != null &&
!_isEncrypting &&
!_isDecrypting &&
!_isEncryptingFileEncryptor &&
!_isDecryptingFileEncryptor)
? _decryptWithFileEncryptor
: null,
icon: _isDecryptingFileEncryptor
? const SizedBox(
width: 20,
height: 20,
child: CircularProgressIndicator(
strokeWidth: 2,
color: Colors.white,
),
)
: const Icon(Icons.lock_open),
label: Text(
_isDecryptingFileEncryptor
? 'Đang giải mã...'
: 'Giải mã bằng file_encryptor',
style: const TextStyle(fontSize: 16),
),
style: ElevatedButton.styleFrom(
padding: const EdgeInsets.symmetric(vertical: 16),
backgroundColor: Colors.deepPurple,
foregroundColor: Colors.white,
),
),
const SizedBox(height: 20),
// Hiển thị thông tin
if (_selectedImagePath != null)
Container(
padding: const EdgeInsets.all(12),
decoration: BoxDecoration(
color: Colors.blue.shade50,
borderRadius: BorderRadius.circular(8),
border: Border.all(color: Colors.blue.shade200),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text(
'Ảnh đã chọn:',
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 14,
),
),
const SizedBox(height: 4),
Text(
_selectedImagePath!,
style: const TextStyle(fontSize: 12),
),
],
),
),
const SizedBox(height: 16),
// Hiển thị ảnh đã giải mã
if (_decryptedImagePath != null)
Container(
padding: const EdgeInsets.all(12),
decoration: BoxDecoration(
color: Colors.green.shade50,
borderRadius: BorderRadius.circular(8),
border: Border.all(color: Colors.green.shade200),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text(
'Ảnh đã giải mã:',
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 14,
color: Colors.green,
),
),
const SizedBox(height: 8),
ClipRRect(
borderRadius: BorderRadius.circular(8),
child: Image.file(
File(_decryptedImagePath!),
fit: BoxFit.contain,
width: double.infinity,
),
),
],
),
),
const SizedBox(height: 16),
// Hiển thị trạng thái
if (_statusMessage.isNotEmpty)
Container(
padding: const EdgeInsets.all(12),
decoration: BoxDecoration(
color: _statusMessage.contains('thành công')
? Colors.green.shade50
: _statusMessage.contains('Lỗi') ||
_statusMessage.contains('thất bại')
? Colors.red.shade50
: Colors.grey.shade50,
borderRadius: BorderRadius.circular(8),
border: Border.all(
color: _statusMessage.contains('thành công')
? Colors.green.shade200
: _statusMessage.contains('Lỗi') ||
_statusMessage.contains('thất bại')
? Colors.red.shade200
: Colors.grey.shade200,
),
),
child: Text(
_statusMessage,
style: TextStyle(
fontSize: 14,
color: _statusMessage.contains('thành công')
? Colors.green.shade900
: _statusMessage.contains('Lỗi') ||
_statusMessage.contains('thất bại')
? Colors.red.shade900
: Colors.black87,
),
),
),
],
),
),
),
),
);
}
}