我们从第三方接收 GPG 加密文件。我正在修改一个 C# 程序来查找加密文件、解密它们并删除加密文件。除了在解密部分提示输入短语外,它都可以正常工作;我知道密码短语,输入时有效。我需要在命令中传递密码,所以提示永远不会出现。
string CommandText = string.Format("echo {0}|gpg.exe --keyring {1} --secret-keyring {2} --batch --yes --passphrase-fd 0 -o {3} -d {4}",
passPhrase, publicKeyRingPath, secretKeyRingPath, outputFullPath, encryptedFilePath);
我也试过:
string CommandText = string.Format("gpg.exe --keyring {1} --secret-keyring {2} --batch --yes --passphrase {0} -o {3} -d {4}",
string CommandText = string.Format("gpg.exe --keyring {1} --secret-keyring {2} --batch --yes --passphrase-fd {0} -o {3} -d {4}",
以及其他几个变体。
这是为 Windows 2.1.0.57899 运行 GnuPG
如果问题出在其他地方,这里有一堆主要由我的前任编写的代码:
public bool decryptInputFile(string encryptedFilePath, string outputFullPath, out string message)
{
message = "decryptInputFile: Started";
try
{
ProcessStartInfo psi = new ProcessStartInfo("cmd.exe")
{
CreateNoWindow = true,
UseShellExecute = true,
RedirectStandardInput = true,
RedirectStandardOutput = true,
RedirectStandardError = true,
WorkingDirectory = decryptPath,
};
message = "decryptInputFile: PSI Initialized";
using (Process process = Process.Start(psi))
{
string CommandText = string.Format("echo {0}|gpg.exe --keyring {1} --secret-keyring {2} --batch --yes --passphrase-fd 0 -o {3} -d {4}",
passPhrase, publicKeyRingPath, secretKeyRingPath, outputFullPath, encryptedFilePath);
process.StandardInput.WriteLine(CommandText);
process.StandardInput.Flush();
process.StandardInput.Close();
process.WaitForExit();
process.Close();
process.Dispose();
message = "decryptInputFile: Success";
//These processes don't close and it keeps the file from being deleted.
foreach (Process P in Process.GetProcessesByName("gpg")) { P.Kill(); }
foreach (Process P in Process.GetProcessesByName("gpg2")) { P.Kill(); }
}
}
catch (Exception x)
{
// If there was an error, we're going to eat it and just let the user know we failed.
message = "decryptInputFile: Error: " + x.Message;
string errMessage = "ERROR: could not decrypt. " + x.Message + "\r\n";
File.AppendAllText(System.Configuration.ConfigurationSettings.AppSettings["LogPath"], errMessage);
return false;
}
if (File.Exists(outputFullPath) && File.Exists(encryptedFilePath))
{
File.Delete(encryptedFilePath);
}
return File.Exists(outputFullPath);
}