Skip to content

Commit d62a19b

Browse files
committedNov 4, 2021
2021-11-04 18:19:11 update
1 parent 40ef113 commit d62a19b

File tree

221 files changed

+53258
-1
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

221 files changed

+53258
-1
lines changed
 

‎.DS_Store

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

0 commit comments

Comments
 (0)
Please sign in to comment.