解密php soap响应

时间:2017-12-27 13:48:00

标签: java php encryption soap

我目前正在尝试构建一个php页面,该页面将基于XML的数据发送到提供商处的SOAP服务,然后提供者返回加密响应。

我需要帮助来弄清楚如何解密响应。

我稍微更改了名称,因此代码是匿名的。

以下是我所做的所有代码"播放"用:

// Turn on error reporting

error_reporting(-1);

// Triple linebreak function
function tlb() {
echo "<br /><br /><br />";
}


//  Test XML

$xml = '<?xml version="1.0" encoding="ISO-8859-1"?>
SOME XML';


// SOAP Config
$soapConfig = array(
    'cache_wsdl' => WSDL_CACHE_NONE,
    'trace' => true,
    'exceptions' => true,
    'encoding' => "ISO-8859-1",
    //'use' => SOAP_LITERAL,
    'verify_peer' => true,
    'stream_context' => stream_context_create([
        'ssl' => [
            'local_cert' => $wsdlcertpem,
            'passphrase' => $wsdlpasspem
        ]
    ])
);


// SOAP Connection

$soapClient = new SoapClient($wsdlurl, $soapConfig);


// SOAP Function

try {
    $soapClient->function($xml);
}
catch (SoapFault $fault) {
    trigger_error("SOAP Fault: (faultcode: {$fault->faultcode}, faultstring: {$fault->faultstring})", E_USER_ERROR);
}


// Load into variables

$LastRequestHeaders = $soapClient->__getLastRequestHeaders();
$LastRequest = $soapClient->__getLastRequest();
$LastResponseHeaders = $soapClient->__getLastResponseHeaders();
$LastResponse = $soapClient->__getLastResponse();
$b64LastResponse = base64_decode($LastResponse);

// Echo output

echo "SOAP XML:<pre>". htmlentities($xml) ."</pre>";
tlb();
echo "SOAP REQUESTHEADERS: <pre>$LastRequestHeaders</pre";
tlb();
echo "SOAP REQUEST: <pre>$LastRequest</pre>";
tlb();
echo "SOAP RESPONSEHEADERS:<pre>$LastResponseHeaders</pre>";
tlb();
echo "SOAP RESPONSE:<pre>$LastResponse</pre>";
tlb();
echo "SOAP RESPONSE Base64Decoded: <pre>$b64LastResponse</pre>";
tlb();


// Check signature on data

$key = openssl_pkey_get_public('$wsdlcertpem');

echo "$key";

$ok = "";
$signature = "";
$ok = openssl_verify($LastResponse, $signature, $key, sha1WithRSAEncryption);
echo "Check: <br/>";
if ($ok == 1) {
    echo "Signature OK";
} elseif ($ok == 0) {
    echo "Signature BAD";
} else {
    echo "Error checking signature";
}
echo "<br/>".$ok."";


tlb();
tlb();
// Attempt to decrypt 

if (!$publicKey = openssl_pkey_get_public($wsdlcerttestpem))
{
    echo "Public Key failed: $publicKey";
    die;
}
$a_key = openssl_pkey_get_details($publicKey);

// Decrypt the data in the small chunks
$chunkSize = ceil($a_key['bits'] / 8);
$output = '';
echo "ChunkSize: $chunkSize";
$encrypted = $b64LastResponse;


$decrypt_block_size = 256;
$decrypted = "";
$data = str_split($b64LastResponse, $decrypt_block_size);
foreach ($data as $chunk)
{
    $partial = "";
    $decryptionok = "";
    $decryptionok = openssl_public_decrypt($chunk, $partial, $publicKey);
    if($decryptionOK === false){return false;}//here also processed errors in decryption. If too big this will be false
    $decrypted .= $partial;
}

echo "Decrypteddata: $decrypted <br/>";

while ($encrypted)
{
    $chunk = substr($encrypted, 0, $chunkSize);
    $encrypted = substr($encrypted, $chunkSize);
    $decrypted = '';
    if (!openssl_public_decrypt($chunk, $decrypted, $publicKey))
    {
        echo "Failed to decrypt data";
        //die;
    }
    $output .= $decrypted;
}
openssl_free_key($publicKey);
echo "Decrypted data: $output";
// Uncompress the unencrypted data.
//$output = gzuncompress($output);

echo '<br /><br /> Unencrypted Data: ' . $output;




?>

标题表明SOAP本身运行正常。

SOAP响应如下:

MIIIZgYJKoZIhvcNAQcCoIIIVzCCCFMCAQExCzAJBgUrDgMCGgUAMIIBRAYJKoZI
hvcNAQcBoIIBNQSCATE8P3htbCB2ZXJzaW9uPSIxLjAiIGVuY29kaW5nPSJJU08t
ODg1OS0xIj8+CjxyZXBseT48ZXJyb3I+PGludmFsaWQtZGF0YT48ZXJyb3ItY29k
ZT5DVUkwNTE8L2Vycm9yLWNvZGU+PGVycm9yLXRleHQ+ZmVqbCB2ZWQgWE1MIGlu
ZGdhbmc8L2Vycm9yLXRleHQ+PGVycm9yLXRleHQt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CwYJKoZIhvcNAQcBMBwGCSqGSIb3DQEJ
BTEPFw0xNzEyMjcxMzQyMTNaMCMGCSqGSIb3DQEJBDEWBBRXJS7GC+sYrrb9YaLm
nsFH3e/lyDANBgkqhkiG9w0BAQEFAASCAQBTSz7X4URhT+u3b40YELnOT+lXirXP
GGNb9UhQFZ+XAq8Fd9DTYs7A5EXpEDsByxnupctz48sG5Q8sosnZaMcQoIQvcPpg
xgZ5Reo/qNlAbiWbGubt0wSCmwqJ1RbVlJu9zle8/7Y557YmQNMgEglnFus2Q8p4
9BuCK+e9bTuJ8JgLM6IokQdWcCWKevkWakHtBxrI/XrPPnPVqQbkS7/uKJ50FqqR
Vv8Ip7dcGJCMaqKi+/VGoEuoDjt262MeKF1ZKI9Et0+wOEtnirMz97bByTyTHH6T
3ijPfaRyCNsdWXy3F8NHsI0I1Bo9mRRp7T4dlX04csVACIQvpmfbMLLL

当我使用base64解码时,我会得到类似这样的内容:

�ioz�"�}tzw(v)�!#��u���z{Ğ������Y첆�z{����!zf��il��h�졪z{ޖ�^������r�ig���i��0�
���M\��r�k���l�m����7��?�M5�sI�^����֧q����h�׭"w�E�)�{H��^���x�)^��i���隳������?�����ܡا��f�{8�5Xm�����^��1�[(j�+��(j�ޞ������ig�{4���ƥ�ig�#�
org.xml.s�v��4�W�6WF���W�V7F���GW&��r'6S�6��FV�Mw is not allowed in prolog.�s����fƖB�FF���W'&�#���&WǓ� R� Rc @�0
     

-we�0         * H       0 <10 UUS10U       Ge�v�G&#39;W7B���2�C`5P@10�&amp;�E54�4�p�## s#mw02Z       161128133915Z0��1)0&#39;Uxt6QyeIFBMIcMJ5J�v�cGe���G��6�g

(还有更多..)

然而,无论我尝试什么,似乎我无法正确解密响应,即使使用他们的证书进行解密。

他们真的没有任何文档,但我有一个程序中的java代码,可以正常解码响应。

以下是java加密客户端的代码段:

private String checkAndDecode(final String base64AndSigned, final Certificate signingCert) throws IOException, SignatureException, NoSuchAlgorithmException {
final byte[] decoded = Base64.decodeBase64(base64AndSigned);

final PKCS7 p7 = new PKCS7(new DerInputStream(decoded));
final SignerInfo[] signerInfo = p7.verify();
final Certificate signerCertificate = signerInfo[0].getCertificate(p7);

if((signingCert != null) && !signingCert.equals(signerCertificate)) {
    throw new SignatureException("Signed by incorrect certificate");
}

return new String(p7.getContentInfo().getContentBytes());
}

不幸的是,我真的不懂Java。

如果有人可以帮助我指出正确的方向,那将非常感激。 如果您需要更多信息,请告诉我,我会提供。

0 个答案:

没有答案