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?appid=您的apppid&secret=你的secrer&code='.$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解密字節跳動小程序/抖音小程序的手機號就完成了


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM