Reputation: 11
I am using gpg(GnuPG) to encrypt .csv file to .gpg file. The below code is generate encrypted file in debug mode. When I Install under windows service it’s throwing exception. “gpg: <>C:\emp.csv: skipped: No public key gpg: [stdin]: encryption failed: No public key”. Its working when I run service in debug mode like “consoleapp.exe -c”
string arguments = string.Format(" --yes --quiet --always-trust -e -o {0} -r \"{1}\" {2}", "C:\\emp.gpg", "KeyName", "C:\\emp.csv");
ProcessStartInfo pInfo = new ProcessStartInfo( @"C:\Program Files (x86)\GNU\GnuPG\gpg2", arguments );
pInfo.WorkingDirectory = @"C:\Program Files (x86)\GNU\GnuPG\";
pInfo.CreateNoWindow = false;
pInfo.UseShellExecute = false;
pInfo.RedirectStandardInput = true;
pInfo.RedirectStandardOutput = true;
pInfo.RedirectStandardError = true;
Process process = new Process()
{
StartInfo = pInfo,
EnableRaisingEvents = true
};
process.Start();
error = process.StandardError.ReadToEnd();
agent.LogConsole(process.StandardOutput.ReadToEnd());
Upvotes: 1
Views: 1143
Reputation: 38732
GnuPG manages per-user GnuPG home directories. If you import keys as a local user (while developing/debugging), they will be imported to your local user's home directory. If you later run it as system service, the user defined as service owner will possibly be another one and has no access to your local user's home directory.
Possible solutions:
GNUPGHOME
environment variable or --homedir
parameter. Be aware that GnuPG is by default rather picky on the folder's permissions, and if you're not very sure about the implications, don't change anything about this.Upvotes: 0