通过 微信对话开放平台 应用绑定 开放API 回调地址配置地址后,通过 客服面板聊天发送消息
有时候消息能正常解密,有时候不能正常解密,代码使用的是
https://developers.weixin.qq.com/doc/aispeech/confapi/thirdapi/thirdapi.html#Java-%E7%A4%BA%E4%BE%8B
这里的Java示例代码,
正常解密的部分解密后在开头部分有部分乱码
-��'b�u7@�Ǻ $M<xml>
偶发性不能解密的部分解密报错 Given final block not properly padded
您好,请您对照文档检查一下您的解密方式,详情见文档:https://developers.weixin.qq.com/doc/aispeech/confapi/thirdkefu/recivemsg.html
接收到的报文
{
"encrypted": "dCKh2hYNKHm//eRRTv56T4758Bl4e7FO+g39hGr9qpbtBaLykdKofWJSI2nfNaKlCf4nxOgdmm+gjdmYpTNtD1m6Ce76NJuajByOz5RTmqhjJiRtTqCXCQIqDZA/0UEQYLy6cs5wHT1d2C9AT7nZhDLmXBMADy8Sd5oYQAem9Mbp8z1DHk6U9tV7MTJi/VoRljwhatprgA/yhBvCjAZ4U6NBJq7j654TdqwNxl7co61+wEEBd0ySoD6liUFOYRydL3q6qPrDUUxjDd7jqR8GMHUKKsZqcdWdQuYJr9/g25u4t7EEUwSwSC2UWZ5+B81HB7HLuEXjvuHVco9i7QTFsQZGG1oIgoqUheLklIZ7PQODcE+cIfMExuey5k1gqo9P2HusEk5P2rSOktBDApX/QwyLGmLHyhlLMSnGYeQcOeGQJKt2M8TbTGtYA+5se3Agg9lEb9OURB2/Bik9ZdIsEtmIpi4ALvu63O6rTBpM/5++X4ap8Ns2yiAeMgpYTA7iO/7MKgZeVQfczILzVePMk6vqE4oxMfQHW2MYF/QoiVbiMIYwePNEeSrjDRV+rQGdEn9YD79oLQ4ZpUynlXOB2AV0ui6GvR/IEIwDbS4AqPkl5uJ6Lz1VqrfIulkuP4lOabB/eQsbduhDuR5chu5pGAduTQvvb/XYAKV3VYIotQKMipTLsMZPHQnCkpQVJRvkFow9Q6CwOItdtCA8p+tPj6gf6EmX42xQnSlccsNVAierV38BMrf3eJMB67oeImF4"
}
解密代码
public class AESUtils {
public static Base64.Encoder encoder = Base64.getEncoder();
public static Base64.Decoder decoder = Base64.getDecoder();
public static String decrypt(String aesKey,String str) throws Exception {
byte[] secretKey = decoder.decode(aesKey.getBytes(StandardCharsets.UTF_8));
byte[] iv = new byte[16];
System.arraycopy(secretKey, 0, iv, 0, 16);
byte[] data = decoder.decode(str.getBytes(StandardCharsets.UTF_8));
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
SecretKey keySpec = new SecretKeySpec(secretKey, "AES");
IvParameterSpec ivps = new IvParameterSpec(iv);
cipher.init(Cipher.DECRYPT_MODE, keySpec, ivps);
return new String(cipher.doFinal(data), StandardCharsets.UTF_8);
}
}
String AesKey = "";
JSONObject requestBody = JSON.parseObject(msgBody);
String encryptedMsg = requestBody.getString("encrypted");
String jsonBody = AESUtils.decrypt(AesKey, encryptedMsg);
机器人ID:servicedb03c