0

I am using gpg encryption decryption, which is working fine on my local system (while running on VS2015) but it fails when I host it to server/or try running it from local IIS. It doesnt prompt for any runtime error, but dont create the decrypted file. here is the command:

gpg --local-user XXXXXT21XXX --output C:\Projects\myserver.api\Server_Files\input\callback_7a7ac0e3-89b1-4749-8d2a-d40cc84a27d5.xml --decrypt C:\Projects\server.api\Server_Files\Serverinput\callback_7a7ac0e3-89b1-4749-8d2a-d40cc84a27d5.asc

code I am using for decryption:

public string Decrypt(string inputFilePath, string extension = "")
        {
            try
            {
                var process = GetCmd();
                string outputFilePath = $@"{FileHelper.InputPath}\{Path.GetFileNameWithoutExtension(inputFilePath)}{extension}";

                string sCommandLine = $"{command}{string.Format(decCommand, outputFilePath, inputFilePath)}";

                process.StandardInput.WriteLine(sCommandLine);
                process.StandardInput.Flush();
                process.StandardInput.Close();
                process.WaitForExit();
                process.Close();
                return outputFilePath;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
 private static System.Diagnostics.Process GetCmd()
        {
            System.Diagnostics.ProcessStartInfo psi =
                 new System.Diagnostics.ProcessStartInfo("cmd.exe");
            psi.CreateNoWindow = false;
            psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
            psi.UseShellExecute = false;
            psi.RedirectStandardInput = true;
            psi.RedirectStandardOutput = true;
            psi.RedirectStandardError = true;
            psi.Verb = "runas";
            psi.WorkingDirectory = @"C:\Program Files (x86)\GnuPG\bin";

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

            return process;
        }

the error :

System.IO.FileNotFoundException: Could not find file 'C:\Projects\Server.api\Server_Files\input\callback_7a7ac0e3-89b1-4749-8d2a-d40cc84a27d5.xml'.
    File name: 'C:\Projects\Server_if.api\Server_Files\input\callback_7a7ac0e3-89b1-4749-8d2a-d40cc84a27d5.xml'
       at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
       at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
       at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
       at System.IO.StreamReader..ctor(String path, Encoding encoding, Boolean detectEncodingFromByteOrderMarks, Int32 bufferSize, Boolean checkHost)
       at System.IO.File.InternalReadAllText(String path, Encoding encoding, Boolean checkHost)
       at Server.Common.Helpers.FileHelper.ReadAllText(String path) in D:\Projects\TFT\Bitbucket_PAYDO\Server_new\Server.Common\Helpers\FileHelper.cs:line 101
       at Server.API.Controllers.ServerTransferController.<GetServerCallback>d__9.MoveNext()

How can I run the gpg decrypt command without prompting for passphrase?

James Z
  • 12,209
  • 10
  • 24
  • 44
Pankaj Nema
  • 165
  • 2
  • 13
  • The error states that it cannot find the file `C:\Projects\Server.api\Server_Files\input\callback_7a7ac0e3-89b1-4749-8d2a-d40cc84a27d5.xml`. Are you sure it exists on the server and the executing user has access to it? Also what you mean with "_It doesnt prompt for any runtime error_"? Because you listed an error message in your question. – RMH Feb 27 '18 at 10:24
  • actually this xml is created once it successfully decrypt the file, but since it is not decrypting it, the xml is not created and the FileNotFound error occurs, so for me, the decryption is the problem, not the file not found. – Pankaj Nema Feb 27 '18 at 10:30
  • @PankajNemi have you tried reading the [StandardOuput](https://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardoutput(v=vs.110).aspx) and [StandardError](https://msdn.microsoft.com/en-us/library/system.diagnostics.process.standarderror(v=vs.110).aspx) readers? You can also check the [ExitCode](https://msdn.microsoft.com/en-us/library/system.diagnostics.process.exitcode(v=vs.110).aspx) to see if GPG maybe prints out some information there? – RMH Feb 27 '18 at 10:49
  • I tried with the same, it shows this as error: gpg: encrypted with RSA key, ID 85857DC18B6ACE7B gpg: decryption failed: No secret key – Pankaj Nema Feb 27 '18 at 11:09
  • it is working on local and when I run the solution from VisualStudio, but not with IIS, I was hoping there is a permission issue, and have granted full control to everyone, but it is still the same. – Pankaj Nema Feb 27 '18 at 11:10
  • Your problem is in the GPG part, not so much in the C# code as it seems to look fine (as an improvement you could throw an exception with the data from the StandardOutput if the output file is missing for easier debugging in the future). – RMH Feb 27 '18 at 12:06
  • 2
    Based off [this question](https://stackoverflow.com/questions/28321712/gpg-decryption-fails-with-no-secret-key-error) it seems that your secret key is missing on the new server (or under the right user). It has an accepted answer can you see if that helps you in any way? Also, note that IIS (by default) runs under a system user while IIS Express usually runs under your own user. If the keyring differs per user, that might be your issue. – RMH Feb 27 '18 at 12:08

1 Answers1

0

I found the answer myself, As directed by @RMH, I tried to found the right user for gpg, in my case it was Administrator. What I did was I created a self hosted web api and tried the same code there, and amazingly it started working.

Pankaj Nema
  • 165
  • 2
  • 13