6

要求に応じてgpg署名を復号化するコンソールアプリケーションを作成しようとしています。GPGパスワードの入力を求められる部分を除いて、すべてが順調に進んでいます。gpg --decryptパスワードダイアログなしでコマンドラインから呼び出すにはどうすればよいですか?

これまでの私のコードは次のとおりです。

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ファイルにパスワードを入れないようにしたいと思います。

助けてくれてありがとう。

4

4 に答える 4

3

--batch --passphrase-fdオプションを一緒に使用します。例: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();
于 2014-06-02T13:56:10.850 に答える
2

もう少し掘り下げました。数ヶ月前、誰かがこれをGpg4Winのフォーラムのバグとして報告しました。現時点での唯一の解決策は、2.1.0から以前のバージョン(私の場合はオプションではありません)にロールバックするか、キーのパスワードを無効にするか、テキストからパイプすることです。フォーラムの投稿は次のとおりです。http ://wald.intevation.org/forum/forum.php?thread_id = 1116&forum_id = 21&group_id=11開発チームからのコメントはありません。

于 2013-03-26T00:19:57.437 に答える
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;
    }
于 2013-12-12T21:11:23.527 に答える
0

注:これを行うことはセキュリティリスクです!

ともかく、

対称暗号化を使用していない限り、署名または復号化するときのパスフレーズ。マニュアルページには、次のオプションが記載されています。


--パスフレーズ文字列

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

--passphrase-fd n

ファイル記述子nからパスフレーズを読み取ります。最初の行のみがファイル記述子nから読み取られます。パスフレーズに0を使用すると、stdinから読み取られます。これは、パスフレーズが1つだけ指定されている場合にのみ使用できます。

--passphrase-file file

ファイルファイルからパスフレーズを読み取ります。最初の行だけがファイルファイルから読み取られます。これは、パスフレーズが1つだけ指定されている場合にのみ使用できます。明らかに、ファイルに保存されているパスフレーズは、他のユーザーがこのファイルを読み取ることができる場合、セキュリティに問題があります。回避できる場合は、このオプションを使用しないでください。

公式のpgpコマンドラインユーティリティは、-zフラグを使用してこの機能を提供します。

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

于 2013-03-26T00:43:19.013 に答える