Python3解析微信小程序密文

官方给的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
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值