c# - GnuPG 加密不适用于进程错误代码 2

标签 c# process gnupg

我有以下代码,当通过 Command Prompt 调用时,此加密代码可以正常工作(创建加密文件)当它在 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();

在 ExitCode 中返回错误代码 2

任何想法都会有所帮助!

最佳答案

来自 GPG Error Code 2 :

GPG is asking whether you want to continue on with the encryption using an unsigned key. Since no user can input Y it produces an error.

To fix this put the following switches

--yes and --always-trust

自从我使用 GPG 以来已经有一段时间了,但参数的顺序通常并不重要,只要它们被命名

这会给你这样的东西

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

注意:这是 C#6 格式,更容易阅读

关于c# - GnuPG 加密不适用于进程错误代码 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37025829/

相关文章:

c# - 如何通过 C# 中的按钮单击事件以编程方式启动 dsa.msc?

c# - FieldOptional FileHelpers 必须标记为可选,因为前一个字段已标记为可选

c# - 对超过 UInt16 屏障的程序集信息进行版本控制

c - 如何原子地执行一小段 C 代码

windows - 在 Windows 操作系统中打开文件时将默认程序重定向到另一个程序

c - 关于 close() 和 read() 的关系

github - 如何在 github 操作中使用 GPG key ?

gnupg - .gpg、.sig. 和 .asc 之间有什么有意义的区别?

java - logback:在滚动之前签署日志文件

c# - 以编程方式将 C# 变量传递给 Crystal Reports