2013-03-25 161 views
6

我試圖編寫一個控制檯應用程序,該應用程序將根據請求解密gpg簽名。一切都很好,除了它提示輸入我的GPG密碼的部分。如何在沒有密碼對話框的情況下從命令行調用gpg --decrypt從命令行解密GPG字符串

這裏是到目前爲止我的代碼:

var startInfo = new ProcessStartInfo("gpg.exe"); 
startInfo.Arguments = "--decrypt"; //this is where I want to insert "--passphrase MyFakePassword" 
startInfo.CreateNoWindow = true; 
startInfo.UseShellExecute = false; 
startInfo.RedirectStandardInput = true; 
startInfo.RedirectStandardOutput = true; 
startInfo.RedirectStandardError = true; 
startInfo.WorkingDirectory = @"C:\Program Files (x86)\GNU\GnuPG"; 

var proc = Process.Start(startInfo); 
var sCommandLine = stringData + "\n"+(char)26+"\n"; //stringData is the encrypted string 
proc.StandardInput.WriteLine(sCommandLine); 
proc.StandardInput.Flush(); 
proc.StandardInput.Close(); 

var result = proc.StandardOutput.ReadToEnd(); 

我使用--passphrase MyFakePassword--passphrase-fd MyFakePassword甚至與我輸入的第一行密碼--passphrase-fd 0嘗試。如果可能的話,我想避免將密碼放在運行此代碼的機器上的txt文件中。

在此先感謝您的幫助。

回答

2

我做了更多的挖掘。幾個月前,有人將此作爲Gpg4Win論壇上的一個錯誤報告。目前唯一的解決方案是從2.1.0回滾到以前的版本(在我的情況下不是選項),禁用密鑰或從文本管道輸入密鑰。這裏是論壇帖子:http://wald.intevation.org/forum/forum.php?thread_id=1116&forum_id=21&group_id=11沒有來自開發團隊的評論。

+0

感謝這個!浪費時間在它上。下載ftp://ftp.gnupg.org/gcrypt/binary/gnupg-w32cli-1.4.9.exe並安裝它解決了我的問題 – 2013-08-15 08:27:53

0

注意:這樣做是安全風險!

反正

密碼簽名或解密除非您使用對稱加密 時。 手冊頁文檔以下選項:


--passphrase串

使用字符串作爲密碼。只有在只提供了一個密碼短語時才能使用。顯然,這在多用戶系統上的安全性非常可疑。如果您可以避免使用此選項,請勿使用 。

--passphrase-FDň

閱讀從文件描述符n密碼。只有第一行將從文件描述符n中讀取。如果您使用0作爲密碼,將從標準輸入讀取。如果僅提供一個密碼短語,則只能使用 。

--passphrase-file file 

從文件文件中讀取密碼。只有第一行將從文件文件中讀取。只有在只提供了一個密碼短語時才能使用。顯然,如果其他用戶可以讀取該文件,則在文件中存儲的密碼短語 具有可疑的安全性。如果可以避免使用此選項,請勿使用此選項。

官方PGP的命令行實用程序提供此功能與-z標誌:

PGP -esa $ $的資源文件-u RECIPIENTPUBLICKEY $ SENDERPRIVATEKEY -z $ PASSPHRASE

+0

這是所有正確的信息,直接從文檔。我想我應該已經更清楚我已閱讀文檔,但規定的解決方案「--passphrase」MyFakePassword「」不起作用。原因可以在我的答案中找到。 – BilldrBot 2013-03-26 01:49:52

1

爲了避免對話密碼嘗試這種方法,我使用它,它的工作完美,你會發現更多的細節。

http://www.systemdeveloper.info/2013/11/decrypt-files-encrypted-with-gnupg-from.html

public static string DecryptFile(string encryptedFilePath) 
    { 
     FileInfo info = new FileInfo(encryptedFilePath); 
     string decryptedFileName = info.FullName.Substring(0, info.FullName.LastIndexOf('.')) + "Dec.TXT"; 
     string encryptedFileName = info.FullName; 

     string password = System.Configuration.ConfigurationManager.AppSettings["passphrase"].ToString(); 

     System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo("cmd.exe"); 

     psi.CreateNoWindow = true; 
     psi.UseShellExecute = false; 
     psi.RedirectStandardInput = true; 
     psi.RedirectStandardOutput = true; 
     psi.RedirectStandardError = true; 
     psi.WorkingDirectory = @System.Configuration.ConfigurationManager.AppSettings["WorkingDirectory"].ToString(); 

     System.Diagnostics.Process process = System.Diagnostics.Process.Start(psi); 
     string sCommandLine = @"echo " + password + "|gpg.exe --passphrase-fd 0 --batch --verbose --yes --output " + decryptedFileName + @" --decrypt " + encryptedFileName; 

     process.StandardInput.WriteLine(sCommandLine); 
     process.StandardInput.Flush(); 
     process.StandardInput.Close(); 
     process.WaitForExit(); 
     //string result = process.StandardOutput.ReadToEnd(); 
     //string error = process.StandardError.ReadToEnd(); 
     process.Close(); 
     return decryptedFileName; 
    } 
3

使用--batch --passphrase-fd選項一起,.eg gpg2 --batch --passphrase-fd 0 --armor --decrypt /path/to/encrypted_file.pgp

在你的代碼,proc.StandardInput.WriteLine(sCommandLine);後補充一點:

proc.StandardInput.WriteLine("your passphrase here"); 
proc.StandardInput.Flush();