你上面的流程是对的,前端请求后端接口时传递
sessionKey、phoneEncryptedData、phoneEncryptedIv、userEncryptedData和userEncryptedIv
后端拿到数据后进行解密即可:
sessionKey、userEncryptedData和userEncryptedIv可以对应解析出用户信息(openid、unoinid、nickName和avatarUrl);sessionKey、phoneEncryptedData、phoneEncryptedIv可以解密出用户手机号码purePhoneNumber
/**
* 小程序 数据解密
*
* @param encryptData 加密数据
* @param iv 对称解密算法初始向量
* @param sessionKey 对称解密秘钥
* @return 解密数据
*/
public static String wxDecrypt(String encryptData, String iv, String sessionKey) throws Exception {
AlgorithmParameters algorithmParameters = AlgorithmParameters.getInstance("AES");
algorithmParameters.init(new IvParameterSpec(Base64.decodeBase64(iv)));
Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(Base64.decodeBase64(sessionKey), "AES"), algorithmParameters);
byte[] decode = PKCS7Decode(cipher.doFinal(Base64.decodeBase64(encryptData)));
return new String(decode, StandardCharsets.UTF_8);
}
得到用户手机号和用户openid/unionId后就可以对应登录或者注册了。