加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 从命令行解密GPG字符串

发布时间:2020-12-15 08:42:03 所属栏目:百科 来源:网络整理
导读:我正在尝试编写一个控制台应用程序,它将根据请求解密gpg签名.一切都很顺利,除了它提示我的GPG密码的部分.如何在没有密码对话框的情况下从命令行调用gpg –decrypt? 到目前为止,这是我的代码: var startInfo = new ProcessStartInfo("gpg.exe");startInfo.A
我正在尝试编写一个控制台应用程序,它将根据请求解密gpg签名.一切都很顺利,除了它提示我的GPG密码的部分.如何在没有密码对话框的情况下从命令行调用gpg –decrypt?

到目前为止,这是我的代码:

var startInfo = new ProcessStartInfo("gpg.exe");
startInfo.Arguments = "--decrypt"; //this is where I want to insert "--passphrase MyFakePassword"
startInfo.CreateNoWindow = true;
startInfo.UseShellExecute = false;
startInfo.RedirectStandardInput = true;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.WorkingDirectory = @"C:Program Files (x86)GNUGnuPG";

var proc = Process.Start(startInfo);
var sCommandLine = stringData + "n"+(char)26+"n"; //stringData is the encrypted string
proc.StandardInput.WriteLine(sCommandLine); 
proc.StandardInput.Flush();
proc.StandardInput.Close();

var result = proc.StandardOutput.ReadToEnd();

我尝试在输入的第一行使用–passphrase MyFakePassword,– passphrase-fd MyFakePassword,甚至–passphrase-fd 0和我的密码.如果可能的话,我想避免将我的密码放在运行此代码的机器上的txt文件中.

在此先感谢您的帮助.

解决方法

同时使用–batch –passphrase-fd选项,.eg gpg2 –batch –passphrase-fd 0 –armor –decrypt /path/to/encrypted_file.pgp

在你的代码中,在proc.StandardInput.WriteLine(sCommandLine)之后;添加这个:

proc.StandardInput.WriteLine("your passphrase here");
proc.StandardInput.Flush();

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读