【问题标题】:Decrypting a GPG string from command line从命令行解密 GPG 字符串
【发布时间】:2013-03-25 21:09:35
【问题描述】:

我正在尝试编写一个控制台应用程序,该应用程序将根据请求解密 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 文件中。

提前感谢您的帮助。

【问题讨论】:

    标签: c# gnupg


    【解决方案1】:

    --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();
    

    【讨论】:

      【解决方案2】:

      我做了更多的挖掘工作。几个月前,有人在 Gpg4Win 的论坛上将此报告为一个错误。目前唯一的解决方案是从 2.1.0 回滚到以前的版本(在我的情况下不是一个选项),禁用密钥的密码,或者从文本中输入。这是论坛帖子:http://wald.intevation.org/forum/forum.php?thread_id=1116&forum_id=21&group_id=11 开发团队没有评论。

      【讨论】:

      【解决方案3】:

      为了避免对话框密码,试试这个方法,我用过,效果很好,你会发现更多细节。

      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;
          }
      

      【讨论】:

        【解决方案4】:

        注意:这样做存在安全风险!

        无论如何,

        签名或解密时的密码,除非您使用对称 加密。 手册页记录了以下选项:


        --密码字符串

        使用字符串作为密码。仅当仅提供一个密码时才能使用此功能。显然,这在多用户系统上具有非常可疑的安全性。不要使用 如果可以避免,请选择此选项。

        --passphrase-fd n

        从文件描述符 n 中读取密码。只有第一行将从文件描述符 n 中读取。如果您使用 0 作为密码,将从标准输入读取。这只能是 仅提供一个密码时使用。

        --passphrase-file file
        

        从文件文件中读取密码。只有第一行将从文件文件中读取。仅当仅提供一个密码时才能使用此功能。显然,存储的密码 如果其他用户可以读取该文件,则该文件的安全性存在问题。如果可以避免,请不要使用此选项。

        官方 pgp 命令行实用程序通过 -z 标志提供此功能:

        pgp -esa $SOURCEFILE $RECIPIENTPUBLICKEY -u $SENDERPRIVATEKEY -z $密码

        【讨论】:

        • 这是所有正确的信息,直接来自文档。我想我应该更清楚我已经阅读了文档,但是规定的解决方案--passphrase "MyFakePassword" 不起作用。原因可以在我的回答中找到。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-06-05
        • 2012-04-23
        • 2019-07-07
        • 1970-01-01
        相关资源
        最近更新 更多