file_encryptor_screen.dart 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import 'package:flutter/material.dart';
  2. import 'package:file_picker/file_picker.dart';
  3. import 'dart:io';
  4. import 'file_helper.dart';
  5. import 'about_screen.dart';
  6. class FileEncryptorScreen extends StatefulWidget {
  7. @override
  8. _FileEncryptorScreenState createState() => _FileEncryptorScreenState();
  9. }
  10. class _FileEncryptorScreenState extends State<FileEncryptorScreen> {
  11. final _passwordController = TextEditingController();
  12. final FileHelper _fileHelper = FileHelper();
  13. Future<void> _encryptFile() async {
  14. if (_passwordController.text.isEmpty) {
  15. ScaffoldMessenger.of(context).showSnackBar(
  16. SnackBar(content: Text('Password cannot be empty')),
  17. );
  18. return;
  19. }
  20. FilePickerResult? result = await FilePicker.platform.pickFiles();
  21. if (result != null) {
  22. File selectedFile = File(result.files.single.path!);
  23. String? selectedDirectory = await FilePicker.platform
  24. .getDirectoryPath(dialogTitle: "Select Directory To Save The File");
  25. if (selectedDirectory == null) {
  26. ScaffoldMessenger.of(context).showSnackBar(
  27. const SnackBar(content: Text('No directory selected.')),
  28. );
  29. return;
  30. }
  31. File encryptedFile =
  32. await _fileHelper.fileEncrypt(selectedFile, _passwordController.text);
  33. String originalFileName = selectedFile.path.split('/').last;
  34. await FileHelper.saveToDirectory(
  35. encryptedFile, selectedDirectory, originalFileName + ".aes");
  36. ScaffoldMessenger.of(context).showSnackBar(
  37. SnackBar(content: Text('File encrypted successfully!')),
  38. );
  39. }
  40. }
  41. Future<void> _decryptFile() async {
  42. if (_passwordController.text.isEmpty) {
  43. ScaffoldMessenger.of(context).showSnackBar(
  44. SnackBar(content: Text('Password cannot be empty')),
  45. );
  46. return;
  47. }
  48. FilePickerResult? result = await FilePicker.platform.pickFiles();
  49. if (result != null) {
  50. File selectedFile = File(result.files.single.path!);
  51. String? selectedDirectory = await FilePicker.platform
  52. .getDirectoryPath(dialogTitle: "Select Directory To Save The File");
  53. if (selectedDirectory == null) {
  54. ScaffoldMessenger.of(context).showSnackBar(
  55. const SnackBar(content: Text('No directory selected.')),
  56. );
  57. return;
  58. }
  59. File decryptedFile =
  60. await _fileHelper.fileDecrypt(selectedFile, _passwordController.text);
  61. String originalFileName = selectedFile.path.split('/').last;
  62. originalFileName = originalFileName.replaceAll(".aes", "");
  63. await FileHelper.saveToDirectory(
  64. decryptedFile, selectedDirectory, originalFileName);
  65. ScaffoldMessenger.of(context).showSnackBar(
  66. SnackBar(content: Text('File decrypted successfully!')),
  67. );
  68. }
  69. }
  70. @override
  71. Widget build(BuildContext context) {
  72. return Scaffold(
  73. appBar: AppBar(
  74. title: Text('Sun crypt'),
  75. actions: [
  76. IconButton(
  77. icon: const Icon(Icons.info),
  78. onPressed: () {
  79. Navigator.pushNamed(context, '/about');
  80. },
  81. ),
  82. ],
  83. ),
  84. body: Padding(
  85. padding: const EdgeInsets.all(16.0),
  86. child: Column(
  87. children: [
  88. TextField(
  89. controller: _passwordController,
  90. decoration: InputDecoration(labelText: 'Password'),
  91. obscureText: true,
  92. ),
  93. SizedBox(height: 20),
  94. ElevatedButton(
  95. onPressed: _encryptFile,
  96. child: Text('Encrypt File'),
  97. ),
  98. SizedBox(height: 20),
  99. ElevatedButton(
  100. onPressed: _decryptFile,
  101. child: Text('Decrypt File'),
  102. ),
  103. ],
  104. ),
  105. ),
  106. );
  107. }
  108. }