【问题标题】:GPG decryption without passphrase, working on local but fails on IIS and hosted environment没有密码的 GPG 解密,在本地工作,但在 IIS 和托管环境上失败
【发布时间】:2018-08-06 22:22:41
【问题描述】:

我正在使用 gpg 加密解密,它在我的本地系统上运行良好(在 VS2015 上运行时),但是当我将它托管到服务器/或尝试从本地 IIS 运行它时它会失败。它不会提示任何运行时错误,但不会创建解密文件。 这是命令:

gpg --local-user XXXXXT21XXX --output C:\Projects\myserver.api\Server_Files\input\callback_7a7ac0e3-89b1-4749-8d2a-d40cc84a27d5.xml --decrypt C:\Projects\server.api\Server_Files\Serverinput\callback_7a7ac0e3-89b1-4749-8d2a-d40cc84a27d5.asc

我用于解密的代码:

public string Decrypt(string inputFilePath, string extension = "")
        {
            try
            {
                var process = GetCmd();
                string outputFilePath = $@"{FileHelper.InputPath}\{Path.GetFileNameWithoutExtension(inputFilePath)}{extension}";

                string sCommandLine = $"{command}{string.Format(decCommand, outputFilePath, inputFilePath)}";

                process.StandardInput.WriteLine(sCommandLine);
                process.StandardInput.Flush();
                process.StandardInput.Close();
                process.WaitForExit();
                process.Close();
                return outputFilePath;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
 private static System.Diagnostics.Process GetCmd()
        {
            System.Diagnostics.ProcessStartInfo psi =
                 new System.Diagnostics.ProcessStartInfo("cmd.exe");
            psi.CreateNoWindow = false;
            psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
            psi.UseShellExecute = false;
            psi.RedirectStandardInput = true;
            psi.RedirectStandardOutput = true;
            psi.RedirectStandardError = true;
            psi.Verb = "runas";
            psi.WorkingDirectory = @"C:\Program Files (x86)\GnuPG\bin";

            System.Diagnostics.Process process = System.Diagnostics.Process.Start(psi);

            return process;
        }

错误:

System.IO.FileNotFoundException: Could not find file 'C:\Projects\Server.api\Server_Files\input\callback_7a7ac0e3-89b1-4749-8d2a-d40cc84a27d5.xml'.
    File name: 'C:\Projects\Server_if.api\Server_Files\input\callback_7a7ac0e3-89b1-4749-8d2a-d40cc84a27d5.xml'
       at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
       at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
       at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
       at System.IO.StreamReader..ctor(String path, Encoding encoding, Boolean detectEncodingFromByteOrderMarks, Int32 bufferSize, Boolean checkHost)
       at System.IO.File.InternalReadAllText(String path, Encoding encoding, Boolean checkHost)
       at Server.Common.Helpers.FileHelper.ReadAllText(String path) in D:\Projects\TFT\Bitbucket_PAYDO\Server_new\Server.Common\Helpers\FileHelper.cs:line 101
       at Server.API.Controllers.ServerTransferController.<GetServerCallback>d__9.MoveNext()

如何在不提示输入密码的情况下运行 gpg decrypt 命令?

【问题讨论】:

  • 错误提示找不到文件C:\Projects\Server.api\Server_Files\input\callback_7a7ac0e3-89b1-4749-8d2a-d40cc84a27d5.xml。您确定它存在于服务器上并且执行用户可以访问它吗?还有你所说的“它不提示任何运行时错误”是什么意思?因为您在问题中列出了一条错误消息。
  • 实际上这个xml是在成功解密文件后创建的,但是由于它没有解密它,所以没有创建xml并且发生FileNotFound错误,所以对我来说,解密是问题,而不是找不到文件。
  • @PankajNemi 你试过阅读StandardOuput 和StandardError 的读者吗?您也可以查看ExitCode,看看GPG是否可能在那里打印出一些信息?
  • 我尝试了同样的方法,它显示为错误:gpg:使用 RSA 密钥加密,ID 85857DC18B6ACE7B gpg:解密失败:没有密钥
  • 基于this question,您的密钥似乎在新服务器上丢失(或在正确的用户下)。它有一个公认的答案你能看看这是否对你有任何帮助吗?另外,请注意 IIS(默认情况下)在系统用户下运行,而 IIS Express 通常在您自己的用户下运行。如果每个用户的密钥环不同,那可能是您的问题。

标签: c# gnupg passphrase


【解决方案1】:

我自己找到了答案,按照@RMH 的指示,我尝试为 gpg 找到合适的用户,在我的例子中是管理员。我所做的是我创建了一个self hosted web api 并在那里尝试了相同的代码,令人惊讶的是它开始工作了。

【讨论】:

    猜你喜欢
    • 2017-09-16
    • 1970-01-01
    • 2015-04-03
    • 2018-11-06
    • 2018-02-23
    • 1970-01-01
    • 2022-06-14
    • 1970-01-01
    • 2020-03-21
    相关资源
    最近更新 更多