2013-03-25 8 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をダウンロードしてインストールすると、問題が修正されました –

0

注:これはセキュリティリスクです!とにかく

パスフレーズあなたは対称 暗号化を使用している場合を除き、署名や復号化の際。 manページの文書は、次のオプション:


--passphrase文字列のパスフレーズとして

使用文字列。これは、パスフレーズが1つだけ提供されている場合にのみ使用できます。明らかに、これはマルチユーザシステムでは非常に疑わしいセキュリティです。回避することができる場合は、 このオプションを使用しないでください。 --passphrase-fdの

N

ファイルディスクリプタnからパスフレーズを読み込みます。最初の行だけがファイル記述子nから読み込まれます。 0を使用すると、パスフレーズはstdinから読み込まれます。パスフレーズが1つしか指定されていない場合は、 を使用できます。

--passphrase-file file 

ファイルファイルからパスフレーズを読み取ります。最初の行だけがファイルfileから読み込まれます。これは、パスフレーズが1つだけ提供されている場合にのみ使用できます。ファイルに が格納されているパスフレーズは、他のユーザーがこのファイルを読むことができれば疑いの余地はありません。このオプションを使用しないでください。

公式PGPのコマンドラインユーティリティは、-zフラグでこの機能を提供しています:

PGP -esa $ SOURCEFILE $ RECIPIENTPUBLICKEY -u $ SENDERPRIVATEKEY -z $ PASSPHRASE

+0

これはドキュメントに記載されているすべての正しい情報です。ドキュメントを読んだことがはっきりしていたはずですが、 '--passphrase" MyFakePassword "'という指定された解決策は動作しません。その理由は私の答えで見つけることができます。 – BilldrBot

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

proc.StandardInput.WriteLine(sCommandLine);これを追加した後、あなたのコードではgpg2 --batch --passphrase-fd 0 --armor --decrypt /path/to/encrypted_file.pgp

を.eg、一緒--batch --passphrase-fdのオプションを使用します。このため

proc.StandardInput.WriteLine("your passphrase here"); 
proc.StandardInput.Flush(); 
関連する問題