GPG自动解密密码传递

182*_*216 2 c# windows passphrase gnupg

我们从第三方收到GPG加密文件.我正在修改一个C#程序,它找到加密文件,解密它们并删除加密文件.这一切都有效,除了在解密部分它提示一个phassphrase; 我知道密码,它在输入时有效.我需要在命令中传递密码,因此提示永远不会出现.

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);
Run Code Online (Sandbox Code Playgroud)

我也尝试过:

    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}",
Run Code Online (Sandbox Code Playgroud)

以及其他几个变种.

这是运行GnuPG for Windows 2.1.0.57899

如果问题出在其他地方,那么主要是由我的前任编写的一堆代码:

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);
}
Run Code Online (Sandbox Code Playgroud)

Jen*_*rat 5

问题

你正在使用GnuPG 2,它只允许--passphrase*选项--batch.

运用 --batch

这些--passphrase*选项用于编写脚本.GnuPG 2限制它们(可能是为了慢慢弃用它们)到--batch模式,其中GnuPG不执行任何交互(例如,请求您的密码或其他"对话").

虽然这仍然可行,但最好使用密码预设gpg-agent,这样您就可以从应用程序代码中完全删除密码.注意--passphrase(只要GnuPG正在运行,系统上的所有用户都可以读取它)和--passphrase-file(密码短语存储在硬盘上,注意权限).

预设密码短语

使用GnuPG 2的首选方法是预设密码gpg-agent,GnuPG严重依赖密码; 在GnuPG 2.1的情况下,甚至完全独立地处理私钥和密码操作.

但是,为了你的救援,GnuPG 2带来了一个新工具gpg-preset-passphrase.在Debian Linux上,它隐藏起来/usr/lib/gnupg2/,我不知道它存储在Windows中的哪个位置.

来自man gpg-preset-passphrase:

gpg-preset-passphrase是一个实用程序,用于为gpg-agent带有密码的运行的内部缓存提供种子.它主要用于无人值守的机器,其中pinentry可能不使用通常的工具,并且在机器启动时给出要使用的密钥的密码.

[...]

gpg-preset-passphrase 以这种方式调用:

gpg-preset-passphrase [options] [command] cacheid
Run Code Online (Sandbox Code Playgroud)

cacheid是一个40个字符的十六进制字符键,用于标识应为其设置或清除密码的密钥.[...]

必须给出以下命令选项之一:

--preset
    Preset a passphrase. This is what you usually will use.
    gpg-preset-passphrase will then read the passphrase from stdin.
Run Code Online (Sandbox Code Playgroud)

总结一下,为你的应用程序初始化GnuPG(以及对应于配置的缓存时间的intervalls)运行gpg-preset-passphrase --preset [fingerprint],它将从stdin读取密码,或者另外使用一个--passphrase passphrase选项直接在查询中设置它.请注意,当同时使用echo或--passphrase方法时,其他系统用户可能会通过列出进程来获取密码; 最好直接从C#写入进程'stdin.