package com.chenpi;
import cn.hutool.crypto.Mode;
import cn.hutool.crypto.Padding;
import cn.hutool.crypto.symmetric.AES;
import java.nio.charset.StandardCharsets;
import java.security.AlgorithmParameters;
import java.security.Security;
import java.util.Arrays;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
@Slf4j
public class WxUtils {
public static String decryptWxData(String encryptedData, String sessionKey, String iv) {
try {
Security.addProvider(new BouncyCastleProvider());
SecretKeySpec spec = new SecretKeySpec(Base64.decodeBase64(sessionKey), "AES");
AlgorithmParameters parameters = AlgorithmParameters.getInstance("AES");
parameters.init(new IvParameterSpec(Base64.decodeBase64(iv)));
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding", "BC");
cipher.init(Cipher.DECRYPT_MODE, spec, parameters);
byte[] resultByte = cipher.doFinal(Base64.decodeBase64(encryptedData));
if (null != resultByte && resultByte.length > 0) {
String result = new String(resultByte, StandardCharsets.UTF_8);
log.info(">>>>> 微信加密数据解析结果:{}", result);
return result;
}
} catch (Exception e) {
log.error(">>>>> 微信加密数据解析失败:", e);
}
return null;
}
public static String decryptWxDataOfHutool(String encryptedData, String sessionKey, String iv) {
AES aes = new AES(Mode.CBC, Padding.NoPadding, Base64.decodeBase64(sessionKey),
Base64.decodeBase64(iv));
byte[] resultByte = aes.decrypt(Base64.decodeBase64(encryptedData));
if (null != resultByte && resultByte.length > 0) {
int padNum = resultByte[resultByte.length - 1];
if (padNum < 1 || padNum > 32) {
padNum = 0;
}
resultByte = Arrays.copyOfRange(resultByte, 0, resultByte.length - padNum);
String result = new String(resultByte, StandardCharsets.UTF_8);
log.info(">>>>> 微信加密数据解析结果:{}", result);
return result;
}
return null;
}
public static void main(String[] args) {
String encryptedData = "9d9sSqhjoZI9DgxC7f+wAnciEhgHv0WVitr6UAF8Wf7mS7PJniGsna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Y5LP2Ny/rD3X6VuO9//WQpszEFprosDZcb0SBTG3dxeIr4lb1I9MHVtJoEOfwLV8WtUN6Rdn/kGZ1nInSAuMyqZLBRfAX2603lv91SkghnY+FT3PCuqaDPDEDUgd/ozM9DWfaI9eaC/OC5NU+WqW9s17Q3+cLaWF7VZswloRuS/8LMrahxmTcDnIFRV+S1SeyV+Bdm79qseeQEdsSVDdLO2aJ1iViLxzbebb8pUIhx10JMacUrOBFVB+J76lLR4AiVT8rydLRA6uq5fFCMlg2GDXVBkbgqUoBVuiozGLKjPPSmfbk5s9JMxa8zSTZ6Vq4EefAeWoSKiWKHtBkFWcUEbp6I2+YycS3kqP8kC069csssGcHqDA6ouP+wEkpoDVS3vFi4fVkpjBtpY5Fu+NIQYUfiC54EA2JMyBS3lHGv01oPW54o4uSZoglcb2S6Yf9fkgS48ft0xE7Yi0YTQBct2jVTOSq5jjwxb26pjAhfkkccdvjZD3wHZSl/dNEsOWdKrdhE6H93Rvqny/nvQFX0DDzZPs1O2rbRAGxgHtKSDfxjWC6g==";
String sessionKey = "VrgQ9xkONam93c+ULD0yLA==";
String iv = "W4ND0KBskxNNnSRCuapaqQ==";
decryptWxDataOfHutool(encryptedData, sessionKey, iv);
decryptWxData(encryptedData, sessionKey, iv);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91