小程序
小游戏
企业微信
微信支付
扫描小程序码分享
1.解密encrypt_key得到key
2.用key和随机字符串nonce和根据download_url得到的字节数组ciphertext解密
第二步解密报错了,提示javax.crypto.AEADBadTagException: Tag mismatch!
证书秘钥和v3秘钥肯定没问题的(别的接口如查询,提现等接口都正常),下载账单的全部代码如下,麻烦大佬看下哪里出问题了,谢谢!
3 个回答
加粗
标红
插入代码
插入链接
插入图片
上传视频
全屏看图可以看清代码
你好,麻烦通过点击下方“反馈信息”按钮,提供出现问题的。
public static byte[] downloadUrl(String download_url) throws Exception { //请求URL //账单文件的下载地址的有效时间为30s URIBuilder uriBuilder = new URIBuilder(download_url); HttpGet httpGet = new HttpGet(uriBuilder.build()); httpGet.addHeader("Accept", "application/json"); //执行请求 CloseableHttpResponse response = httpClient.execute(httpGet); try { int statusCode = response.getStatusLine().getStatusCode(); if (statusCode == 200) { byte[] bytes = EntityUtils.toByteArray(response.getEntity()); return bytes; } else if (statusCode == 204) { System.out.println("success"); } else { System.out.println("failed,resp code = " + statusCode + ",return body = " + EntityUtils.toString(response.getEntity())); throw new IOException("request failed"); } } finally { response.close(); } return null; } /** * 解密二级商户账单 * @param associatedData * @param nonce * @param ciphertext 二进制密文,需以字节数组形式处理,不需要进行Base64解码。 * @return * @throws GeneralSecurityException * @throws IOException */ public String decryptToString(byte[] associatedData, byte[] nonce, byte[] ciphertext) throws GeneralSecurityException, IOException { try { Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding"); SecretKeySpec key = new SecretKeySpec(aesKey, "AES"); GCMParameterSpec spec = new GCMParameterSpec(TAG_LENGTH_BIT, nonce); cipher.init(Cipher.DECRYPT_MODE, key, spec); cipher.updateAAD(associatedData); return new String(cipher.doFinal(ciphertext), "utf-8"); } catch (NoSuchAlgorithmException | NoSuchPaddingException e) { throw new IllegalStateException(e); } catch (InvalidKeyException | InvalidAlgorithmParameterException e) { throw new IllegalArgumentException(e); } } }
我这里完全按照你说的做的,还是这个错,可以帮我看看分析一下么,谢谢你了,必有答谢
关注后,可在微信内接收相应的重要提醒。
请使用微信扫描二维码关注 “微信开放社区” 公众号
全屏看图可以看清代码
1.这里的账单解密的SecretKeySpec里的key是encrypt_key解密后的key,
2.附加数据associatedData,这里是空字符串
我就是这两个搞错了
public static byte[] downloadUrl(String download_url) throws Exception { //请求URL //账单文件的下载地址的有效时间为30s URIBuilder uriBuilder = new URIBuilder(download_url); HttpGet httpGet = new HttpGet(uriBuilder.build()); httpGet.addHeader("Accept", "application/json"); //执行请求 CloseableHttpResponse response = httpClient.execute(httpGet); try { int statusCode = response.getStatusLine().getStatusCode(); if (statusCode == 200) { byte[] bytes = EntityUtils.toByteArray(response.getEntity()); return bytes; } else if (statusCode == 204) { System.out.println("success"); } else { System.out.println("failed,resp code = " + statusCode + ",return body = " + EntityUtils.toString(response.getEntity())); throw new IOException("request failed"); } } finally { response.close(); } return null; } /** * 解密二级商户账单 * @param associatedData * @param nonce * @param ciphertext 二进制密文,需以字节数组形式处理,不需要进行Base64解码。 * @return * @throws GeneralSecurityException * @throws IOException */ public String decryptToString(byte[] associatedData, byte[] nonce, byte[] ciphertext) throws GeneralSecurityException, IOException { try { Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding"); SecretKeySpec key = new SecretKeySpec(aesKey, "AES"); GCMParameterSpec spec = new GCMParameterSpec(TAG_LENGTH_BIT, nonce); cipher.init(Cipher.DECRYPT_MODE, key, spec); cipher.updateAAD(associatedData); return new String(cipher.doFinal(ciphertext), "utf-8"); } catch (NoSuchAlgorithmException | NoSuchPaddingException e) { throw new IllegalStateException(e); } catch (InvalidKeyException | InvalidAlgorithmParameterException e) { throw new IllegalArgumentException(e); } } }
我这里完全按照你说的做的,还是这个错,可以帮我看看分析一下么,谢谢你了,必有答谢