java程序电话号码_JAVA获取微信小程序手机号

用 session_key ,encryptedData ,iv 解密获取手机号

import com.anmoyi.common.Base64; //此包为自己封装的,只要能解密base64都行

import java.security.spec.AlgorithmParameterSpec;

import javax.crypto.Cipher;

import javax.crypto.spec.IvParameterSpec;

import javax.crypto.spec.SecretKeySpec;

/**

* 解密用户手机号

* @param keyStr sessionkey

* @param ivStr ivData

* @param encDataStr 带解密数据

* @return

* @throws Exception

* @date 2019年05月08日

*/

public static String decrypt(String keyStr, String ivStr, String encDataStr)throws Exception {

byte[] encData = Base64.decode(encDataStr);

byte[] iv =Base64.decode(ivStr);

byte[] key = Base64.decode(keyStr);

AlgorithmParameterSpec ivSpec = new IvParameterSpec(iv);

Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

SecretKeySpec keySpec = new SecretKeySpec(key, "AES");

cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);

return new String(cipher.doFinal(encData),"UTF-8");

}

结果返回

{

"phoneNumber": "187*********",

"purePhoneNumber": "187********",

"countryCode": "86",

"watermark":

{

"appid":"APPID",

"timestamp":TIMESTAMP

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Java获取微信小程序用户手机号,需要使用微信开放平台提供的接口。具体步骤如下: 1. 在微信开放平台创建小程序,并获取程序的AppID和AppSecret。 2. 在小程序获取用户授权,包括获取用户手机号的授权。 3. 在Java中发起HTTPS请求,请求微信提供的接口,获取用户手机号。 以下是示例代码: ```java import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; public class GetPhoneNumber { public static void main(String[] args) throws IOException { String appid = "your_appid"; String secret = "your_appsecret"; String js_code = "user_js_code"; String grant_type = "authorization_code"; // 发起HTTPS请求,获取openid和session_key String url = "https://api.weixin.qq.com/sns/jscode2session?appid=" + appid + "&secret=" + secret + "&js_code=" + js_code + "&grant_type=" + grant_type; URL obj = new URL(url); HttpURLConnection con = (HttpURLConnection) obj.openConnection(); con.setRequestMethod("GET"); BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); String inputLine; StringBuffer response = new StringBuffer(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); // 解析返回的JSON数据,获取openid和session_key JSONObject jsonObject = new JSONObject(response.toString()); String openid = jsonObject.getString("openid"); String session_key = jsonObject.getString("session_key"); // 发起HTTPS请求,获取用户手机号 String encryptedData = "user_encrypted_data"; String iv = "user_iv"; url = "https://api.weixin.qq.com/wxa/getphonenumber?access_token=" + access_token; String data = "encryptedData=" + URLEncoder.encode(encryptedData, "UTF-8") + "&iv=" + URLEncoder.encode(iv, "UTF-8") + "&session_key=" + session_key; obj = new URL(url); con = (HttpURLConnection) obj.openConnection(); con.setRequestMethod("POST"); con.setDoOutput(true); DataOutputStream wr = new DataOutputStream(con.getOutputStream()); wr.writeBytes(data); wr.flush(); wr.close(); in = new BufferedReader(new InputStreamReader(con.getInputStream())); response = new StringBuffer(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); // 解析返回的JSON数据,获取用户手机号 jsonObject = new JSONObject(response.toString()); String phoneNumber = jsonObject.getString("phoneNumber"); System.out.println(phoneNumber); } } ``` 注意:以上示例代码仅供参考,具体实现需要根据实际情况进行调整。同时,获取用户手机号需要用户授权,因此需要在小程序获取用户授权并获取用户手机号
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值