使用 GnuPG 从 C# 进行 PGP 加密

发布于 2024-09-05 06:54:24 字数 771 浏览 2 评论 0原文

我正在尝试用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

花开半夏魅人心 2024-09-12 06:54:24

您可以做的第一件事就是删除重定向并亲自查看控制台,以便了解 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.

素食主义者 2024-09-12 06:54:24

尝试将 --passphrase-fd 0 添加到 GnuPG 命令行,指示它从标准输入读取密码。

Try adding --passphrase-fd 0 to the GnuPG command line to instruct it to read password from the standard input.

梦里人 2024-09-12 06:54:24

当我必须执行一些解密时,我遇到了同样的问题。我最终要做的就是像这样通过管道传输密码。

    procStartInfo.Arguments = @"/c echo " + passphrase + @"| c:\gnupg\gpg --passphrase-fd --     decrypt --output " + "\"" + fileLocationToDecryptTo + "\"" + " \"" + fileLocationToDecryptFrom     + "\"";

看起来效果相当不错。

I had the same problem when I was having to perform some decryption. What I wound up doing is Piping the passphrase like so.

    procStartInfo.Arguments = @"/c echo " + passphrase + @"| c:\gnupg\gpg --passphrase-fd --     decrypt --output " + "\"" + fileLocationToDecryptTo + "\"" + " \"" + fileLocationToDecryptFrom     + "\"";

It seems to work fairly well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文