官方给的demo包引入不了,这种可以
小程序必须先登录再获取手机号,不能将wx.login方法写在getPhoneNumber里面,否则session_key解密不了
import base64
import json
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
APP_ID = "xxx"
def decrypt(data, iv, session_key):
session_key = base64.b64decode(session_key)
data = base64.b64decode(data)
iv = base64.b64decode(iv)
cipher = Cipher(algorithms.AES(session_key), modes.CBC(iv)).decryptor()
def _un_pad(s):
return s[:-ord(s[len(s) - 1:])]
decrypted = json.loads(_un_pad(cipher.update(data)).decode("utf8"))
if decrypted['watermark']['appid'] != APP_ID:
raise Exceprion('用户不存在')
return decrypted