We receive GPG encrypted files from a third party. I'm modifying a C# program that finds the encrypted files, decrypts them, and deletes the encrypted ones. It all works except during the decryption part it prompts for a phassphrase; I know the passphrase and it works when entered. I need to pass the passphrase in the command so the prompt never appears.
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);
I have also tried:
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}",
As well as several other variations.
This is running GnuPG for Windows 2.1.0.57899
In case the issues is elsewhere here is a bunch of code primarily written by my predecessor:
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 + "
";
File.AppendAllText(System.Configuration.ConfigurationSettings.AppSettings["LogPath"], errMessage);
return false;
}
if (File.Exists(outputFullPath) && File.Exists(encryptedFilePath))
{
File.Delete(encryptedFilePath);
}
return File.Exists(outputFullPath);
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…