(Java版)微信小程序解析加密参数获取手机号

	@RequestMapping(value = "/getPhoneNumber")
	public void getPhoneNumber(String code,String iv,String encryptedData) throws Exception {
		log.info("===解密消息获取手机号码===");
		AES aes = new AES();
		try {
			String openId = HttpRequest.sendGet("https://api.weixin.qq.com/sns/jscode2session?appid=" + PayConfigUtil.APP_ID
					+ "&secret=" + PayConfigUtil.APP_SECRET + "&js_code=" + code + "&grant_type=authorization_code", code);
			JSONObject json = JSONObject.fromObject(openId);
			String sessionKey = json.getString("session_key");
			byte[] resultByte = aes.decrypt(Base64.decodeBase64(encryptedData),Base64.decodeBase64(sessionKey), Base64.decodeBase64(iv));
			String userInfo = new String(resultByte, "UTF-8");
			log.info("-----"+userInfo + "---");
			Utils.jsonUtilCode(UtilState.SUCCESS, userInfo);
		} catch (Exception e) {
			Utils.jsonUtilCode(UtilState.ERROR, e.getMessage());
			e.printStackTrace();
		}
	}
import java.io.UnsupportedEncodingException;
import java.security.AlgorithmParameters;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.Security;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Base64;
import org.bouncycastle.jce.provider.BouncyCastleProvider;  

public class AES {  
	public static boolean initialized = false;  
	
	/**
	 * AES解密
	 * @param content 密文
	 * @return
	 * @throws InvalidAlgorithmParameterException 
	 * @throws NoSuchProviderException 
	 */
	public byte[] decrypt(byte[] content, byte[] keyByte, byte[] ivByte) throws InvalidAlgorithmParameterException {
		initialize();
		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;
	}  
	
	public static void initialize(){  
        if (initialized) return;  
        Security.addProvider(new BouncyCastleProvider());  
        initialized = true;  
    }
	//生成iv  
    public static AlgorithmParameters generateIV(byte[] iv) throws Exception{  
        AlgorithmParameters params = AlgorithmParameters.getInstance("AES");  
        params.init(new IvParameterSpec(iv));  
        return params;  
    }  
}  

 

你可能感兴趣的:((Java版)微信小程序解析加密参数获取手机号)