//methods /// <summary> /// Method to encrypt a file using PGP via the GPG command line. /// </summary> /// <param name="sourcePath">File to be encrypted.</param> /// <param name="destinationPath">Path to encrypted file.</param> /// <param name="encryptionKey">Key to use for encryption. Use a recipient name in the GPG key store.</param> /// <returns></returns> public FileEncryptorResult CreateEncryptedFile(string sourcePath, string destinationPath, string encryptionKey) { FileEncryptorResult result = FileEncryptorResult.FileStatusUnknown; if (String.IsNullOrEmpty(sourcePath)) { throw new ArgumentNullException("The name of file that needs to be encrypted cannot be null or empty."); } if (String.IsNullOrEmpty(destinationPath)) { throw new ArgumentNullException("The name of the destination file for the encrypted data cannot be null or empty."); } result = RunGPGEncryptor(sourcePath, destinationPath, encryptionKey); return(result); }
private FileEncryptorResult RunGPGEncryptor(string sourcePath, string destinationPath, string encryptionKey) { FileEncryptorResult result = FileEncryptorResult.FileStatusUnknown; Process gpgEncryptorProcess = new Process(); try { _processExitCode = -1; _processMessages = string.Empty; this.Source = sourcePath; this.Destination = destinationPath; this.EncryptionKey = encryptionKey; gpgEncryptorProcess.StartInfo.WorkingDirectory = _gpgWorkingFolder; gpgEncryptorProcess.StartInfo.FileName = _gpgAppPath; gpgEncryptorProcess.StartInfo.Arguments = BuildEncryptionArguments(); this.ProcessCommandLine = GetCommandLine(gpgEncryptorProcess.StartInfo.Arguments); gpgEncryptorProcess.StartInfo.CreateNoWindow = true; gpgEncryptorProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; gpgEncryptorProcess.StartInfo.UseShellExecute = false; gpgEncryptorProcess.StartInfo.RedirectStandardOutput = true; gpgEncryptorProcess.StartInfo.RedirectStandardError = true; gpgEncryptorProcess.Start(); _msg.Length = 0; _msg.Append(gpgEncryptorProcess.StandardOutput.ReadToEnd()); _msg.Append(" "); _msg.Append(gpgEncryptorProcess.StandardError.ReadToEnd()); _processMessages = _msg.ToString(); gpgEncryptorProcess.WaitForExit(); _processExitCode = gpgEncryptorProcess.ExitCode; if (this.ProcessExitCode != 0) { result = FileEncryptorResult.EncryptionFailed; } else { result = FileEncryptorResult.EncryptedFileCreated; } } catch (System.Exception ex) { result = FileEncryptorResult.EncryptionFailed; _msg.Length = 0; _msg.Append("Error in RunGPGEncryptor method.\r\n"); _msg.Append(AppMessages.FormatErrorMessage(ex)); if (this.ProcessCommandLine.Length > 0) { _msg.Append("\r\nCommand line: "); _msg.Append(this.ProcessCommandLine); } if (this.ProcessMessages.Length > 0) { _msg.Append("\r\nProcess messages: "); _msg.Append(this.ProcessMessages); } throw new System.Exception(_msg.ToString()); } finally { ; } return(result); }