Ejemplo n.º 1
0
        //methods

        /// <summary>
        /// Method to encrypt a file using PGP via the GPG command line.
        /// </summary>
        /// <param name="sourcePath">Path to encrypted file.</param>
        /// <param name="destinationPath">Path to the decrypted file.</param>
        /// <param name="decryptionPassphrase">Password to be used to decrypt the file. Can be the password itself or a path to a file containing the password.</param>
        /// <returns></returns>
        public FileDecryptorResult CreateDecryptedFile(string sourcePath,
                                                       string destinationPath,
                                                       string decryptionPassphrase)
        {
            FileDecryptorResult result = FileDecryptorResult.FileStatusUnknown;


            if (String.IsNullOrEmpty(sourcePath))
            {
                throw new ArgumentNullException("The name of file that needs to be decrypted cannot be null or empty.");
            }
            if (String.IsNullOrEmpty(destinationPath))
            {
                throw new ArgumentNullException("The name of the destination file for the decrypted data cannot be null or empty.");
            }

            result = RunGPGDecryptor(sourcePath, destinationPath, decryptionPassphrase);


            return(result);
        }
Ejemplo n.º 2
0
        private FileDecryptorResult RunGPGDecryptor(string sourcePath,
                                                    string destinationPath,
                                                    string decryptionPassphrase)
        {
            FileDecryptorResult result = FileDecryptorResult.FileStatusUnknown;
            Process             gpgDecryptorProcess = new Process();

            try
            {
                _processExitCode          = -1;
                _processMessages          = string.Empty;
                this.Source               = sourcePath;
                this.Destination          = destinationPath;
                this.DecryptionPassphrase = decryptionPassphrase;

                gpgDecryptorProcess.StartInfo.WorkingDirectory = _gpgWorkingFolder;
                //oGPGDecryptorProcess.StartInfo.FileName = _sGPGAppPath;
                gpgDecryptorProcess.StartInfo.FileName  = _windowsCmdExePath;
                gpgDecryptorProcess.StartInfo.Arguments = BuildDecryptionArguments();
                this.ProcessCommandLine = GetCommandLine(gpgDecryptorProcess.StartInfo.Arguments);
                gpgDecryptorProcess.StartInfo.CreateNoWindow         = true;
                gpgDecryptorProcess.StartInfo.WindowStyle            = ProcessWindowStyle.Hidden;
                gpgDecryptorProcess.StartInfo.UseShellExecute        = false;
                gpgDecryptorProcess.StartInfo.RedirectStandardOutput = true;
                gpgDecryptorProcess.StartInfo.RedirectStandardError  = true;


                gpgDecryptorProcess.Start();
                _msg.Length = 0;
                _msg.Append(gpgDecryptorProcess.StandardOutput.ReadToEnd());
                _msg.Append(" ");
                _msg.Append(gpgDecryptorProcess.StandardError.ReadToEnd());
                _processMessages = _msg.ToString();
                gpgDecryptorProcess.WaitForExit();
                _processExitCode = gpgDecryptorProcess.ExitCode;

                if (this.ProcessExitCode != 0)
                {
                    result = FileDecryptorResult.DecryptionFailed;
                }
                else
                {
                    result = FileDecryptorResult.DecryptedFileCreated;
                }
            }
            catch (System.Exception ex)
            {
                result      = FileDecryptorResult.DecryptionFailed;
                _msg.Length = 0;
                _msg.Append("Error in RunGPGDecryptor 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);
        }