Decrypting a GPG string from command line
Solution 1
I did a bit more digging. A few months ago someone reported this as a bug on Gpg4Win's forums. The only solutions at this time are to roll back from 2.1.0 to a previous version (not an option in my case), disable the password for the key, or pipe it in from text. Here's the forum post: http://wald.intevation.org/forum/forum.php?thread_id=1116&forum_id=21&group_id=11 There is no comment from the development team.
Solution 2
Use the --batch --passphrase-fd
options together, .eg gpg2 --batch --passphrase-fd 0 --armor --decrypt /path/to/encrypted_file.pgp
In your code, after proc.StandardInput.WriteLine(sCommandLine);
add this:
proc.StandardInput.WriteLine("your passphrase here");
proc.StandardInput.Flush();
Solution 3
To avoid the dialog password try this method, I use it and it worked perfectly, you will find more details.
http://www.systemdeveloper.info/2013/11/decrypt-files-encrypted-with-gnupg-from.html
public static string DecryptFile(string encryptedFilePath)
{
FileInfo info = new FileInfo(encryptedFilePath);
string decryptedFileName = info.FullName.Substring(0, info.FullName.LastIndexOf('.')) + "Dec.TXT";
string encryptedFileName = info.FullName;
string password = System.Configuration.ConfigurationManager.AppSettings["passphrase"].ToString();
System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo("cmd.exe");
psi.CreateNoWindow = true;
psi.UseShellExecute = false;
psi.RedirectStandardInput = true;
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;
psi.WorkingDirectory = @System.Configuration.ConfigurationManager.AppSettings["WorkingDirectory"].ToString();
System.Diagnostics.Process process = System.Diagnostics.Process.Start(psi);
string sCommandLine = @"echo " + password + "|gpg.exe --passphrase-fd 0 --batch --verbose --yes --output " + decryptedFileName + @" --decrypt " + encryptedFileName;
process.StandardInput.WriteLine(sCommandLine);
process.StandardInput.Flush();
process.StandardInput.Close();
process.WaitForExit();
//string result = process.StandardOutput.ReadToEnd();
//string error = process.StandardError.ReadToEnd();
process.Close();
return decryptedFileName;
}
Comments
-
BilldrBot about 2 years
I'm trying to write a console application that will decrypt a gpg signature on request. Everything's going fine, EXCEPT for the part where it prompts for my GPG password. How do I call
gpg --decrypt
from the command line without a password dialog?Here's my code so far:
var startInfo = new ProcessStartInfo("gpg.exe"); startInfo.Arguments = "--decrypt"; //this is where I want to insert "--passphrase MyFakePassword" startInfo.CreateNoWindow = true; startInfo.UseShellExecute = false; startInfo.RedirectStandardInput = true; startInfo.RedirectStandardOutput = true; startInfo.RedirectStandardError = true; startInfo.WorkingDirectory = @"C:\Program Files (x86)\GNU\GnuPG"; var proc = Process.Start(startInfo); var sCommandLine = stringData + "\n"+(char)26+"\n"; //stringData is the encrypted string proc.StandardInput.WriteLine(sCommandLine); proc.StandardInput.Flush(); proc.StandardInput.Close(); var result = proc.StandardOutput.ReadToEnd();
I've tried using
--passphrase MyFakePassword
,--passphrase-fd MyFakePassword
and even--passphrase-fd 0
with my password on the first line of input. I'd like to avoid putting my password in a txt file on the machine that's running this code, if at all possible.Thanks in advance for any help.