问题
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.
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);
//...
}
Also what does secureMessageManager.ExportPublicKey()
do?
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');
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.
回答1:
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.
回答2:
You should check msdn forum they have similar topic. encrypt with php
On asp.net forum OAEP padding
来源:https://stackoverflow.com/questions/20264842/rsa-encryption-from-from-c-sharp-to-php