我想在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#中也没有成功。
如果您将phpseclib与C#一起使用,您是否愿意向我展示您的源代码?还是可以通过其他方式帮助我?

谢谢您的好意!

最佳答案

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


  照原样,您已经发布了将从PHP脚本获取响应然后返回的代码。就是说,这里有一些代码:http://csharp-tricks-en.blogspot.de/2015/04/rsa-with-c-and-php.html phpseclib的东西已经过时了-现代版本的phpseclib内置了对XML密钥的支持。但是C#可以帮助您。


我只是将其发布在此处以找到它。

This article解释该方法。

关于c# - Phpseclib在 session 中生成RSA key (使用C#客户端),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42177836/

10-09 09:19