2017-04-19 41 views
1

我通过Soap连接到web服务,并且需要填写标题凭证才能登录。PHP Soap填充标题凭证

$user_id  = 'MyUserId'; 
$unique_key  = $this->getUniqueKey(); 
$base_password = $this->getFieldBase('MyPassword', $uniqueKey); 
$base_date  = $this->getFieldBase(gmdate('Y-m-d\TH:i:s\.00\Z'), $unique_key); 
$nonce   = $this->getFieldNonce($unique_key, '.myPemFile.pem'); 

<wss:UsernameToken> 
    <wss:Username>' . $user_id . '</wss:Username> 
    <wss:Password>' . $base_password . '</wss:Password> 
    <wss:Nonce>' . $nonce . '</wss:Nonce> 
    <wss:Created>' . $base_date . '</wss:Created> 
</wss:UsernameToken> 

所有值(用户名除外)都遵循结构。

enter image description here enter image description here enter image description here

我有这个工作了5.6 PHP项目,但现在我需要它适应PHP 7项目,这意味着我可以不再使用mcrypt_encrypt(),因为它已过时,因此我需要使用openssl_encrypt()

我现在的职责是:

private function getFieldBase($data, $key) 
{ 
    $ivsize   = openssl_cipher_iv_length('AES-128-ECB'); 
    $iv    = openssl_random_pseudo_bytes($ivsize); 
    $ciphertext  = openssl_encrypt($data, 'AES-128-ECB', $key, 0, $iv); 

    return trim(base64_encode($ciphertext)); 
} 

private function getFieldNonce($data, $pbkey) 
{ 
    openssl_public_encrypt($data, $crypttext, openssl_pkey_get_public(file_get_contents($pbkey))); 

    return base64_encode($crypttext); 
} 

private function getUniqueKey() 
{ 
    return substr(md5(uniqid(microtime())), 0, 16); 
} 

钍E题是连接到web服务时,我收到错误:

Rejected: Error: The key session is invalid. It was not possible to decipher the field Created

,告诉我,我的getFieldBase()功能是错误的。

回答

1

已解决

参数RAW_OUTPUT必须在函数openssl_encrypt内为真。

private function getFieldBase($data, $key) 
{ 
    $ivsize  = openssl_cipher_iv_length('AES-128-ECB'); 
    $iv   = openssl_random_pseudo_bytes($ivsize); 
    $ciphertext = openssl_encrypt($data, 'AES-128-ECB', $key, TRUE, $iv); 

    return base64_encode($ciphertext); 
}