All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.zopen.wechat.mp.util.MiniDecryptUtil Maven / Gradle / Ivy

There is a newer version: 1.0.5
Show newest version
package com.zopen.wechat.mp.util;

import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.util.Arrays;
import org.bouncycastle.util.encoders.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.AlgorithmParameters;
import java.security.Security;

// 小程序敏感数据解密
// 依赖:org.bouncycastle.bcprov-jdk15on
public class MiniDecryptUtil {

    private static final Logger logger = LoggerFactory.getLogger(MiniDecryptUtil.class);

    public static String decrypt(String encryptedData, String sessionKey, String iv) {

        // 被加密的数据
        byte[] dataByte = Base64.decode(encryptedData);
        // 加密秘钥
        byte[] keyByte = Base64.decode(sessionKey);
        // 偏移量
        byte[] ivByte = Base64.decode(iv);

        try {
            // 如果密钥不足16位,那么就补足.  这个if 中的内容很重要
            int base = 16;
            if (keyByte.length % base != 0) {
                int groups = keyByte.length / base + (keyByte.length % base != 0 ? 1 : 0);
                byte[] temp = new byte[groups * base];
                Arrays.fill(temp, (byte) 0);
                System.arraycopy(keyByte, 0, temp, 0, keyByte.length);
                keyByte = temp;
            }
            // 初始化
            Security.addProvider(new BouncyCastleProvider());
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding", "BC");
            SecretKeySpec spec = new SecretKeySpec(keyByte, "AES");
            AlgorithmParameters parameters = AlgorithmParameters.getInstance("AES");
            parameters.init(new IvParameterSpec(ivByte));
            cipher.init(Cipher.DECRYPT_MODE, spec, parameters);// 初始化
            byte[] resultByte = cipher.doFinal(dataByte);
            if (null != resultByte && resultByte.length > 0) {
                return new String(resultByte, "UTF-8");
            }
        } catch (Exception e) {
            logger.error("小程序数据解密失败", e);
        }
        return null;
    }

//    public static void main(String[] args) {
//        try {
//            userInfoTest();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
//
//    private static void userInfoTest() throws Exception {
//        String sessionKey = "tiihtNczf5v6AKRyjwEUhQ==";
//        String encryptedData =
//                "CiyLU1Aw2KjvrjMdj8YKliAjtP4gsMZM" +
//                        "QmRzooG2xrDcvSnxIMXFufNstNGTyaGS" +
//                        "9uT5geRa0W4oTOb1WT7fJlAC+oNPdbB+" +
//                        "3hVbJSRgv+4lGOETKUQz6OYStslQ142d" +
//                        "NCuabNPGBzlooOmB231qMM85d2/fV6Ch" +
//                        "evvXvQP8Hkue1poOFtnEtpyxVLW1zAo6" +
//                        "/1Xx1COxFvrc2d7UL/lmHInNlxuacJXw" +
//                        "u0fjpXfz/YqYzBIBzD6WUfTIF9GRHpOn" +
//                        "/Hz7saL8xz+W//FRAUid1OksQaQx4CMs" +
//                        "8LOddcQhULW4ucetDf96JcR3g0gfRK4P" +
//                        "C7E/r7Z6xNrXd2UIeorGj5Ef7b1pJAYB" +
//                        "6Y5anaHqZ9J6nKEBvB4DnNLIVWSgARns" +
//                        "/8wR2SiRS7MNACwTyrGvt9ts8p12PKFd" +
//                        "lqYTopNHR1Vf7XjfhQlVsAJdNiKdYmYV" +
//                        "oKlaRv85IfVunYzO0IKXsyl7JCUjCpoG" +
//                        "20f0a04COwfneQAGGwd5oa+T8yO5hzuy" +
//                        "Db/XcxxmK01EpqOyuxINew==";
//        String iv = "r7BXXKkLb8qrSNn05n0qiA==";
//        String data = decrypt(encryptedData, sessionKey, iv);
//        System.out.println(data);
//    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy