简体   繁体   中英

.exe processes are not running from a C# application when called

I wrote an app that, among other things, decrypts a file using GpgApi. I used GpgApi in C# to decrypt the file as follows:

 GpgInterface.ExePath = @"C:\Program Files (x86)\GNU\GnuPG\gpg.exe";

 GpgDecrypt decrypt = new GpgDecrypt(encryptedFile, file);
 decrypt.AskPassphrase = GetPassword;
 { 
    GpgInterfaceResult result = decrypt.Execute();
    Callback(result);
 }

I also installed gpg classic from https://www.gnupg.org/download/ , and the public and private keys already exist in the server.

Now, the application works flawlessy in my dev machine. It decrypts the file and uploads the data to the database server.

However, when I run the code in the server, I get the following error:

at GpgApi.GpgInterface.<Execute>b__e(Object sender, EventArgs args) at System.Diagnostics.Process.RaiseOnExited() at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading._ThreadPoolWaitOrTimerCallback.PerformWaitOrTimerCallback(Object state, Boolean timedOut)

I'm not sure what it means, the server has the public and private keys, and the gpg classic (the same version that's installed on my dev machine in the same path) is installed. Also, when I run the gpg --decrypt using command prompt, it decrypts the file without any issue. The GpgApi.dll is also provided in the bin folder. The application is actually a WCF service library. Any idea what I could be doing wrong?

The wcf service in the IIS application contains all the required .dlls. Everything else works except for Gpg decrypt.

EDIT:

This is what I have in Event Viewer:

Application: 61ReportsDecryptService.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.NullReferenceException
Stack:
   at GpgApi.GpgInterface.<Execute>b__e(System.Object, System.EventArgs)
   at System.Diagnostics.Process.OnExited()
   at System.Diagnostics.Process.RaiseOnExited()
   at System.Diagnostics.Process.CompletionCallback(System.Object, Boolean)
   at System.Threading._ThreadPoolWaitOrTimerCallback.WaitOrTimerCallback_Context(System.Object, Boolean)
   at System.Threading._ThreadPoolWaitOrTimerCallback.WaitOrTimerCallback_Context_f(System.Object)
   at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
   at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
   at System.Threading._ThreadPoolWaitOrTimerCallback.PerformWaitOrTimerCallback(System.Object, Boolean)

I checked for the file permission and even gave "Everyone" full control on the folder. That didn't work.

I then removed GpgApi, and started cmd process to decrypt the file. That didn't work either. The event viewer above is when I used GpgApi. Here's the code I used for decrypting.

    string encryptedFile = dataFileLocation + filename;
    filename = filename.ToString().Substring(0, filename.ToString().IndexOf(".gpg")).ToString();
    string file = dataFileLocation + filename; //@"C:\MMS\Data\" + filename + ".mdb";
    string sCommandLine = "C:\\Program Files (x86)\\GNU\\GnuPG\\gpg.exe --passphrase password --output \"" + file + "\" --decrypt \"" + encryptedFile + "\"";

    this.WriteToFile("starting command line decryption");

    System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo(@"cmd.exe");
    psi.CreateNoWindow = true;
    psi.UseShellExecute = false;
    psi.RedirectStandardOutput = true;
    psi.RedirectStandardInput = true;
    psi.RedirectStandardError = true;
    psi.WorkingDirectory = @"C:\Program Files (x86)\GNU\GnuPG";

    System.Diagnostics.Process process = System.Diagnostics.Process.Start(psi);

    string cmdLine = @"gpg.exe --passphrase-fd 0 -o C:\MMS\Data\test.mdb --decrypt C:\MMS\Data\filename.gpg";

    process.StandardInput.WriteLine(cmdLine);
    process.StandardInput.Flush();
    process.StandardInput.Close();
    process.WaitForExit();
    process.Close();

But for some reason it doesn't run at all. It doesn't tell me even if I don't supply the password, or if the password is wrong. It just doesn't do anything at all. I even had it in try-catch block, the exception never fired. If I copy-paste the above gpg.exe command to command prompt, then it works, but doesn't do anything when done from C#. What other options do I have left?

I had the same issue with decrypting files using GpgApi. It looks like this library isn't well-designed.

Finally I've used PgpSharp instead of GpgApi and it works fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM