Phpseclib 在会话中生成 RSA 密钥(使用 C# 客户端)

Phpseclib generated RSA keys in a session (with C# client)

我想在 PHP 和 C# 之间进行 RSA 通信。 我尝试使用 phpseclib 但我遇到了一些问题。

这是我在服务器上创建密钥的方式:

$rsa = new Crypt_RSA();
extract($rsa->createKey());
$_SESSION['RSAPubKey'] = $publickey;
$_SESSION['RSAPrivKey'] = $privatekey;
echo $_SESSION['RSAPubKey']; //here I send the public key to the client

这是 C# 客户端:

string valasz = RequestPOST(Program.SzerverDomain + "/Teszt1/SZCShost.php", "hostmuv=biztkapcsrsakulcs");

这是“RequestPOST()”方法:

    static CookieContainer cookieJar = new CookieContainer();
    public static string RequestPOST(String URL, string postData)
    {

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Uri.EscapeUriString(URL));
        request.CookieContainer = cookieJar;

        var data = Encoding.UTF8.GetBytes(postData);

        request.Method = "POST";
        request.ContentType = "application/x-www-form-urlencoded";
        request.ContentLength = data.Length;

        using (var stream = request.GetRequestStream())
        {
            stream.Write(data, 0, data.Length);
        }

        var response = (HttpWebResponse)request.GetResponse();

        var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();

        return responseString.Replace("<br>", "\n").Replace("<br/>", "\n");
    }

现在我有钥匙了,但是我不能使用它们。我尝试加载它们,但在 php 和 C# 中都没有成功。 如果你在 C# 中使用了 phpseclib,你能告诉我你的源代码吗?或者你能以其他方式帮助我吗?

谢谢你的好意!

此解决方案来自 neubert 的评论:

As is you've posted code that'll get the response from the PHP script and then return it. That said, here's some code: http://csharp-tricks-en.blogspot.de/2015/04/rsa-with-c-and-php.html The phpseclib stuff is out of date - modern versions of phpseclib have built in support for XML keys. But the C# can maybe help you.

我只是post在这里找到它。

This article说明方法。