question

rohanpandole-8211 avatar image
0 Votes"
rohanpandole-8211 asked rohanpandole-8211 commented

I was trying to run GPG.exe from C# language but not able to get expected output

Hi all,

Trying to run a GPG decryption command from within C# but getting no output. C# Code is...

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 using System.Diagnostics;
    
 namespace GPGproject
 {
     class Program
     {
         public static void Main()
         {
             DecryptFile();
         }
          public static void DecryptFile()
          {
             string outputName = @"E:\GPGTest\testdecrypt26.txt";
             string inputName = @"E:\GPGTest\sampletest.pgp";
    
             string gpgHomeDir = @"C:\Program Files (x86)\GnuPG\bin";
             string arguments = "--homedir \"" + gpgHomeDir + "\" --passphrase-fd 0 " + " --output " + outputName + " --decrypt " + inputName;
             string path = @"C:\Program Files (x86)\GnuPG\bin\gpg.exe";
                                
    
        var procStartInfo = new ProcessStartInfo(path, arguments)
             {
                 WorkingDirectory = gpgHomeDir,
                 CreateNoWindow = true,
                 UseShellExecute = false,
                 RedirectStandardOutput = true,
                 RedirectStandardError = true,
                 RedirectStandardInput = true
             };
    
             var proc = new Process { StartInfo = procStartInfo };
             proc.Start();
             proc.StandardInput.WriteLine("passphrasetest1");
             proc.StandardInput.Flush();
    
             string result = proc.StandardOutput.ReadToEnd();
             string error = proc.StandardError.ReadToEnd();
    
             Console.WriteLine(result);
             Console.WriteLine(error);
         }
     }
 }

If I use the following arguments then I got this error :
string arguments = "--homedir \"" + gpgHomeDir + "\" --passphrase-fd 0 " + " --output " + outputName + " --decrypt " + inputName;
gpg: encrypted with RSA key, ID EB7CAC8D8297712C
gpg: decryption failed: No secret key


Or if I use the following arguments then I got pop up for passphrase, i dont want to enter passphrase from pop up, i only want to pass passphrase from program :
string arguments = "--passphrase-fd 0 " + " --output " + outputName + " --decrypt " + inputName;

![119541-image.png][1]



Please suggest to me where I did wrong code to pass passphrase from program






dotnet-csharpdotnet-aspnet-core-webapidotnet-aspnet-core-security
image.png (12.7 KiB)
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@rohanpandole-8211
There is no problem with the code. The problem lies with this tool, it should be an incorrect command that caused the current problem.
This is a third-party tool, and it does not seem to provide a dedicated forum to discuss related issues, I suggest you can go to StackOverflow to ask a question about it with the gnupg tag.

0 Votes 0 ·

OK, this issue is resolved by adding gpg option and value like --pinentry-mode loopback, that will not show popup for pinentry-qt

0 Votes 0 ·

0 Answers