Search code examples
asp.netencryptioniis-7.5windows-server-2008gnupg

GPG encryption in an ASP.NET C# Web application - No Public Key


I have set-up Gpg4win on Windows Server 2008 R2 and the website is running .Net 4.5.

I'm using the Starksoft OpenPGP dll.

I've added the required public key to Gpg4win via remote desktop, however when testing in the browser I get the following in the browser:

gpg: [email protected]: skipped: No public key gpg: [stdin]: encryption failed: No public key

I've tested locally on my machine and directly in GPA and Kleopatra on the server and the encryption is working correctly. This leads me to believe that the issue is with the public key being set-up via remote desktop and not being accessible to the application pool or similar.

I've tried copying the pubring.gpg, secring.gpg and trustdb.gpg in to a protected subfolder of the website as suggested somewhere (I forget where now) but this has not worked.

Any ideas how to set-up the public keys to be accessible to the IIS user?


Solution

  • Research

    Continued research lead me to this SO question: Gpg encryption over web browser which then lead me down the lines of running it via cmd - Running Command line from an ASPX page, and returning output to page

    Solution

    1. Export the required keys somewhere (in this case c:\public.key
    2. Create a page with the following code and execute it

      System.Diagnostics.Process si = new System.Diagnostics.Process();
      si.StartInfo.WorkingDirectory = "c:\\";
      si.StartInfo.UseShellExecute = false;
      si.StartInfo.FileName = "cmd.exe";
      si.StartInfo.Arguments = "gpg --import c:\\public.key";
      si.StartInfo.CreateNoWindow = false;
      si.StartInfo.RedirectStandardInput = true;
      si.StartInfo.RedirectStandardOutput = true;
      si.StartInfo.RedirectStandardError = true;
      si.Start();
      string output = si.StandardOutput.ReadToEnd();
      si.Close();
      
    3. The key now works :)