欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

微信小程序 Java 后台获取电话号码

最编程 2024-07-10 17:52:03
...

小程序端:

  wx.request({
	url: registerphone, //自己的地址
	data: {
	  openid: openid,
	  encryptedData: encryptedData, //手机加密数据
	  iv: iv, // 加密iv
	  session_key: session_key,// 加密key
	},
	method: "post",
	header: {
	  "content-type": "application/x-www-form-urlencoded",
	},
	success: (resp) => {
	  console.log(resp);// 返回手机号
	},
  });

springboot后台:

pom.xml maven 依赖包:

        <dependency>
            <groupId>org.bouncycastle</groupId>
            <artifactId>bcprov-jdk15on</artifactId>
            <version>1.70</version>
        </dependency>

http 后台代码:


    @PostMapping("/registerphone")
    public String registerphone(String openid, String encryptedData, String iv, String session_key)
    {
        String retS = "";
        String appid = "wxc32bd5819d7a1cc5";
        try{
            JSONObject decryptObject = NativeUtils.decrypt(appid, encryptedData, session_key, iv);

            //logger.info(decryptObject.toJSONString());
            retS = decryptObject.getString("phoneNumber");

        } catch (Exception ex){

        }

        return retS;
    }


//---------------------------------------------------------------------------------
// 解密接口

    /**
	 * 解密数据
	 * @return
	 * @throws Exception
	 */
	public static JSONObject decrypt(String appId, String encryptedData, String sessionKey, String iv){
		try {
			byte[] resultByte = decrypt(Base64.decodeBase64(encryptedData),Base64.decodeBase64(sessionKey),Base64.decodeBase64(iv));
			if(null != resultByte && resultByte.length > 0){
				String result = new String(WxPKCS7Encoder.decode(resultByte));
				JSONObject jsonObject = JSONObject.parseObject(result);
				String decryptAppId = jsonObject.getJSONObject("watermark").getString("appid");
				if(!appId.equals(decryptAppId)){
					return null;
				}
				return jsonObject;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	static {
		Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
	}
	/**
	 * AES解密
	 *
	 * @param content
	 *            密文
	 * @return
	 * @throws InvalidAlgorithmParameterException
	 * @throws NoSuchProviderException
	 */
	public static byte[] decrypt(byte[] content, byte[] keyByte, byte[] ivByte) throws InvalidAlgorithmParameterException {
		try {
			Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
			Key sKeySpec = new SecretKeySpec(keyByte, "AES");
			cipher.init(Cipher.DECRYPT_MODE, sKeySpec, generateIV(ivByte));// 初始化
			byte[] result = cipher.doFinal(content);
			return result;
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (NoSuchPaddingException e) {
			e.printStackTrace();
		} catch (InvalidKeyException e) {
			e.printStackTrace();
		} catch (IllegalBlockSizeException e) {
			e.printStackTrace();
		} catch (BadPaddingException e) {
			e.printStackTrace();
		} catch (NoSuchProviderException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	// 生成iv
	public static AlgorithmParameters generateIV(byte[] iv) throws Exception {
		AlgorithmParameters params = AlgorithmParameters.getInstance("AES");
		params.init(new IvParameterSpec(iv));
		return params;
	}

在网上找了好久,东拼洗凑居然能用,希望对写小程序的朋友有帮助。