【问题标题】:GPG error usage: gpg [options] [filename] during gpg --decrypt [closed]GPG 错误用法:gpg [options] [filename] 在 gpg --decrypt [关闭]
【发布时间】:2014-08-04 11:03:43
【问题描述】:

我尝试使用 GPG 命令解密我的文件:

        FileInfo info = new FileInfo(@"C:\Users\***\Desktop\files\file.pgp");

        string decryptedFileName = info.FullName.Substring(0, info.FullName.LastIndexOf('.')) + "Dec.TXT";
        string encryptedFileName = info.FullName;

        string password = "pass";

        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 = @"C:\Program Files (x86)\GNU\GnuPG";
        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;
        string sCommandLine = @"gpg --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(); 

不幸的是,我收到以下错误:“usage: gpg [options] [filename]\r\n” 知道有什么问题吗?

【问题讨论】:

  • 转储sCommandLine,可能两个文件名之一为空或严重转义。
  • 是的,你是对的。我将文件名更改为: string decryptedFileName = "file_enc.txt"; string encryptedFileName = "file.pgp";现在它可以正常工作了,谢谢!
  • 我再次发布了评论作为答案(显然是),并提供了更多详细信息,因此您可以接受。

标签: c# encryption cmd gnupg


【解决方案1】:

错误信息表示传递的参数有误。两个字符串中的一个未设置,或者特殊字符的转义存在问题。

转储sCommandLine(或在此处添加调试器断点),可能两个文件名之一为空或严重转义。这应该很容易帮助您找到导致问题的变量。

您可能还想考虑使用像 BouncyCastle 这样的原生 OpenPGP 库,而不是从 C# 调用 GnuPG 二进制文件。

【讨论】:

  • 在我的情况下,由于文件名路径中的空格(“”)而出现问题。
猜你喜欢
  • 2018-09-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-10-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-21
相关资源
最近更新 更多