开发者

How do I Encrypt/Decrypt using GnuPG/GPG4Win from a website?

I am attempting to encrypt/decrypt user names to pass between two different websites using gpg4win 2.1.0. I can get my code to work on my development machine, but not in production.

I'm developing the application with VS 2008.

I have run the GPG4Win install package, chose gnupg & kleopatra.

I've attempted to use the code found at "http://www.codeproject.com/KB/security/gnupgdotnet.aspx".

I keep getting Error Code 255 Unknown error.

The following code sample is how the GPG application is being run. The BuildOptions() function returns the string:

--homedir "C:\Path\gnupg" --no-auto-check-trustdb --always-trust --no-default-keyring --secret-keyring "C:\Path\gnupg\secring.gpg" --keyring "C:\Path\gnupg\pubring.gpg" --yes --batch --encrypt --armor --recipient recipient@email.ca --passphrase-fd 0 --no-verbose

public void ExecuteCommand(string inputText, out string outputText)
{
  outputText = "";

  string gpgOptions = BuildOptions();
  string gpgExecutable = GetGpgPath();
  binddirectory = GetGPGInstallLocation();

  // TODO check existence of _bindirectory and gpgExecutable

  // Create startinfo object

  ProcessStartInfo pInfo = new ProcessStartInfo(gpgExecutable, gpgOptions);
  pInfo.WorkingDirectory = _bindirectory;
  pInfo.CreateNoWindow = true;
  pInfo.UseShellExecute = false;
  // Redirect everything: 
  // stdin to send the passphrase, stdout to get encrypted message, stderr in case of errors...
  pInfo.RedirectStandardInput = true;
  pInfo.RedirectStandardOutput = true;
  pInfo.RedirectStandardError = true;
  _processObject = Process.Start(pInfo);

  // Send pass phrase, if any
  if (!string.IsNullOrEmpty(_passphrase))
  {
    _processObject.StandardInput.WriteLine(_passphrase);
    _processObject.StandardInput.Flush();
  }

  // Send input text
  _processObject.StandardInput.Write(inputText);
  _processObject.StandardInput.Flush();
  _processObject.StandardInput.Close();

  _outputString = "";
  _errorString = "";

  // Create two threads to read both output/error streams without creating a deadlock
  ThreadStart outputEntry = new ThreadStart(StandardOutputReader);
  Thread outputThread = new Thread(outputEntry);
  outputThread.Start();
  ThreadStart errorEntry = new ThreadStart(StandardErrorReader);
  Thread errorThread = new Thread(errorEntry);
  errorThread.Start();

  if (_processObject.WaitForExit(ProcessTimeOutMilliseconds))
  {
     // Process exited before timeout...
   开发者_运维技巧  // Wait for the threads to complete reading output/error (but use a timeout!)
     if (!outputThread.Join(ProcessTimeOutMilliseconds/2))
       outputThread.Abort();

     if (!errorThread.Join(ProcessTimeOutMilliseconds/2))
       errorThread.Abort();
  }
  else
  {
    // Process timeout: PGP hung somewhere... kill it (as well as the threads!)
    _outputString = "";
    _errorString = "Timed out after " + ProcessTimeOutMilliseconds.ToString() + " milliseconds";
    _processObject.Kill();
    if (outputThread.IsAlive)
      outputThread.Abort();

    if (errorThread.IsAlive)
      errorThread.Abort();
  }

  // Check results and prepare output
  _exitcode = _processObject.ExitCode;
  if (_exitcode == 0)
    outputText = _outputString;
  else
  {
    if (_errorString == "")
    {
      _errorString = "GPGNET: [" + _processObject.ExitCode.ToString() + "]: Unknown error";
    }
    throw new GnuPGException(_errorString);
  }
}


I was able to resolve my issue by creating a user for the app pool identity to run as and set the necessary permissions to access the keyring/gpg program.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜