【发布时间】:2017-02-11 15:48:29
【问题描述】:
我想在 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,你会这么好心给我看你的源代码吗?或者你能以其他方式帮助我吗?
谢谢你的好意!
【问题讨论】:
-
您可以发布您编写的代码以实际加载密钥吗?正如您发布的代码一样,该代码将从 PHP 脚本获取响应然后返回它。也就是说,这里有一些代码:csharp-tricks-en.blogspot.de/2015/04/rsa-with-c-and-php.html phpseclib 的东西已经过时了——现代版本的 phpseclib 已经内置了对 XML 键的支持。但 C# 或许能帮到你。
-
毕竟我用的是updated version of that article。有用。谢谢!
标签: c# php server rsa phpseclib