java 后端解密获取微信的unionId

一、创建 解密代码类

 

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.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 {
            // java是没有
            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) {
            e.printStackTrace();
        } catch (Exception e) {
            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;
    }

二、创建测试类

/**
 *   如果有,直接返回 ,程序结束
 *   如果没有,将用于解密的session_key获取
 *   传入encryptedData等加密数据
 *   对加密数据进行逆解密
 *   拿到unionID 返回,程序结
 *
 * userInfo TODO 注意一下:通过user.getInfo获取的和通过button授权获取的格式不一样
 *
 */
public static void main(String[] args) {
    Map resUserInfo = new HashMap<>();
    String session_key="XXX"; //  参考微信开发文档中根据code获取session_key

    String iv = "XXX"; // 微信小程序中调用getUserInfo方法时微信返给的 e.detail.iv
    String encryptedData ="XXX";  // 被加密的数据,微信小程序中调用getUserInfo方法时微信返给的e.detail.encryptedData
    byte[] dataByte = Base64.decodeBase64(encryptedData);
    // 加密秘钥
    byte[] keyByte = Base64.decodeBase64(session_key);
    // 偏移量
    byte[] ivByte = Base64.decodeBase64(iv);
    String newuserInfo = "";
    try {
        AES aes = new AES();
        byte[] resultByte = aes.decrypt(dataByte, keyByte, ivByte);
        if (null != resultByte && resultByte.length > 0) {
            newuserInfo = new String(resultByte, "UTF-8");
            System.out.println("获取到的unionId:"+JSONObject.parseObject(newuserInfo).get("unionId"));
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

3、导入所需要的jar

https://download.csdn.net/download/qq_33238562/11311800

4、 增加依赖


   org.bouncycastle
   bcprov-jdk15on
   1.55

 

你可能感兴趣的:(java,springboot)