GnuPG Encryption Does Not Work with Code 2 Process Error

I have the code below, where this encryption code works fine (creating an encrypted file) when it is called through Command Prompt, and the same one does not work when it is in Console Application.

var destFilePath = @"D:\test.gpg";
var recipient = "test@test.com";
var sourceFilePath = @"D:\test.txt";
var proc = new Process
           {
                StartInfo = new ProcessStartInfo
                {
                    FileName = "cmd.exe",
                    Arguments = string.Format("gpg2 --output {0} --encrypt --recipient {1} {2}",
                                                destFilePath, recipient, sourceFilePath),
                    UseShellExecute = false,
                    RedirectStandardError = true,
                    RedirectStandardInput = true,
                    RedirectStandardOutput = true,
                    CreateNoWindow = true,
                    WorkingDirectory = @"C:\Program Files (x86)\GNU\GnuPG"
                }
            };

            proc.Start();
            proc.WaitForExit();
            int rc = proc.ExitCode;
            proc.Close();
            Console.WriteLine(rc.ToString());
            Console.ReadKey();

which returns error code 2 in ExitCode

Any ideas will help!

+4
source share
1 answer

from GPG 2 error code :

GPG asks if you want to continue encryption using an unsigned key. Since the user cannot enter Y, he throws an error.

To fix this, select the following switches

--yes and --always-trust

, GPG, ,

-

FileName = "gpg2.exe",
Arguments = $"--output {destFilePath} --encrypt --yes --always-trust --recipient {recipient} {sourceFilePath}",

: # 6,

+1

All Articles