xiaonao00 2019-12-06
1.场景
在做微信支付退款通知接口时,微信对通知的内容做了加密,并且指定用 AES256 解密,官方指定的解密方式如下:
2.导包
<!-- https://mvnrepository.com/artifact/org.bouncycastle/bcprov-jdk15on --> <dependency> <groupId>org.bouncycastle</groupId> <artifactId>bcprov-jdk15on</artifactId> <version>1.60</version> </dependency> <!-- https://mvnrepository.com/artifact/org.jasypt/jasypt --> <dependency> <groupId>org.jasypt</groupId> <artifactId>jasypt</artifactId> <version>1.9.3</version> </dependency>
3.解密
说明:方法中参数 reqInfo 就是退款结果通知接口中的 req_info 字段值
private String descrypt(String reqInfo) throws NoSuchPaddingException, NoSuchAlgorithmException, NoSuchProviderException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException { byte[] reqInfoB = Base64.decodeBase64(reqInfo); String key_ = DigestUtils.md5Hex(WXPayConfig.getInstance().getKey()).toLowerCase(); if (Security.getProvider("BC") == null){ Security.addProvider(new BouncyCastleProvider()); } Cipher cipher = Cipher.getInstance("AES/ECB/PKCS7Padding", "BC"); SecretKeySpec secretKeySpec = new SecretKeySpec(key_.getBytes(), "AES"); cipher.init(Cipher.DECRYPT_MODE, secretKeySpec); return new String(cipher.doFinal(reqInfoB)); }
4.结果
5.参考
解密方式:https://blog.csdn.net/qq_25958497/article/details/87937020
报错解决(报错内容“No such provider: BC”):https://www.cnblogs.com/wswang/p/7718150.html
网上的很多PHP微信支付接入教程都颇为复杂,且需要配置和引入较多的文件,本人通过整理后给出一个单文件版的,希望可以给各位想接入微信支付的带来些许帮助和借鉴意义。直接运行该文件即可给指定的微信用户退款。