java使用AES-256-ECB(PKCS7Padding)解密——微信支付退款通知接口指定解密方式

1.场景

在做微信支付退款通知接口时,微信对通知的内容做了加密,并且指定用 AES256 解密,官方指定的解密方式如下:

  java使用AES-256-ECB(PKCS7Padding)解密——微信支付退款通知接口指定解密方式

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.结果

java使用AES-256-ECB(PKCS7Padding)解密——微信支付退款通知接口指定解密方式

5.参考

解密方式:https://blog.csdn.net/qq_25958497/article/details/87937020

报错解决(报错内容“No such provider: BC”):https://www.cnblogs.com/wswang/p/7718150.html

相关推荐