2017-02-11 59 views
2

我想在PHP和C#之间进行RSA通信。 我尝试使用phpseclib但我遇到了一些问题。Phpseclib在会话中生成RSA密钥(使用C#客户端)

这是我如何在服务器上创建的键:

$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,您会如此友善地告诉我您的源代码吗?或者你能以另一种方式帮助我吗?

谢谢你的好意!

+0

你可以发布你写的代码来实际加载密钥吗?正如你已经发布的代码,将得到PHP脚本的响应,然后返回它。也就是说,这里有一些代码:http://csharp-tricks-en.blogspot.de/2015/04/rsa-with-c-and-php.html phpseclib的东西已经过时 - 现代版本的phpseclib已经构建支持XML密钥。但是C#也许可以帮助你。 – neubert

+1

毕竟,我使用了[该文章的更新版本](http://csharp-tricks-en.blogspot.hu/2015/04/rsa-with-c-and-php_27.html)。 它的工作原理。谢谢! –

回答

0

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

正如你已经发布的代码,会得到来自PHP脚本的响应,然后返回。这就是说,下面是一些代码:http://csharp-tricks-en.blogspot.de/2015/04/rsa-with-c-and-php.html phpseclib的东西已经过时 - 现代版本的phpseclib已经内置了对XML密钥的支持。但是C#也许可以帮助你。

我只是发布它在这里找到它。

This article解释了该方法。