天天看點

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<String, String> 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、 增加依賴

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