从C#到PHP的RSA加密 [英] RSA encryption from From C# to PHP
问题描述
我需要连接到使用RSA加密的特定Web服务.使用NuSOAP和PHPseclib,我可以连接到服务器以获取用于RSA加密的公钥并将数据发送回服务器.
I need to connect to a certain webservice that uses RSA encryption. Using NuSOAP and PHPseclib I can connect to the server to get the public key used to RSA encrypt and send back data to the server.
在他们加密数据的方式中出现了问题,那里的开发人员给了我用C#编写的文档和示例,这些文档和示例很难理解并转换为PHP.唯一需要加密的是密码.
Problem arise in the way they are encrypting the data, the developer there gave me documentation and sample written in C# to which is hard to comprehend and convert to PHP. The only thing that needs to be encrypted is the password.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Dns.Lib.Compression;
using Dns.Lib.Security.Cryptography;
using ThirdPartyTest.ThirdPartyService;
using SoapExtensionLib;
private static string BaseServiceCall(int sysUserId, string password)
{
SecureMessageManager secureMessageManager = new SecureMessageManager();
ThirdPartyService.Main client = new ThirdPartyService.Main();
string dnPublicKey = client.GetPublicKey();//this is used to get the pbKey
secureMessageManager.ImportThirdPartyRSAKey(dnPublicKey);
string sessionId = Guid.NewGuid().ToString("N");
string tokenRequestXml = @"<request>
<id>" + sysUserId + @"</id>
<password>" + secureMessageManager.RSAEncryptString(password) + @"</password>
<session>" + sessionId + @"</session>
<public>" + secureMessageManager.ExportPublicKey() + @"</public>
</request>";
string tokenResonse = client.GetSommething(tokenRequestXml);
//...
}
secureMessageManager.ExportPublicKey()
还能做什么?
这是我上面的PHP版本.
Here is my PHP version of the above.
require_once('RSA-PHPseclib0.3.5/Crypt/RSA.php');
require_once('nusoap.php');
$sysUserId = '.....';
$pass = '.....';
$sessionId = session_id();
$pubKey = "<rsakeyvalue><modulus>kdT4UUBowCepvTU3panVLnAWqKSIik8eSc5Sog0e7BOP2EoNVwer5RfxIICaWzVpJtPAQJYQ9AaW4qpp3qIw3g3DR7OTw/lvif6y5VemiRue6u4T2eef4AG3Ka0MoMhTtMxIRXGNqx6hPyPn40F9ZMFwprFupFl0Z2LPRLR3Fek=</modulus><exponent>AQAB</exponent></rsakeyvalue>";//The Public key received from an other program
$rsa = new Crypt_RSA();
$return = $rsa->loadKey($pubKey);
$password = base64_encode($rsa->encrypt($pass));
$inputData = "<request><id>$sysUserId</id><password>$password</password><session>$sessionId</session><public>$pubKey</public></request>";
$params = array('inputData'=>$inputData);
$result = $client->call('GetSommething', $params, $namespaces, '', '',null,'document','literal');
我从他们的服务器收到的错误是'服务器无法处理请求.--->解码OAEP填充时发生错误.我收集到的信息与我加密密码的方式有关.花了40多个小时打了砖墙,不知道该怎么办.
The error that I receive from they server is 'Server was unable to process request. ---> Error occurred while decoding OAEP padding.'
. I have gather it has something to do with the way I encrypt the password. Having spent over 40 hours hitting a brick wall, not sure what to do.
推荐答案
解决方案此处需要对默认phpseclib库进行一些更改,以使其与.net库进行加密/解密兼容.
我感到惊讶的是,PHP加密/解密RSA的方式与C#进行加密的方式之间的不兼容之类的东西虽然有所不同,但涉及的话题很少.
Got the solution here and Here
There are some changes that needs to be made to the default phpseclib library to make it compatible to encrypt/decrypt with the .net libraries.
I am just surprised that Something as obvious as incompatibility between the way PHP encrypt/decrypt RSA and the way C# does it are somehow different but very few topic on it.
这篇关于从C#到PHP的RSA加密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!