使用 crypto-js 解密微信 数据(没啥用的,记录下)

            let wx_login_url = 'https://api.weixin.qq.com/sns/jscode2session?appid=' + appid + '&secret=' + secret + '&js_code=' + code + '&grant_type=authorization_code';
            wx.request({
                url: wx_login_url,
                data: {},
                header: {
                    'content-type': 'application/json' // 默认值
                },
                success(res) {
                    that.openid = res.data.openid;
                    that.session_key = res.data.session_key;

                    if (that.autoLogin) {
                        WXGameTools.getSetting((res) => {
                            if (res.statue == 0) {
                                //进入大厅
                                let userInfo = res.data.userInfo;
                                that.setUserInfo(userInfo);
                            } else {
                                that.createUserInfoBtn();
                            }
                        });
                    } else {
                        that.createUserInfoBtn();
                    }
                }
            });

上面代码是获取登录code和key

            let sessionKey = this.session_key;
            let iv = userInfo.iv;
            if (sessionKey) {
                let encryptedData = userInfo.encryptedData;
                let CryptoJS = require('crypto-js.min');

                sessionKey = CryptoJS.enc.Base64.parse(decodeURIComponent(sessionKey));
                iv = CryptoJS.enc.Base64.parse(decodeURIComponent(iv));

                let decrypt = CryptoJS.AES.decrypt(encryptedData, sessionKey, {
                    iv: iv,
                    mode: CryptoJS.mode.CBC,
                    padding: CryptoJS.pad.Pkcs7
                });

                console.log("value: " + require('base64.min').Base64.decode(CryptoJS.enc.Base64.stringify(decrypt)));
                userInfo = JSON.parse(require('base64.min').Base64.decode(CryptoJS.enc.Base64.stringify(decrypt)));
            }

上面代码是使用 crypto-js 解密 微信传给的数据
iv和encryptedData,是用户授权才能拿到的数据

解密后能获取到用户的 openid和一系列数据,
unionid可能获取不到,
想要获取unionid 还要参考以下链接
https://www.jianshu.com/p/90f1255fa49e

1赞

<a class="a解密用到的js.zip (18.2 KB)

1赞