php解密抖音小程序用户手机号/字节跳动小程序thinkphp

## php解密抖音小程序用户手机号/字节跳动小程序thinkphp

字节跳动小程序/抖音小程序的手机号,是有前端根据组件获取到用户信息

<button open-type="getPhoneNumber"

bindgetphonenumber="getPhoneNumberHandler"></button>

获取得到的数据

Page({

getPhoneNumberHandler(e) {

console.log(e.detail.errMsg);

console.log(e.detail.iv);

console.log(e.detail.encryptedData);

},

});

返回的参数encryptedData:包括敏感数据在内的完整用户信息的加密数据,需要后台进行解密

解密方法

1,对称解密使用的算法为AES-128-CBC,数据采用PKCS#7填充。

2,对称解密的目标密文为encryptedData。

3,对称解密秘钥aeskey = Base64_Decode(session_key), aeskey长度为 16Byte。

4,对称解密算法初始向量为Base64_Decode(iv)。

session_key:需要后端自己获取,encryptedData与iv前端传递;

<?php

public function getPhone(){

$code = input('code');

$iv = input('iv');

$sign = input('encryptedData');

$phone = '';

$resjson = $this->httpCurl('https://developer.toutiao.com/api/apps/jscode2session?app.$code,[]);

if ($resjson['error']==0){

$key = $resjson['session_key'];

$openid = $resjson['openid'];

//解密数据

$phone = openssl_decrypt(base64_decode($sign,true), 'AES-128-CBC', base64_decode($key), OPENSSL_RAW_DATA, base64_decode($iv));

if (!$phone){

return json(['code'=>2,'msg'=>'手机号解密失败']);

}else{

$phone = json_decode($phone,true)['phoneNumber'];

}

}else{

return json(['code'=>2,'msg'=>'授权登陆失败']);

}

}

function httpCurl($url,$data=null)

{

//初始化

$ch = curl_init();

//设置选项,包括URL

curl_setopt($ch,CURLOPT_URL,$url);

curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);

if(!empty($data)){

curl_setopt($ch,CURLOPT_POST,1);

curl_setopt($ch,CURLOPT_POSTFIELDS,$data);

}

$output = curl_exec($ch);

curl_close($ch);

return json_decode($output,true);

}

$phone 获得的数据为:

{

"phoneNumber": "138xxxxxxxx", // 用户绑定的手机号(国外手机号会有区号)

"purePhoneNumber": "138xxxxxxxx", // 没有区号的手机号

"countryCode": "86", // 区号

"watermark": {

"appid": "ttxxxxxxxxxxxxxxxx",

"timestamp": 15000000000000000

}

}

这样php解密字节跳动小程序/抖音小程序的手机号就完成了