我正在尝试使用gpg4win 2.1.0来加密/解密两个不同网站之间传递的用户名。我可以让我的代码在我的开发机器上工作,但不能用于生产。如何使用GnuPG/GPG4Win从网站加密/解密?
我发展与VS 2008
的应用程序,我已运行GPG4Win安装包,选择了GNUPG &克列奥帕特拉。
我试图使用在“http://www.codeproject.com/KB/security/gnupgdotnet.aspx”找到的代码。
我总是收到错误代码255未知的错误。
以下代码示例是GPG应用程序的运行方式。该BuildOptions()
函数返回字符串:
--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 [email protected] --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);
}
}
是你的代码或gnupg可执行文件错误的异常? –
它来自gnupg可执行文件的错误。显示的错误消息是“GPGNET:[255]:未知的错误”。 – jaysuper
我相信我的问题与网站运行的帐户有关,无法访问密钥环文件夹。应用程序池正在网络服务标识下运行。如果我将其更改为在我的用户帐户下运行,那么一切都按我预期的那样运行。 – jaysuper