javax.crypto.AEADBadTagException: Tag mismatch!
at java.base/com.sun.crypto.provider.GaloisCounterMode$GCMDecrypt.doFinal(GaloisCounterMode.java:1395) ~[na:na]
at java.base/com.sun.crypto.provider.GaloisCounterMode.engineDoFinal(GaloisCounterMode.java:406) ~[na:na]
at java.base/javax.crypto.Cipher.doFinal(Cipher.java:2205) ~[na:na]
最近对接支付遇到这个问题,分享一下我的解决办法:
1,很多人提到的,确认你的api密钥正确,其实如果你可以正常下单,那应该是正确的了,因为下单的时候也用到密钥,如果真的是密钥错误那应该是无法下单。
2,我就是卡在这里
//更改前
JSONObject resource = jsonObject.getJSONObject("resource");
AesUtil aesUtil = new AesUtil("你的密钥".getBytes());
String s = aesUtil.decryptToString(
resource.getString("associated_data").getBytes(),
resource.getString("nonce").getBytes(),
resource.getString("ciphertext")
);
//更改后
JSONObject resource = jsonObject.getJSONObject("resource");
AesUtil aesUtil = new AesUtil("你的密钥".getBytes(StandardCharsets.UTF_8));
String s = aesUtil.decryptToString(
resource.getString("associated_data").getBytes(StandardCharsets.UTF_8),
resource.getString("nonce").getBytes(StandardCharsets.UTF_8),
resource.getString("ciphertext")
);
没错,标明使用utf-8字符集进行转换,问题就解决了。
感谢博主,所有的 bytes 都要用UTF-8
感谢感谢,我也是这个错误,按照博主的方式已经解决了:
原代码:![](http://mmbiz.qpic.cn/mmbiz_png/VIcDPYu8LWbUM1Y91oJKiaeF3UM0cHUK9WDTv52md51Oq9HdUXcbxpk7aqomQgyrPLqptESvRHDYaPibV2xIA3rQ/0?wx_fmt=png)
修改之后代码,以及正确的解析数据: