...
|
...
|
@@ -29,37 +29,64 @@ class WXBizDataCrypt |
|
|
*
|
|
|
* @return int 成功0,失败返回对应的错误码
|
|
|
*/
|
|
|
public function decryptData($encryptedData, $iv, &$data)
|
|
|
public function decryptData( $encryptedData, $iv, &$data )
|
|
|
|
|
|
{
|
|
|
|
|
|
if (strlen($this->sessionKey) != 24) {
|
|
|
|
|
|
return ErrorCode::$IllegalAesKey;
|
|
|
|
|
|
}
|
|
|
$aesKey = base64_decode($this->sessionKey);
|
|
|
|
|
|
$aesKey=base64_decode($this->sessionKey);
|
|
|
|
|
|
if (strlen($iv) != 24) {
|
|
|
|
|
|
return ErrorCode::$IllegalIv;
|
|
|
|
|
|
}
|
|
|
$aesIV = base64_decode($iv);
|
|
|
|
|
|
$aesCipher = base64_decode($encryptedData);
|
|
|
$aesIV=base64_decode($iv);
|
|
|
|
|
|
// $aesCipher=base64_decode($encryptedData);
|
|
|
|
|
|
$aesCipher=$encryptedData;
|
|
|
|
|
|
$pc = new Prpcrypt($aesKey);
|
|
|
$result = $pc->decrypt($aesCipher, $aesIV);
|
|
|
|
|
|
$result = $pc->decrypt($aesCipher,$aesIV);
|
|
|
|
|
|
var_dump($result);
|
|
|
|
|
|
if ($result[0] != 0) {
|
|
|
|
|
|
return $result[0];
|
|
|
|
|
|
}
|
|
|
|
|
|
$dataObj = json_decode($result[1]);
|
|
|
if ($dataObj == NULL) {
|
|
|
return ErrorCode::$IllegalBuffer;
|
|
|
$dataObj=json_decode( $result[1] );
|
|
|
|
|
|
if( $dataObj == NULL )
|
|
|
|
|
|
{
|
|
|
|
|
|
return ErrorCode::$IllegalBuffer.'--';
|
|
|
|
|
|
}
|
|
|
if ($dataObj->watermark->appid != $this->appid) {
|
|
|
return ErrorCode::$IllegalBuffer;
|
|
|
|
|
|
if( $dataObj->watermark->appid != $this->appid )
|
|
|
|
|
|
{
|
|
|
|
|
|
return ErrorCode::$IllegalBuffer.';;';
|
|
|
|
|
|
}
|
|
|
|
|
|
$data = $result[1];
|
|
|
|
|
|
return ErrorCode::$OK;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
...
|
...
|
@@ -133,33 +160,52 @@ class Prpcrypt |
|
|
* @param string $aesIV 解密的初始向量
|
|
|
* @return string 解密得到的明文
|
|
|
*/
|
|
|
public function decrypt($aesCipher, $aesIV)
|
|
|
{
|
|
|
public function decrypt( $aesCipher, $aesIV )
|
|
|
|
|
|
{
|
|
|
|
|
|
try {
|
|
|
$module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
|
|
|
mcrypt_generic_init($module, $this->key, $aesIV);
|
|
|
|
|
|
//解密
|
|
|
$decrypted = mdecrypt_generic($module, $aesCipher);
|
|
|
mcrypt_generic_deinit($module);
|
|
|
mcrypt_module_close($module);
|
|
|
// $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
|
|
|
|
|
|
// mcrypt_generic_init($module, $this->key, $aesIV);
|
|
|
|
|
|
// //解密
|
|
|
|
|
|
// $decrypted = mdecrypt_generic($module, $aesCipher);
|
|
|
|
|
|
// mcrypt_generic_deinit($module);
|
|
|
|
|
|
// mcrypt_module_close($module);
|
|
|
|
|
|
$decrypted = openssl_decrypt($aesCipher,'AES-128-CBC',$this->key,OPENSSL_ZERO_PADDING,$aesIV);
|
|
|
|
|
|
// var_dump($decrypted);
|
|
|
|
|
|
} catch (Exception $e) {
|
|
|
|
|
|
return array(ErrorCode::$IllegalBuffer, null);
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
try {
|
|
|
//去除补位字符
|
|
|
|
|
|
//去除补位字符
|
|
|
|
|
|
$pkc_encoder = new PKCS7Encoder;
|
|
|
|
|
|
$result = $pkc_encoder->decode($decrypted);
|
|
|
|
|
|
} catch (Exception $e) {
|
|
|
//print $e;
|
|
|
|
|
|
//print $e;
|
|
|
|
|
|
return array(ErrorCode::$IllegalBuffer, null);
|
|
|
|
|
|
}
|
|
|
|
|
|
return array(0, $result);
|
|
|
|
|
|
}
|
|
|
}
|
|
|
|
...
|
...
|
|