|
| 1 | +package me.chanjar.weixin.cp.util.crypto; |
| 2 | + |
| 3 | +import com.google.gson.JsonObject; |
| 4 | +import me.chanjar.weixin.common.util.crypto.SHA1; |
| 5 | +import me.chanjar.weixin.common.util.crypto.WxCryptUtil; |
| 6 | +import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder; |
| 7 | +import me.chanjar.weixin.common.error.WxRuntimeException; |
| 8 | +import org.apache.commons.codec.binary.Base64; |
| 9 | + |
| 10 | +import javax.crypto.Cipher; |
| 11 | +import javax.crypto.spec.IvParameterSpec; |
| 12 | +import javax.crypto.spec.SecretKeySpec; |
| 13 | +import java.nio.charset.StandardCharsets; |
| 14 | +import java.util.Arrays; |
| 15 | +import java.util.UUID; |
| 16 | + |
| 17 | +/** |
| 18 | + * 企业微信智能机器人 API 模式消息加解密工具. |
| 19 | + * |
| 20 | + * <p>机器人 API 模式使用机器人后台配置的 Token、EncodingAESKey 和机器人 ID, |
| 21 | + * 与企业应用 access_token 无关。</p> |
| 22 | + */ |
| 23 | +public class WxCpIntelligentRobotCryptUtil extends WxCryptUtil { |
| 24 | + |
| 25 | + public WxCpIntelligentRobotCryptUtil(String token, String encodingAesKey, String aiBotId) { |
| 26 | + super(token, encodingAesKey, aiBotId); |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * 解密机器人 API 模式的 JSON 回调消息. |
| 31 | + */ |
| 32 | + public String decrypt(String msgSignature, String timestamp, String nonce, String encryptedContent) { |
| 33 | + String signature = SHA1.gen(this.token, timestamp, nonce, encryptedContent); |
| 34 | + if (!signature.equals(msgSignature)) { |
| 35 | + throw new WxRuntimeException("加密消息签名校验失败"); |
| 36 | + } |
| 37 | + |
| 38 | + try { |
| 39 | + Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding"); |
| 40 | + cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(this.aesKey, "AES"), |
| 41 | + new IvParameterSpec(Arrays.copyOfRange(this.aesKey, 0, 16))); |
| 42 | + byte[] bytes = me.chanjar.weixin.common.util.crypto.PKCS7Encoder.decode( |
| 43 | + cipher.doFinal(Base64.decodeBase64(encryptedContent))); |
| 44 | + if (bytes.length < 20) { |
| 45 | + throw new WxRuntimeException("解密后数据长度异常,可能为错误的密文或EncodingAESKey"); |
| 46 | + } |
| 47 | + |
| 48 | + int plainTextLength = 0; |
| 49 | + for (int index = 16; index < 20; index++) { |
| 50 | + plainTextLength = (plainTextLength << 8) | (bytes[index] & 0xff); |
| 51 | + } |
| 52 | + int plainTextEnd = 20 + plainTextLength; |
| 53 | + if (plainTextLength < 0 || plainTextEnd > bytes.length) { |
| 54 | + throw new WxRuntimeException("解密后数据格式非法:消息长度不正确,可能为错误的密文或EncodingAESKey"); |
| 55 | + } |
| 56 | + |
| 57 | + String receiverId = new String(Arrays.copyOfRange(bytes, plainTextEnd, bytes.length), StandardCharsets.UTF_8); |
| 58 | + if (!this.appidOrCorpid.equals(receiverId)) { |
| 59 | + throw new WxRuntimeException("智能机器人ID不正确,请核实!"); |
| 60 | + } |
| 61 | + return new String(Arrays.copyOfRange(bytes, 20, plainTextEnd), StandardCharsets.UTF_8); |
| 62 | + } catch (WxRuntimeException e) { |
| 63 | + throw e; |
| 64 | + } catch (Exception e) { |
| 65 | + throw new WxRuntimeException(e); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * 加密机器人 API 模式的 JSON 回复消息. |
| 71 | + */ |
| 72 | + public String encrypt(String plainJson, String timestamp, String nonce) { |
| 73 | + String encryptedContent = encrypt(UUID.randomUUID().toString().replace("-", "").substring(0, 16), plainJson); |
| 74 | + JsonObject result = new JsonObject(); |
| 75 | + result.addProperty("encrypt", encryptedContent); |
| 76 | + result.addProperty("msg_signature", SHA1.gen(this.token, timestamp, nonce, encryptedContent)); |
| 77 | + result.addProperty("timestamp", timestamp); |
| 78 | + result.addProperty("nonce", nonce); |
| 79 | + return WxCpGsonBuilder.create().toJson(result); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * 解密 URL 校验请求中的 echostr. |
| 84 | + */ |
| 85 | + public String verifyUrl(String msgSignature, String timestamp, String nonce, String echoStr) { |
| 86 | + return decrypt(msgSignature, timestamp, nonce, echoStr); |
| 87 | + } |
| 88 | +} |
0 commit comments