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

Rohan Pandole 1 Reputation point
2021-07-30T20:25:28.227+00:00

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

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,138 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,245 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,201 questions
{count} votes

1 answer

Sort by: Most helpful
  1. byron ajcalon lopez 1 Reputation point
    2022-02-08T18:14:13.37+00:00

    string arguments = "--passphrase-fd 0 " + " --output " + outputName + " --decrypt --pinentry-mode=loopback --passphrase=YourPassphrase " + inputName;

    0 comments No comments