add clear
This commit is contained in:
@@ -0,0 +1,99 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace common;
|
||||
|
||||
|
||||
class Decode
|
||||
{
|
||||
|
||||
private $sessionKey;
|
||||
private $iv;
|
||||
private $encryptedData;
|
||||
private $appId;
|
||||
|
||||
|
||||
private $OK = 0;
|
||||
private $IllegalAesKey = -41001;
|
||||
private $IllegalIv = -41002;
|
||||
private $IllegalBuffer = -41003;
|
||||
private $DecodeBase64Error = -41004;
|
||||
|
||||
/**
|
||||
* @param mixed $sessionKey
|
||||
* @return Decode
|
||||
*/
|
||||
public function setSessionKey($sessionKey)
|
||||
{
|
||||
$this->sessionKey = $sessionKey;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $iv
|
||||
* @return Decode
|
||||
*/
|
||||
public function setIv($iv)
|
||||
{
|
||||
$this->iv = $iv;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $encryptedData
|
||||
* @return Decode
|
||||
*/
|
||||
public function setEncryptedData($encryptedData)
|
||||
{
|
||||
$this->encryptedData = $encryptedData;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $appId
|
||||
* @return Decode
|
||||
*/
|
||||
public function setAppId($appId)
|
||||
{
|
||||
$this->appId = $appId;
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $asArray
|
||||
* @return array|mixed
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function decode($asArray)
|
||||
{
|
||||
if (strlen($this->sessionKey) != 24) {
|
||||
throw new \Exception('encodingAesKey 非法', $this->IllegalAesKey);
|
||||
}
|
||||
|
||||
$aesKey = base64_decode($this->sessionKey);
|
||||
if (strlen($this->iv) != 24) {
|
||||
throw new \Exception('base64解密失败', $this->IllegalIv);
|
||||
}
|
||||
|
||||
$aesIV = base64_decode($this->iv);
|
||||
$aesCipher = base64_decode($this->encryptedData);
|
||||
$result = openssl_decrypt($aesCipher, "AES-128-CBC", $aesKey, OPENSSL_RAW_DATA, $aesIV);
|
||||
if ($result === false) {
|
||||
throw new \Exception('aes 解密失败', $this->IllegalBuffer);
|
||||
}
|
||||
|
||||
$dataObj = json_decode($result);
|
||||
if ($dataObj->watermark->appid != $this->appId) {
|
||||
throw new \Exception('aes 解密失败', $this->IllegalBuffer);
|
||||
}
|
||||
|
||||
if ($asArray) {
|
||||
return get_object_vars($dataObj);
|
||||
}
|
||||
|
||||
return $dataObj;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user