背景:

微信快捷登录,获取用户手机号。

微信公众平台解释:

考虑到安全性,微信服务器的开放数据通过加密传到开发者服务器,然后需要开发者解密得到开放数据。
\"在这里插入图片描述\"

根据小程序微信公众平台解释,步骤如下:
1、前端需要调用wx.login(),获取code;
2、使用code调用微信接口获取session_key:https://developers.weixin.qq.com/miniprogram/dev/api/open-api/login/code2Session.html
3、加密数据encryptedData和iv由前端提供
参考:获取手机号
4、用以上获取的encryptedData,session_key,iv解密

	/**
     * 获取信息
     */
    public static JSON  getUserInfo(String encryptedData,String sessionkey,String iv) {
        // 被加密的数据
        byte[] dataByte =  64.decode(encryptedData);
        // 加密秘钥
        byte[] keyByte =  64.decode(sessionkey);
        // 偏移量
        byte[] ivByte =  64.decode(iv);
        try {
            // 如果密钥不足16位,那么就补足.  这个if 中的内容很重要
            int   = 16;
            if (keyByte.length %   != 0) {
                int groups = keyByte.length /   + (keyByte.length %   != 0 ? 1 : 0);
                byte[] temp = new byte[groups *  ];
                Arrays.fill(temp, (byte) 0);
                System.arraycopy(keyByte, 0, temp, 0, keyByte.length);
                keyByte = temp;
            }
            // 初始化
            Security.addProvider(new BouncyCastleProvider());
            Cipher cipher = Cipher.getInstance(\"AES/CBC/PKCS7Padding\", \"BC\");
            SecretKeySpec spec = new SecretKeySpec(keyByte, \"AES\");
            AlgorithmParameters parameters = AlgorithmParameters.getInstance(\"AES\");
            parameters.init(new IvParameterSpec(ivByte));
            cipher.init(Cipher.DECRYPT_MODE, spec, parameters);// 初始化
            byte[] resultByte = cipher.doFinal(dataByte);
            if (null != resultByte && resultByte.length > 0) {
                String result = new String(resultByte, \"UTF-8\");
                return JSON .parse (result);
            }
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (InvalidParameterSpecException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (InvalidAlgorithmParameterException e) {
            e.printStackTrace();
        } catch (NoSuchProviderException e) {
            e.printStackTrace();
        }
        return null;
    }

问题

待完善。。。

参考:
获取手机号

收藏 打印