简体   繁体   中英

GPG encryption is working in console debug mode but not in release mode( window service)

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());  

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:

  • Logon as the service's user and import the keys.
  • If you're only performing encryption: import the key when starting up the application.
  • Define a fixed home directory for your application using the 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM