サードパーティから GPG 暗号化ファイルを受け取ります。暗号化されたファイルを見つけて復号化し、暗号化されたファイルを削除する C# プログラムを変更しています。復号化部分でフェーズフレーズを要求する場合を除いて、すべて機能します。パスフレーズを知っていて、入力すると機能します。プロンプトが表示されないように、コマンドでパスフレーズを渡す必要があります。
string CommandText = string.Format("echo {0}|gpg.exe --keyring {1} --secret-keyring {2} --batch --yes --passphrase-fd 0 -o {3} -d {4}",
passPhrase, publicKeyRingPath, secretKeyRingPath, outputFullPath, encryptedFilePath);
私も試しました:
string CommandText = string.Format("gpg.exe --keyring {1} --secret-keyring {2} --batch --yes --passphrase {0} -o {3} -d {4}",
string CommandText = string.Format("gpg.exe --keyring {1} --secret-keyring {2} --batch --yes --passphrase-fd {0} -o {3} -d {4}",
他のいくつかのバリエーションと同様に。
これは Windows 2.1.0.57899 用の GnuPG を実行しています
問題が別の場所にある場合に備えて、主に私の前任者によって書かれた一連のコードを次に示します。
public bool decryptInputFile(string encryptedFilePath, string outputFullPath, out string message)
{
message = "decryptInputFile: Started";
try
{
ProcessStartInfo psi = new ProcessStartInfo("cmd.exe")
{
CreateNoWindow = true,
UseShellExecute = true,
RedirectStandardInput = true,
RedirectStandardOutput = true,
RedirectStandardError = true,
WorkingDirectory = decryptPath,
};
message = "decryptInputFile: PSI Initialized";
using (Process process = Process.Start(psi))
{
string CommandText = string.Format("echo {0}|gpg.exe --keyring {1} --secret-keyring {2} --batch --yes --passphrase-fd 0 -o {3} -d {4}",
passPhrase, publicKeyRingPath, secretKeyRingPath, outputFullPath, encryptedFilePath);
process.StandardInput.WriteLine(CommandText);
process.StandardInput.Flush();
process.StandardInput.Close();
process.WaitForExit();
process.Close();
process.Dispose();
message = "decryptInputFile: Success";
//These processes don't close and it keeps the file from being deleted.
foreach (Process P in Process.GetProcessesByName("gpg")) { P.Kill(); }
foreach (Process P in Process.GetProcessesByName("gpg2")) { P.Kill(); }
}
}
catch (Exception x)
{
// If there was an error, we're going to eat it and just let the user know we failed.
message = "decryptInputFile: Error: " + x.Message;
string errMessage = "ERROR: could not decrypt. " + x.Message + "\r\n";
File.AppendAllText(System.Configuration.ConfigurationSettings.AppSettings["LogPath"], errMessage);
return false;
}
if (File.Exists(outputFullPath) && File.Exists(encryptedFilePath))
{
File.Delete(encryptedFilePath);
}
return File.Exists(outputFullPath);
}