2

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?

user1828605
  • 1,575
  • 1
  • 19
  • 50
  • I think the key is "RaiseOnExited". I suspect the code runs correctly and fails when it finishes. Could be as simple as the return parameter is wrong, the code is trying to output a message and you didn't capture standard output, events are still running when exiting the application. – jdweng Jul 03 '15 at 20:57
  • Is there anything I could provide to help you determine where the problem lies? Either I didn't understand what you're saying, or I just don't know where to look to even begin to fix the problem. How can I find out where the code is trying to output any message - I just ran the program in my dev machine, but it didn't output anything anywhere. Where and what would you check to debug this? – user1828605 Jul 03 '15 at 21:20
  • 1
    You will have to debug this like any `NullReferenceException`. Certainly, there's not enough context in your question for someone here to answer it. GpgApi is clearly starting some external process to do some work and handles the `Process.Exited` event, but for some reason tries to dereference a `null` value in its handler (which is an anonymous method in the `GpgInterface.Execute()` method). But you need to debug the implementation of the handler and see what exactly is null and why. See the duplicate question for general tips on how to do that. – Peter Duniho Jul 03 '15 at 22:55
  • 1
    When connecting to a server you are running as a guest account which has limited access to the server. An executable can run if the exe is giving the correct privileges, but you will not be able to create any files on the server. I would start by checking Administrative Tools : Event Viewer on the server to see if an error was generated at the time the application ran. – jdweng Jul 04 '15 at 06:47
  • I looked at everything you mentioned. I removed GpgApi and used command line in c#, but it's still not running anything at all. – user1828605 Jul 07 '15 at 17:26
  • @DourHighArch the title of the question, please read the questions, comments, and the edits. I mentioned that I removed GpgApi and am using gpg decrypt as I would in the command line. I also tried to run other programs using the same method, but that didn't do anything either. – user1828605 Jul 08 '15 at 13:39
  • So, @jdweng, you're right. It had to do with the permission. Since, Default App Pool doesn't have access to run executable, I created an identity with domain account which allowed me to run the executable. It's working now. Thanks – user1828605 Jul 08 '15 at 17:49

1 Answers1

0

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.

Dmitry Polyakov
  • 1,342
  • 14
  • 23