2011-09-28 63 views
1

我正在尝试使用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); 
    } 
} 
+0

是你的代码或gnupg可执行文件错误的异常? –

+0

它来自gnupg可执行文件的错误。显示的错误消息是“GPGNET:[255]:未知的错误”。 – jaysuper

+0

我相信我的问题与网站运行的帐户有关,无法访问密钥环文件夹。应用程序池正在网络服务标识下运行。如果我将其更改为在我的用户帐户下运行,那么一切都按我预期的那样运行。 – jaysuper

回答

0

我能够通过创建用户解决我的问题的应用程序池标识为运行,并设置必要的权限来访问密钥环/ GPG程序。

0

我在Windows服务应用程序中使用此代码。文件传输加密和解密

但如果inputText的尺寸比大〜180 KB

_processObject.StandardInput.Write(inputText); 

这条线被锁定我的服务。

我不明白这个状态。请帮忙 !!!