当前位置: 首页 > article >正文

微信小程序消息推送解密

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;
    }
}


http://www.kler.cn/a/556387.html

相关文章:

  • c++:stack与deque
  • 英文字体:极简现代浓缩未来派科技海报标题排版无衬线字体 PODIUM Sharp Font
  • SRS编译arm版本出错 ERROR: opus not found using pkg-config
  • 体育电竞比分网开发流程
  • 大数据运维实战:通过自定义Hooks优化Spark Catalyst,提升Spark性能
  • 一文详解U盘启动Legacy/UEFI方式以及GPT/MBR关系
  • qt.qpa.fonts: Unable to open default EUDC font: “EUDC.TTE“
  • Redis三剑客解决方案
  • selenium爬取苏宁易购平台某产品的评论
  • 【QT中的一些高级数据结构,持续更新中...】
  • 汽车智能制造企业数字化转型SAP解决方案总结
  • Redis数据结构总结-listPack
  • OpenMetadata MySQL数据质量治理实现分析
  • 【Alertmanager】alertmanager告警多种通知方式--企业微信告警、钉钉告警、电话和短信告警
  • 【Java高级篇】——第15篇:深入探讨Spring Boot与微服务架构
  • Java八股文(下)
  • 从零开始玩转TensorFlow:小明的机器学习故事 2
  • scala中为什么能用常量的地方就不用变量
  • Git配置个人和公司的提交信息,通过‘目录配置‘
  • go.mod 里的 toolchain 怎么去掉