微信小程序消息推送解密

java 复制代码
package com.test.main.b2b;

import org.apache.commons.codec.binary.Base64;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.util.Arrays;


/**
 * @author 
 * @version 1.0
 * @description: 解谜微信小程序消息通知
 * @date 2025/2/20 15:29
 */
public class 支付回调解密 {
    // EncodingAESKey(消息加密密钥)
    public static String encodingAesKey = "sdIeXprnCzbVmDIp";
    // Encrypt Msg
    public static String encryptedData = "urMQfiTkA+nLIV/gpAxwQaFurMQfiTkA+nLIV/gpAxwQaFurMQfiTkA+nLIV/gpAxwQaFurMQfiTkA+nLIV/gpAxwQaFurMQfiTkA+nLIV/gpAxwQaFurMQfiTkA+nLIV/gpAxwQaF";

    public static void main(String[] args) throws Exception {
    	// 请替换成你的真实数据
        String decrypt = decrypt(encryptedData, encodingAesKey);
        System.out.println(decrypt);
    }

    public static String decrypt(String encryptedMsg, String encodingAesKey) throws Exception {
        // Base64解码密钥(微信EncodingAESKey是Base64编码的)
        byte[] aesKey = Base64.decodeBase64(encodingAesKey + "=");
        byte[] original;
        try {
            // 设置解密模式为AES的CBC模式
            Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
            SecretKeySpec key_spec = new SecretKeySpec(aesKey, "AES");
            IvParameterSpec iv = new IvParameterSpec(Arrays.copyOfRange(aesKey, 0, 16));
            cipher.init(Cipher.DECRYPT_MODE, key_spec, iv);
            // 使用BASE64对密文进行解码
            byte[] encrypted = Base64.decodeBase64(encryptedMsg);
            // 解密
            original = cipher.doFinal(encrypted);
        } catch (Exception e) {
            e.printStackTrace();
            throw new AesException(AesException.DecryptAESError);
        }
        String xmlContent;
        try {
            // 去除补位字符
            byte[] bytes = PKCS7Encoder.decode(original);
            // 分离16位随机字符串,网络字节序和AppId
            byte[] networkOrder = Arrays.copyOfRange(bytes, 16, 20);
            int xmlLength = recoverNetworkBytesOrder(networkOrder);
            xmlContent = new String(Arrays.copyOfRange(bytes, 20, 20 + xmlLength), "UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
            throw new AesException(AesException.IllegalBuffer);
        }
        return xmlContent;
    }

    // 还原4个字节的网络字节序
    static int recoverNetworkBytesOrder(byte[] orderBytes) {
        int sourceNumber = 0;
        for (int i = 0; i < 4; i++) {
            sourceNumber <<= 8;
            sourceNumber |= orderBytes[i] & 0xff;
        }
        return sourceNumber;
    }
}
相关推荐
流烟默5 小时前
vue和微信小程序处理markdown格式数据
前端·vue.js·微信小程序
家里有只小肥猫8 小时前
uniApp小程序保存canvas图片
前端·小程序·uni-app
低代码布道师8 小时前
性格测评小程序10生成报告
低代码·小程序
科技小E8 小时前
EasyRTC:基于WebRTC与P2P技术,开启智能硬件音视频交互的全新时代
网络·网络协议·小程序·webrtc·p2p·智能硬件·视频监控
乔冠宇10 小时前
微信小程序中将图片截图为正方形(自动居中)
微信小程序·小程序·typescript·uniapp
播播资源10 小时前
ChatGPT付费创作系统V3.1.3独立版 WEB端+H5端+小程序端 (DeepSeek高级通道+推理输出格式)安装教程
前端·ai·chatgpt·ai作画·小程序·deepseek·deepseek-v3
V+zmm1013413 小时前
在线办公小程序(springboot论文源码调试讲解)
vue.js·spring boot·微信小程序·小程序·毕业设计
css趣多多13 小时前
小程序的分包
小程序
V+zmm1013414 小时前
自驾游拼团小程序的设计与实现(ssm论文源码调试讲解)
java·数据库·微信小程序·小程序·毕业设计
寰宇软件15 小时前
PHP房屋出租出售高效预约系统小程序源码
前端·小程序·uni-app·vue·php