[GPG使用进程类从c#网站无法解密

问题描述 投票:0回答:1

我正在使用Process类在C#asp.net网站中使用GnuPG解密pgp加密文件。我可以使用Process类在网站上成功导入和加密GPG命令。当我运行--decrypt时,文件未解密,StandardError输出看起来像这样:

gpg:使用2048位RSA密钥进行加密,ID为AAAAAAAAAAAAAAAA,创建于2019-12-06“公司A”gpg:解密失败:没有密钥

我确实有我在代码中使用的正确密钥。但是此网页未找到密钥。我已经通过在网站上执行--list-secret-keys GPG命令来确认这一点。它什么也没列出。网站内的--list-keys cmd确实列出了公共密钥。

我可以在同一Web服务器上从Windows cmd提示符运行相同的GPG解密cmd,并且解密使用相同的密码进行。这是DOS cmd和输出:

C:\ path> gpg --batch --trust-model always --pinentry-mode loopback --passphrase“ PassPhrase”-输出“ D:\ Websites \ test.txt” --decrypt“ D:\ Websites \ test.pgp“

[gpg:用2048位RSA密钥(ID AAAAAAAAAAAAAAAA,加密,创建于2019-12-06“公司A”

这是我的aspx和aspx.cs解密代码:

<%@ Page Title="" Language="C#" Debug="true" AutoEventWireup="true" CodeFile="td.aspx.cs" Inherits="TestDecrypt" %>

using System;
using System.Web.UI;
using System.Diagnostics;

public partial class TestDecrypt : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            string inFile = "D:\\Websites\\test.res.pgp";
            string outFile = inFile.Replace(".res.pgp", "res.txt");
            DecryptFile(inFile, outFile);
        }
        catch (Exception ex)
        {
            Response.Write("ex.Message=" + ex.Message);
        }
    }

    private void DecryptFile(string inputName, string outputName)
    {
        const string commandFormat = @"--passphrase --batch --trust-model always --pinentry-mode loopback --output {0} --decrypt {1}";
        PgpCmd(string.Format(commandFormat, outputName, inputName), "PassPhrase");
    }

    public void PgpCmd(string command, string password)
    {
        string path = string.Format(@"{0}\gpg.exe", @"C:\Program Files (x86)\GnuPG\bin");

        var procStartInfo = new ProcessStartInfo(path, command)
        {
            CreateNoWindow = true,
            UseShellExecute = false,
            RedirectStandardError = true
        };

        var proc = new Process { StartInfo = procStartInfo };
        proc.Start();

        while (!proc.StandardError.EndOfStream)
        {
            string line = proc.StandardError.ReadLine();
            Response.Write("<br>proc line:" + line);
        }

        proc.WaitForExit();
    }
}

这是我的aspx和aspx.cs列表键代码:

<%@ Page Title="" Language="C#" Debug="true" AutoEventWireup="true" CodeFile="td2.aspx.cs" Inherits="TestDecrypt2" %>

using System;
using System.Web.UI;
using System.Diagnostics;

public partial class TestDecrypt2 : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            ListKeys();
        }
        catch (Exception ex)
        {
            Response.Write("ex.Message=" + ex.Message);
        }
    }

    private void ListKeys()
    {
        Response.Write("<br />all keys:");
        string cmd = @"--list-keys";
        PgpCmd(cmd);

        Response.Write("<br /><br />secret keys:");
        cmd = @"--list-secret-keys";
        PgpCmd(cmd);
    }

    public void PgpCmd(string command)
    {
        string path = string.Format(@"{0}\gpg.exe", @"C:\Program Files (x86)\GnuPG\bin");

        var procStartInfo = new ProcessStartInfo(path, command)
        {
            CreateNoWindow = true,
            UseShellExecute = false,
            RedirectStandardOutput = true
        };

        var proc = new Process { StartInfo = procStartInfo };
        proc.Start();

        while (!proc.StandardOutput.EndOfStream)
        {
            string line = proc.StandardOutput.ReadLine();
            Response.Write("<br>proc line:" + line);
        }

        proc.WaitForExit();
    }
}

任何人都知道为什么网页无法访问密钥,但是DOS cmd可以访问吗?

感谢您的帮助

c# encryption process gnupg pgp
1个回答
0
投票

我发现了问题。我的网站与使用远程桌面连接登录到Web服务器时的用户不同,正在与RDC用户(用于安装和创建Gnupg文件)的位置不同的位置寻找私钥。我尝试使用GPG --homedir来设置位置,但是没有运气。我终于将所需文件(包括私钥文件)复制到了网站用户使用的目录位置,并对其进行了修复。我从网站上进行了GPG -h查找网站正在寻找私钥的文件夹位置。无需更改编码。对这篇文章大喊大叫,这终于帮助我找到了解决方案。 GnuPG + Webservice + ASP.NET

© www.soinside.com 2019 - 2024. All rights reserved.