2008-11-20 51 views
0

我有一個看起來非常愚蠢的問題。我一定錯過了一些愚蠢的東西。我們的生產服務器上有一個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做到這一點。不知道爲什麼。

回答

1

消息

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