微信小程序解密用户信息和手机号

话不多说,直接上代码

package com.juqitech.zb.weixin.service.impl;

import com.juqitech.zb.common.util.FastJsonUtils;
import com.juqitech.zb.common.util.StringUtil;
import org.apache.commons.codec.binary.Base64;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.spec.AlgorithmParameterSpec;

/**
 * @author ypf
 * @date 2018/9/27
 * 微信小程序解密用户信息和手机号
 */
public class MiniProgramTest {

    public static void main(String args[]) throws Exception {
        MiniProgramTest test=new MiniProgramTest();
        /**
         * 解密手机号
         */
        /*String encryptedData="mgxuts7uEbmLQKcW329VaQTua4MIdRXKTm+YnFSaOGYK1zd+uKEhhGj5cfU8g5oP1RSk4PXbDrHuQY8MI0zwtfQL2+Zfq0NDm11w8efNCclVSxEbHdUYeJUkhhTsNkZgt3SzYv5ptqTNyJ7TOTaX78wkN1uWTEV2sa/imvdnBclp0Qim0TS3MGoaSyE09MqTvbeG9Z1PjxsDwjVH0FxG1Q==";
        String sessionKey="Fn6r4gAIOiZI0JBXn4hQ0w==";
        String iv="SG386etdytWA3sOXHxqfnw==";
        System.out.println("==================================================");
        System.out.println(test.getPhoneNumber(encryptedData,sessionKey,iv)+"==================================================");;
        System.out.println("==================================================");*/

        /**
         * 解密用户信息
         */
        String encryptedData="CiyLU1Aw2KjvrjMdj8YKliAjtP4gsMZMQmRzooG2xrDcvSnxIMXFufNstNGTyaGS9uT5geRa0W4oTOb1WT7fJlAC+oNPdbB+3hVbJSRgv+4lGOETKUQz6OYStslQ142dNCuabNPGBzlooOmB231qMM85d2/fV6ChevvXvQP8Hkue1poOFtnEtpyxVLW1zAo6/1Xx1COxFvrc2d7UL/lmHInNlxuacJXwu0fjpXfz/YqYzBIBzD6WUfTIF9GRHpOn/Hz7saL8xz+W//FRAUid1OksQaQx4CMs8LOddcQhULW4ucetDf96JcR3g0gfRK4PC7E/r7Z6xNrXd2UIeorGj5Ef7b1pJAYB6Y5anaHqZ9J6nKEBvB4DnNLIVWSgARns/8wR2SiRS7MNACwTyrGvt9ts8p12PKFdlqYTopNHR1Vf7XjfhQlVsAJdNiKdYmYVoKlaRv85IfVunYzO0IKXsyl7JCUjCpoG20f0a04COwfneQAGGwd5oa+T8yO5hzuyDb/XcxxmK01EpqOyuxINew==";
        String sessionKey="tiihtNczf5v6AKRyjwEUhQ==";
        String iv="r7BXXKkLb8qrSNn05n0qiA==";
        System.out.println("==================================================");
        System.out.println(test.getUserInfo(encryptedData,sessionKey,iv)+"==================================================");;
        System.out.println("==================================================");

    }

    public WechatUserInfoVO getUserInfo(String encryptedData, String sessionKey, String iv) throws Exception {
        String result= decrypt(sessionKey,iv,encryptedData);
        if(StringUtil.isNotBlank(result)){
            return FastJsonUtils.json2Bean(result,WechatUserInfoVO.class);
        }
        return null;
    }

    public String getPhoneNumber(String encryptedData, String sessionKey, String iv) throws Exception{
        String result= decrypt(sessionKey,iv,encryptedData);
        if(StringUtil.isNotBlank(result)){
            PhoneNumberInfo phoneNumberInfo= FastJsonUtils.json2Bean(result,PhoneNumberInfo.class);
            if(phoneNumberInfo!=null){
                return phoneNumberInfo.getPhoneNumber();
            }
        }
        return null;
    }

    public static String decrypt(String sessionKey,String ivData, String encrypData) throws Exception {
        byte[] encData = Base64.decodeBase64(encrypData);
        byte[] iv = Base64.decodeBase64(ivData);
        byte[] key = Base64.decodeBase64(sessionKey);
        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");
    }


    static class WechatUserInfoVO {
        private String openid;//用户的唯一标识
        private String nickname;//用户昵称
        private Integer sex;//用户的性别,值为1时是男性,值为2时是女性,值为0时是未知
        private String province;//用户个人资料填写的省份
        private String city;//普通用户个人资料填写的城市
        private String country;//国家,如中国为CN
        private String headimgurl;//用户头像,最后一个数值代表正方形头像大小(有0、46、64、96、132数值可选,0代表640*640正方形头像),用户没有头像时该项为空。若用户更换头像,原有头像URL将失效。
        private String privilege;//用户特权信息,json 数组,如微信沃卡用户为(chinaunicom)
        private String unionid;//只有在用户将公众号绑定到微信开放平台帐号后,才会出现该字段。
        private String sessionKey;
        private String unionId;

        public String getOpenid() {
            return openid;
        }

        public void setOpenid(String openid) {
            this.openid = openid;
        }

        public String getNickname() {
            return nickname;
        }

        public void setNickname(String nickname) {
            this.nickname = nickname;
        }

        public Integer getSex() {
            return sex;
        }

        public void setSex(Integer sex) {
            this.sex = sex;
        }

        public String getProvince() {
            return province;
        }

        public void setProvince(String province) {
            this.province = province;
        }

        public String getCity() {
            return city;
        }

        public void setCity(String city) {
            this.city = city;
        }

        public String getCountry() {
            return country;
        }

        public void setCountry(String country) {
            this.country = country;
        }

        public String getHeadimgurl() {
            return headimgurl;
        }

        public void setHeadimgurl(String headimgurl) {
            this.headimgurl = headimgurl;
        }

        public String getPrivilege() {
            return privilege;
        }

        public void setPrivilege(String privilege) {
            this.privilege = privilege;
        }

        public String getUnionid() {
            return unionid;
        }

        public void setUnionid(String unionid) {
            this.unionid = unionid;
        }

        public String getSessionKey() {
            return sessionKey;
        }

        public void setSessionKey(String sessionKey) {
            this.sessionKey = sessionKey;
        }

        public String getUnionId() {
            return unionId;
        }

        public void setUnionId(String unionId) {
            this.unionId = unionId;
        }

        @Override
        public String toString() {
            return "WechatUserInfoVO{" +
                    "openid='" + openid + '\'' +
                    ", nickname='" + nickname + '\'' +
                    ", sex=" + sex +
                    ", province='" + province + '\'' +
                    ", city='" + city + '\'' +
                    ", country='" + country + '\'' +
                    ", headimgurl='" + headimgurl + '\'' +
                    ", privilege='" + privilege + '\'' +
                    ", unionid='" + unionid + '\'' +
                    ", sessionKey='" + sessionKey + '\'' +
                    ", unionId='" + unionId + '\'' +
                    '}';
        }
    }

    static class PhoneNumberInfo{
        private String phoneNumber;
        private String purePhoneNumber;
        private String countryCode;


        public String getPhoneNumber() {
            return phoneNumber;
        }

        public void setPhoneNumber(String phoneNumber) {
            this.phoneNumber = phoneNumber;
        }

        public String getPurePhoneNumber() {
            return purePhoneNumber;
        }

        public void setPurePhoneNumber(String purePhoneNumber) {
            this.purePhoneNumber = purePhoneNumber;
        }

        public String getCountryCode() {
            return countryCode;
        }

        public void setCountryCode(String countryCode) {
            this.countryCode = countryCode;
        }

    }


}


你可能感兴趣的:(微信小程序解密用户信息和手机号)