使用 GnuPG 从 C# 进行 PGP 加密
我正在尝试用 C# 加密传入文档,并且使用带有输入重定向的 GnuPG。我需要在一个步骤中使用 -se(签名和加密),这需要输入密码。但由于某种原因,输入重定向不起作用。感谢您的帮助。 控制将被阻止。 我不确定是否存在死锁或子进程(gpg.exe)等待输入。
pgpproc = new Process();
pgpproc.StartInfo.FileName = exeFilePath;
pgpproc.StartInfo.RedirectStandardOutput = false;
pgpproc.StartInfo.RedirectStandardInput = true;
pgpproc.StartInfo.UseShellExecute = false;
pgpproc.StartInfo.Arguments = "-a -o C:\PGPStaging\output.pgp -se -r recipientID C:\PGPStaging\input.txt";
pgpproc.StartInfo.CreateNoWindow = true;
pgpproc.Start();
StreamWriter myStreamWriter = pgpproc.StandardInput;
myStreamWriter.Write("*****");
myStreamWriter.Close();
if (pgpproc.WaitForExit(waittime))
{
//success
}
else
{
//failure
pgpproc.Kill();
pgpproc.WaitForExit();
}
`
I'm trying to encrypt a incoming document in C#, and I'm using GnuPG with input redirection. I need to use -se(sign and encrypt) in a single step, which requires entering passphrase. But for some reason, input redirection is not working. Appreciate your help.
Control is going to else block.
I'm not sure if there is deadlock or child process(gpg.exe) waiting for the input.
pgpproc = new Process();
pgpproc.StartInfo.FileName = exeFilePath;
pgpproc.StartInfo.RedirectStandardOutput = false;
pgpproc.StartInfo.RedirectStandardInput = true;
pgpproc.StartInfo.UseShellExecute = false;
pgpproc.StartInfo.Arguments = "-a -o C:\PGPStaging\output.pgp -se -r recipientID C:\PGPStaging\input.txt";
pgpproc.StartInfo.CreateNoWindow = true;
pgpproc.Start();
StreamWriter myStreamWriter = pgpproc.StandardInput;
myStreamWriter.Write("*****");
myStreamWriter.Close();
if (pgpproc.WaitForExit(waittime))
{
//success
}
else
{
//failure
pgpproc.Kill();
pgpproc.WaitForExit();
}
`
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以做的第一件事就是删除重定向并亲自查看控制台,以便了解 gnupg 正在做什么/等待什么。接下来,尝试在密码末尾添加换行符(\n 或 0x13 或 0x130x10,尝试所有变体) - 当您键入某些内容时,按 Enter 键传递它,并且您也必须在此处执行相同的操作。
最后,您可以使用 OpenPGPBlackbox 来执行操作,无需外部应用程序。
The first thing you can do is remove redirection and see the console yourself in order to get to know, what gnupg is doing / waiting. Next, try adding new-line character (\n or 0x13 or 0x130x10, try all variants) to the end of the password - when you type something, you press Enter to pass it, and you must do the same here as well.
Finally, you can use OpenPGPBlackbox to perform the operation without external applications.
尝试将
--passphrase-fd 0
添加到 GnuPG 命令行,指示它从标准输入读取密码。Try adding
--passphrase-fd 0
to the GnuPG command line to instruct it to read password from the standard input.当我必须执行一些解密时,我遇到了同样的问题。我最终要做的就是像这样通过管道传输密码。
看起来效果相当不错。
I had the same problem when I was having to perform some decryption. What I wound up doing is Piping the passphrase like so.
It seems to work fairly well.