/// <summary> /// Encrypt a single file or all files in a folder /// </summary> /// <param name="filePath">full path of a file or folder</param> /// <param name="passcode">password to encrypt</param> private void EncryptFileOrFolder(string filePath, byte[] passcode) { List <string> allFiles = new List <string>(); FileAttributes attr = File.GetAttributes(filePath); if (!attr.HasFlag(FileAttributes.Directory)) { allFiles.Add(filePath); } else { string[] files = Directory.GetFiles(filePath, "*", SearchOption.AllDirectories); if (files.Length > 0) { allFiles.AddRange(files); } } IZXCryptor cryptor = new ZXCryptor(); foreach (string file in allFiles) { string fileExt = Path.GetExtension(file); if (String.Compare(fileExt, _encryptedFileExt, StringComparison.CurrentCultureIgnoreCase) == 0) // skip encrypted files { continue; } // set statusbar text CurrentProcessingFile = PathShortener(file, _shortFilePathLength); string outfile = file + _encryptedFileExt; try { cryptor.EncryptFile(file, outfile, passcode); } catch { if (File.Exists(outfile)) { File.Delete(outfile); } throw; } if (DeleteOldFile) { FileInfo fi = new FileInfo(file); fi.IsReadOnly = false; // permanent delete is not possible, see https://stackoverflow.com/questions/38935535/is-overwriting-a-file-multiple-times-enough-to-erase-its-data File.Delete(file); } } }
/// <summary> /// Decrypt a single file or all files in a folder /// </summary> /// <param name="filePath">full path of a file or folder</param> /// <param name="passcode">password to decrypt</param> private void DecryptFileOrFolder(string filePath, byte[] passcode) { List <string> allFiles = new List <string>(); FileAttributes attr = File.GetAttributes(filePath); if (!attr.HasFlag(FileAttributes.Directory)) { allFiles.Add(filePath); } else { string[] files = Directory.GetFiles(filePath, "*", SearchOption.AllDirectories); if (files.Length > 0) { allFiles.AddRange(files); } } IZXCryptor cryptor = new ZXCryptor(); foreach (string file in allFiles) { string fileExt = Path.GetExtension(file); if (String.Compare(fileExt, _encryptedFileExt, StringComparison.CurrentCultureIgnoreCase) != 0) // skip un-encrypted files { continue; } // set statusbar text CurrentProcessingFile = PathShortener(file, _shortFilePathLength); string outfile = Path.Combine(Path.GetDirectoryName(file), Path.GetFileNameWithoutExtension(file)); try { cryptor.DecryptFile(file, outfile, passcode); } catch { if (File.Exists(outfile)) { File.Delete(outfile); } throw; } if (DeleteOldFile) { FileInfo fi = new FileInfo(file); fi.IsReadOnly = false; File.Delete(file); } } }
private async void CryptFileCore(object arg) { byte[] passcode = null; byte[] key = null; try { if (!File.Exists(InputFile)) { await _dialogCoordinator.ShowMessageAsync(this, title : "Error", message : "Input file does not exist."); return; } if (!Directory.Exists(OutputDir)) { await _dialogCoordinator.ShowMessageAsync(this, title : "Error", message : "Output directory does not exist."); return; } string outputFileName = IsEncryption ? Path.GetFileName(InputFile) + _encryptedFileExt : Path.GetFileNameWithoutExtension(InputFile); string outputFilePath = Path.Combine(OutputDir, outputFileName); if (!OverwriteExistingFile && File.Exists(outputFilePath)) { var dlgSetting = new MetroDialogSettings() { AffirmativeButtonText = "Yes", NegativeButtonText = "No" }; string msg = String.Format("Output file '{0}' already exists. Do you want to overwrite it?", outputFileName); MessageDialogResult result = await _dialogCoordinator.ShowMessageAsync(this, "Overwrite File", msg, MessageDialogStyle.AffirmativeAndNegative, dlgSetting); if (result != MessageDialogResult.Affirmative) { return; } } if (IsEncryption) { if (!CompareSecureString(Password1, Password2)) { await _dialogCoordinator.ShowMessageAsync(this, title : "Password mismatch", message : "Please try again."); return; } } using (SecureStringWrapper wrapper = new SecureStringWrapper(Password1)) { if (!String.IsNullOrWhiteSpace(KeyFile)) { key = GetEncryptionKey(KeyFile); } passcode = MergeBytes(wrapper.ByteArray, key); } // encrypt/decrypt file IZXCryptor cryptor = new ZXCryptor(); if (IsEncryption) { cryptor.EncryptFile(InputFile, outputFilePath, passcode); } else { cryptor.DecryptFile(InputFile, outputFilePath, passcode); } // clear password and key if (passcode != null) { for (int i = 0; i < passcode.Length; i++) { passcode[i] = 0; } } if (key != null) { for (int i = 0; i < key.Length; i++) { key[i] = 0; } } if (IsEncryption) { await _dialogCoordinator.ShowMessageAsync(this, title : "File encrypted", message : "Output file: " + outputFilePath); } else { await _dialogCoordinator.ShowMessageAsync(this, title : "File decrypted", message : "Output file: " + outputFilePath); } } catch (Exception ex) { // clear password and key if (passcode != null) { for (int i = 0; i < passcode.Length; i++) { passcode[i] = 0; } } if (key != null) { for (int i = 0; i < key.Length; i++) { key[i] = 0; } } if (_dialogCoordinator != null) { await _dialogCoordinator.ShowMessageAsync(this, title : "Exception!", message : ex.Message); } } }
private async void OpenEncDecFileCore(object arg) { byte[] passcode = null; byte[] key = null; try { if (Mode == EncryptionMode.Encrypt) { if (!CompareSecureString(Password1, Password2)) { await _dialogCoordinator.ShowMessageAsync(this, title : "Password mismatch", message : "Please try again."); return; } } using (SecureStringWrapper wrapper = new SecureStringWrapper(Password1)) { if (!String.IsNullOrWhiteSpace(KeyFile)) { key = GetEncryptionKey(KeyFile); } //string pwdStr = wrapper.ClearText; //byte[] pwdBytes = wrapper.ByteArray; passcode = MergeBytes(wrapper.ByteArray, key); } // open/encrypt/decrypt file foreach (string file in FileList) { if (Mode == EncryptionMode.Encrypt) { EncryptFileOrFolder(file, passcode); } else if (Mode == EncryptionMode.Decrypt) { DecryptFileOrFolder(file, passcode); } else if (Mode == EncryptionMode.Open) { // check file extension if (String.Compare(Path.GetExtension(file), _encryptedFileExt, StringComparison.CurrentCultureIgnoreCase) != 0) { break; } string tmpFolder = Path.GetTempPath() + "zxcrypt\\"; tmpFolder += Path.GetRandomFileName().Replace(".", String.Empty); string outfile = Path.Combine(tmpFolder, Path.GetFileNameWithoutExtension(file)); try { IZXCryptor cryptor = new ZXCryptor(); cryptor.DecryptFile(file, outfile, passcode); // open file Process.Start(outfile); // launch ZXCryptMon, which is one instance app, to clean up the temp files once the above process close string dllPath = new Uri(Assembly.GetExecutingAssembly().CodeBase).LocalPath; string appFullPath = Path.GetDirectoryName(dllPath) + "\\ZXCryptMon.exe"; Process.Start(appFullPath); // for open, we will handle only one file break; } catch { Directory.Delete(tmpFolder, true); throw; } } } // clear password and key if (passcode != null) { for (int i = 0; i < passcode.Length; i++) { passcode[i] = 0; } } if (key != null) { for (int i = 0; i < key.Length; i++) { key[i] = 0; } } // everything is good, so close the window CloseWindow = true; } catch (Exception ex) { // clear password and key if (passcode != null) { for (int i = 0; i < passcode.Length; i++) { passcode[i] = 0; } } if (key != null) { for (int i = 0; i < key.Length; i++) { key[i] = 0; } } if (_dialogCoordinator != null) { await _dialogCoordinator.ShowMessageAsync(this, title : "Exception!", message : ex.Message); } } }