2017-04-19 46 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); 
}