使用GnuPG从C#进行PGP加密

3

我正在尝试使用C#加密传入的文档,使用GnuPG进行输入重定向。我需要在单个步骤中使用-se(签名和加密),这需要输入密码。但出于某些原因,输入重定向不起作用。感谢您的帮助。 控制流程进入else块。 我不确定是否存在死锁或子进程(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();
}

`

3个回答

1

你可以首先移除重定向并查看控制台,以了解 gnupg 正在做什么/等待什么。接下来尝试在密码末尾添加换行符(\n或0x13或0x130x10,尝试所有变体) - 当你输入密码时,你按Enter键传递它,这里也必须这样做。最后,你可以使用 OpenPGPBlackbox 来执行操作而无需外部应用程序。


1

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


0

当我需要执行一些解密操作时,我遇到了同样的问题。最终我做的是像这样传递密码:

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

看起来运行得相当不错。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接