GPG有两种常见的风格,一种需要命令行上的passphrase fd 0来告诉它从标准输入中读取密码,另一种则不需要,我忘了哪个版本需要它,但在文档IIRC中它很模糊。
一切都只是正确地调用子流程。这是我的一个节目的剪辑。
private string GPGEncrypt(string src)
{
FileInfo fi = new FileInfo(src);
string OutputName = GetEncryptedName(src);
if (File.Exists(OutputName))
{
if (!chkOverwrite.Checked)
{
SetStatus("Output file already exists - " + OutputName);
return "";
}
}
string path = fi.DirectoryName;
System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo(props.PGPExecutable);
psi.CreateNoWindow = true;
psi.UseShellExecute = false;
psi.RedirectStandardError = true;
psi.RedirectStandardInput = true;
psi.RedirectStandardOutput = true;
psi.WorkingDirectory = Path.GetDirectoryName(props.PGPExecutable);
string args = string.Format(
" --encrypt --recipient {0} --output \"{1}\" \"{2}\""
, txtEncryptID.Text.Trim()
, OutputName
, src
);
txtCommandLine.Text = args;
psi.Arguments = args;
System.Diagnostics.Process process = System.Diagnostics.Process.Start(psi);
process.WaitForExit();
int rc = process.ExitCode;
process.Close();
txtCommandLine.Text = string.Format("{0} exited with return code {1},", Path.GetFileName(props.PGPExecutable), rc);
return OutputName;
}