C#重定向标准输入与PGP -ka命令

问题描述:

我有一个看起来非常愚蠢的问题。我一定错过了一些愚蠢的东西。我们的生产服务器上有一个PGP密钥环。为了安全起见,它所属的用户帐户不允许以交互方式登录。我们的问题是我们有时需要添加新的密钥,并且无法轻松完成。所以我们认为我们可以创建一个快速的控制台应用程序,它将作为它的ID运行,并通过命令行调用PGP命令。C#重定向标准输入与PGP -ka命令

该命令被调用,但它要求输入以确认我们在做什么。我们的问题是我们发送给standardinput的“y”从不显示,并且密钥未经验证。

这里是代码:

using System; 
using System.Collections.Generic; 
using System.Text; 
using System.IO; 
using System.Text.RegularExpressions; 
using System.DirectoryServices; 
using System.Threading; 

namespace TestConsoleApp 
{ 
    class RegExValidator 
    { 
     private System.Diagnostics.Process myProcess; 

     public RegExValidator() 
     { 
     } 

     public static void Main(string[] args) 
     { 
      RegExValidator myValidator = new RegExValidator(); 
      myValidator.InstallKeys("C:\\Test\\batch.asc", "batch.asc"); 
     } 


     private void InstallKeys(string keyPath, string keyName) 
     { 
      myProcess = new System.Diagnostics.Process(); 
      myProcess.StartInfo.RedirectStandardInput = true; 
      myProcess.StartInfo.CreateNoWindow = false; 
      myProcess.StartInfo.UseShellExecute = false; 
      myProcess.StartInfo.FileName = "pgp"; 
      myProcess.StartInfo.Arguments = "-ka " + keyPath + ""; 
      myProcess.Start(); 

      StreamWriter myInput = myProcess.StandardInput; 
      myInput.AutoFlush = true; 
      Thread.Sleep(3000); 

      myInput.WriteLine("y"); 

      myInput.WriteLine(Environment.NewLine); 

     } 

    } 

} 

这是我们得到的命令行输出。

C:\Test>TestConsoleApp.exe 
    Pretty Good Privacy(tm) Version 6.5.2 
    (c) 1999 Network Associates Inc. 
    Uses the BSafe(tm) Toolkit, which is copyright RSA Data Security, Inc. 
    Export of this software may be restricted by the U.S. government. 

    WARNING: Environmental variable TZ is not  defined, so GMT timestamps 
      may be wrong. See the PGP User's Guide to properly define TZ 

    Looking for new keys... 
    DSS 2048/1024 0xDE053A3D 2007/05/29 Batch Interface <[email protected]> 
    sig?   0xDE053A3D    (Unknown signator, can't be checked) 

    keyfile contains 1 new keys. Add these keys to keyring ? (Y/n) 
    C:\Test> 

任何人都可以帮忙吗?

感谢

编辑

我们尝试过这种方法,但不是我们的PGP刚搬到一个文件,我们得到了Y/N盒和工作。看起来你可能无法用PGP做到这一点。不知道为什么。

消息

keyfile contains 1 new keys. Add these keys to keyring ? (Y/n) 

建议用一个大写ÿ答复。请尝试更改您的来电:

myInput.WriteLine("Y"); 

(我没有PGP安装检查,但遇到了对的情况下坚持其他命令行界面。)

另一件事是尝试flushing stream buffers,它会清除所有缓冲区为流,并导致任何缓冲数据写入底层设备:

myInput.WriteLine("Y"); 
myInput.Flush(); 
+0

嗨吉梅尔,感谢您的答复,但它没有工作,我们得到完全相同的输出。 – Jon 2008-11-20 12:02:19