Skip to content

Commit 503c17c

Browse files
committed
新增小程序调用示例
新增小程序调用示例
1 parent b005632 commit 503c17c

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
namespace app\api\utils;
3+
4+
/**
5+
*
6+
*/
7+
class wxBizDataCrypt
8+
{
9+
10+
public static $OK = 0;
11+
public static $IllegalAesKey = -41001;
12+
public static $IllegalIv = -41002;
13+
public static $IllegalBuffer = -41003;
14+
public static $DecodeBase64Error = -41004;
15+
16+
private $appid;
17+
private $sessionKey;
18+
19+
/**
20+
* 构造函数
21+
* @param $sessionKey string 用户在小程序登录后获取的会话密钥
22+
* @param $appid string 小程序的appid
23+
*/
24+
public function __construct( $appid, $sessionKey)
25+
{
26+
$this->sessionKey = $sessionKey;
27+
$this->appid = $appid;
28+
}
29+
30+
31+
/**
32+
* 检验数据的真实性,并且获取解密后的明文.
33+
* @param $encryptedData string 加密的用户数据
34+
* @param $iv string 与用户数据一同返回的初始向量
35+
* @param $data string 解密后的原文
36+
*
37+
* @return int 成功0,失败返回对应的错误码
38+
*/
39+
public function decryptData( $encryptedData, $iv )
40+
{
41+
if (strlen($this->sessionKey) != 24) {
42+
return self::$IllegalAesKey;
43+
}
44+
$aesKey=base64_decode($this->sessionKey);
45+
46+
47+
if (strlen($iv) != 24) {
48+
return self::$IllegalIv;
49+
}
50+
$aesIV=base64_decode($iv);
51+
52+
$aesCipher=base64_decode($encryptedData);
53+
54+
$result=openssl_decrypt( $aesCipher, "AES-128-CBC", $aesKey, 1, $aesIV);
55+
56+
$dataObj=json_decode( $result );
57+
if( $dataObj == NULL )
58+
{
59+
return self::$IllegalBuffer;
60+
}
61+
if( $dataObj->watermark->appid != $this->appid )
62+
{
63+
return self::$IllegalBuffer;
64+
}
65+
$data = $result;
66+
return $data;
67+
}
68+
}

0 commit comments

Comments
 (0)