|
|
|
@@ -1,6 +1,7 @@ |
|
|
|
package com.xueyi.system.wechat.service; |
|
|
|
|
|
|
|
import com.alibaba.fastjson2.JSON; |
|
|
|
import com.xueyi.common.core.utils.core.CryptoUtil; |
|
|
|
import com.xueyi.common.redis.utils.RedisUtil; |
|
|
|
import com.xueyi.system.wechat.domain.bean.WeChatSessionResponse; |
|
|
|
import org.slf4j.Logger; |
|
|
|
@@ -61,28 +62,34 @@ public class WeChatService { |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public String decrypt(String encryptedData, String iv, String sessionKey) { |
|
|
|
try { |
|
|
|
// Base64 解码 |
|
|
|
byte[] sessionKeyBytes = Base64.getDecoder().decode(sessionKey); |
|
|
|
byte[] encryptedDataBytes = Base64.getDecoder().decode(encryptedData); |
|
|
|
byte[] ivBytes = Base64.getDecoder().decode(iv); |
|
|
|
public String decrypt(String encryptedData, String iv, String wechatAuth) throws Exception{ |
|
|
|
|
|
|
|
// 创建 AES 密钥对象 |
|
|
|
SecretKey secretKey = new SecretKeySpec(sessionKeyBytes, "AES"); |
|
|
|
String str = CryptoUtil.decrypt(wechatAuth); |
|
|
|
WeChatSessionResponse obj = JSON.parseObject(str, WeChatSessionResponse.class); |
|
|
|
// Base64 解码 |
|
|
|
byte[] sessionKeyBytes = Base64.getDecoder().decode(obj.getSessionKey()); |
|
|
|
byte[] encryptedDataBytes = Base64.getDecoder().decode(encryptedData); |
|
|
|
byte[] ivBytes = Base64.getDecoder().decode(iv); |
|
|
|
log.info("sessionKeyBytes: {}", new String(sessionKeyBytes)); |
|
|
|
log.info("encryptedDataBytes: {}", new String(encryptedDataBytes)); |
|
|
|
log.info("ivBytes: {}", new String(ivBytes)); |
|
|
|
|
|
|
|
// 创建 AES 解密器 |
|
|
|
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); |
|
|
|
cipher.init(Cipher.DECRYPT_MODE, secretKey, new IvParameterSpec(ivBytes)); |
|
|
|
// 创建 AES 密钥对象 |
|
|
|
SecretKey secretKey = new SecretKeySpec(sessionKeyBytes, "AES"); |
|
|
|
log.info("secretKey: {} 1===", secretKey.getAlgorithm()); |
|
|
|
|
|
|
|
// 解密 |
|
|
|
byte[] decryptedBytes = cipher.doFinal(encryptedDataBytes); |
|
|
|
String decoded = new String(decryptedBytes, "UTF-8"); |
|
|
|
// 创建 AES 解密器 |
|
|
|
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); |
|
|
|
log.info("cipher: {} 2===", cipher.getAlgorithm()); |
|
|
|
cipher.init(Cipher.DECRYPT_MODE, secretKey, new IvParameterSpec(ivBytes)); |
|
|
|
|
|
|
|
// 解密 |
|
|
|
byte[] decryptedBytes = cipher.doFinal(encryptedDataBytes); |
|
|
|
log.info("decryptedBytes: {} 3===", new String(decryptedBytes)); |
|
|
|
String decoded = new String(decryptedBytes, "UTF-8"); |
|
|
|
log.info("decoded: {} 4===", decoded); |
|
|
|
return decoded; |
|
|
|
|
|
|
|
return decoded; |
|
|
|
} catch (Exception e) { |
|
|
|
throw new IllegalArgumentException("Illegal Buffer", e); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|