doulu1325 2019-03-29 16:04
浏览 30

如何在PHP中解密加密的soap响应?

I am trying to implement a webservice using PHP SoapClient. The Webservice is protected with WS-Security. I am using this package https://github.com/robrichards/wse-php and followed the examples provided within the package.

I applied UsernameToken, signed the headers & body and sent encrypted request to the webservice.

Below is the code snippet:

<?php
define('PRIVATE_KEY', dirname(__FILE__) . '/private.pem');
define('SERVICE_CERT', dirname(__FILE__) . '/downloadedfromwebpage.cer');
define('CERT_FILE', dirname(__FILE__) . '/convertedfromCER.pem');

class MySoap extends SoapClient {
    function __doRequest($request, $location, $saction, $version) {
        $doc = new DOMDocument('1.0');
        $doc->loadXML($request);

        $objWSSE = new WSSESoap($doc);

        // add Timestamp with no expiration timestamp
        $objWSSE->signAllHeaders = true; 
        $objWSSE->addTimestamp();

        // create new XMLSec Key using AES256_CBC and type is private key
        $objKey = new XMLSecurityKey(XMLSecurityKey::RSA_SHA1, array('type'=>'private'));

        // load the private key from file - last arg is bool if key in file (TRUE) or is string (FALSE)
        $objKey->loadKey(PRIVATE_KEY, TRUE);

        // Sign the message - also signs appropiate WS-Security items
        $options = array("insertBefore" => FALSE, "");
        $objWSSE->signSoapDoc($objKey, $options);

        // Add certificate (BinarySecurityToken) to the message
        $token = $objWSSE->addBinaryToken(file_get_contents(CERT_FILE));

        // Attach pointer to Signature
        $objWSSE->attachTokentoSig($token);

        $objKey = new XMLSecurityKey(XMLSecurityKey::AES256_CBC);
        $objKey->generateSessionKey();

        $siteKey = new XMLSecurityKey(XMLSecurityKey::RSA_OAEP_MGF1P, array('type'=>'public'));
        $siteKey->loadKey(SERVICE_CERT, TRUE, TRUE);

        $options = array("KeyInfo" => array("X509SubjectKeyIdentifier" => true));
        $objWSSE->encryptSoapDoc($siteKey, $objKey, $options);

        $retVal = parent::__doRequest($objWSSE->saveXML(), $location, $saction, $version);

        #return $retVal;
        $doc = new DOMDocument();
        $doc->loadXML($retVal);

        $options = array("keys" => array("private" => array("key" => PRIVATE_KEY, "isFile" => true, "isCert" => false)));
        $objWSSE->decryptSoapDoc($doc, $options); // Here it always throws exception "Failure decrypting Data"

        return $doc->saveXML();
    }
}

I am receiving the encrypted response but i am not able to decrypt it. It always throws me exception "Failure decrypting Data".
Any help is much appreciated.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 关于大棚监测的pcb板设计
    • ¥15 stm32开发clion时遇到的编译问题
    • ¥15 lna设计 源简并电感型共源放大器
    • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
    • ¥15 Vue3地图和异步函数使用
    • ¥15 C++ yoloV5改写遇到的问题
    • ¥20 win11修改中文用户名路径
    • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
    • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
    • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题