代码之家  ›  专栏  ›  技术社区  ›  SidC

C#中的GNUPG解密[已关闭]

  •  1
  • SidC  · 技术社区  · 11 年前

    我的任务是解密从各种FTP站点检索到的pgp文件。我们在服务器上安装了GNUPG。我也评估过赏金城堡和StarkSoft。我并不是真的想购买商业解密软件。因此,我正在寻找使用Bouncy Castle或.NET Framework 4.5本身中的某些东西来完成此解密任务的方法。如果你已经使用GNUPG解密了pgp文件,请分享你的博客文章链接,或者提供一些关于你的方法的见解。

    1 回复  |  直到 6 年前
        1
  •  2
  •   Gary Walker    11 年前

    GPG有两种常见的风格,一种需要命令行上的passphrase fd 0来告诉它从标准输入中读取密码,另一种则不需要,我忘了哪个版本需要它,但在文档IIRC中它很模糊。

    一切都只是正确地调用子流程。这是我的一个节目的剪辑。

    private string GPGEncrypt(string src)
    {
      FileInfo fi = new FileInfo(src);
      string OutputName = GetEncryptedName(src);
      if (File.Exists(OutputName))
      {
        if (!chkOverwrite.Checked)
        {
          SetStatus("Output file already exists - " + OutputName);
          return "";
        }
      }
    
      string path = fi.DirectoryName;
    
      System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo(props.PGPExecutable);
      psi.CreateNoWindow = true;
      psi.UseShellExecute = false;
      psi.RedirectStandardError = true;
      psi.RedirectStandardInput = true;
      psi.RedirectStandardOutput = true;
      psi.WorkingDirectory = Path.GetDirectoryName(props.PGPExecutable);
    
      string args = string.Format(
        " --encrypt --recipient {0} --output \"{1}\" \"{2}\""
        , txtEncryptID.Text.Trim() // 1
        , OutputName // 2
        , src // 3
      );
    
      txtCommandLine.Text = args;
    
      psi.Arguments = args;
    
      System.Diagnostics.Process process = System.Diagnostics.Process.Start(psi);
      // process.StandardInput.WriteLine(CommandLine);
      // process.StandardInput.Flush();
      // process.StandardInput.Close();
      process.WaitForExit();
      int rc = process.ExitCode;
      process.Close();
    
      txtCommandLine.Text = string.Format("{0} exited with return code {1},", Path.GetFileName(props.PGPExecutable), rc);
    
      return OutputName;
    }