2017-02-11 58 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解釋了該方法。