小程序
小游戏
企业微信
微信支付
扫描小程序码分享
key已经从encrypt_key解密出,nonce也有,只有ciphertext 是乱码,导致解出明文报错,微信的接口调用返回状态都是200,不知道错在哪
2 个回答
加粗
标红
插入代码
插入链接
插入图片
上传视频
//以下方法注意 byte[] bytes = EntityUtils.toByteArray(response.getEntity()); 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[]数组 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); } } }
你好,麻烦通过点击下方“反馈信息”按钮,提供出现问题的。
官方给出下载API账单的详细说明,有空可以看下https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_1_8.shtml
关注后,可在微信内接收相应的重要提醒。
请使用微信扫描二维码关注 “微信开放社区” 公众号
//以下方法注意 byte[] bytes = EntityUtils.toByteArray(response.getEntity()); 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[]数组 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); } } }
官方给出下载API账单的详细说明,有空可以看下https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_1_8.shtml