This has been working correctly for me for years. You have to have GPG installed, of course. Recommend placing in try/catch block with logging.
Decrypt file:
public static string GPGDecryptFile(string encryptedSourceFile, string decryptedOutputFile = "", string passphrase = "")
{
ClearExceptionProperties();
if (!encryptedSourceFile.HasValueP4() || !File.Exists(encryptedSourceFile)) return null;
if (!decryptedOutputFile.HasValueP4()) decryptedOutputFile = Path.GetDirectoryName(encryptedSourceFile) + "\\" + Path.GetFileNameWithoutExtension(encryptedSourceFile);
if (!decryptedOutputFile.Contains(".")) decryptedOutputFile += ".txt";
GetConfigSettings();
if (!passphrase.HasValueP4()) passphrase = gpgPassphrase;
try
{
System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo("cmd.exe");
psi.CreateNoWindow = true;
psi.UseShellExecute = false;
psi.RedirectStandardError = true;
psi.RedirectStandardInput = true;
psi.RedirectStandardOutput = true;
psi.WorkingDirectory = Path.GetDirectoryName(gpgExePath);
string sCommandLine = string.Format("echo {1}|\"{0}\" --batch --passphrase-fd 0 -o \"{2}\" --decrypt \"{3}\"", gpgExePath, passphrase, decryptedOutputFile, encryptedSourceFile);
var process = System.Diagnostics.Process.Start(psi);
process.StandardInput.WriteLine(sCommandLine);
process.StandardInput.Flush();
process.StandardInput.Close();
process.WaitForExit();
int rc = process.ExitCode;
process.Close();
Debug.WriteLine(string.Format("{0} exited with return code {1}.", Path.GetFileName(gpgExePath), rc));
}
catch (Exception ex)
{
Debug.WriteLine(ex.ToString());
decryptedOutputFile = "";
LastExceptionInfo = string.Format("Error in ADO operation {0}. Error: {1}", new StackFrame().SFP4(), ex.Message);
LastException = ex;
}
return decryptedOutputFile;
}