decryptFile static method

Future<void> decryptFile(
  1. String path,
  2. String password
)

Decrypt the file at args1 using the password at args2. The decrypted content is saved to a new file with _dec suffix. The original file remains unchanged. The decryption uses SecureCompressor from secure_compressor package. The output file is named ORIGINAL_NAME_dec.txt in the current directory.

Implementation

static Future<void> decryptFile(String path, String password) async {
  final file = File(path);
  if (!file.existsSync()) {
    print("❌ File not found: $path");
    return;
  }

  final encrypted = await file.readAsString();
  try {
    final decrypted = decrypt(encrypted, password);
    // Ambil nama file tanpa ekstensi
    final filename = path.split(Platform.pathSeparator).last;
    final base = filename.contains(".") ? filename.substring(0, filename.lastIndexOf(".")) : filename;

    final outFile = File("${Directory.current.path}${Platform.pathSeparator}${base}_dec.txt");
    await outFile.writeAsString(decrypted);

    print("✅ Decrypted -> ${outFile.path}");
  } catch (e) {
    print("❌ Wrong password or corrupted file.");
  }
}