获取微信RSA加密公钥
获取RSA加密公钥是为“企业转账到银行卡”这个接口做准备的,这里获取到的是PKCS#1公钥的,
还需要转成PKCS#8公钥的,因为现在的api接口需要的是PKCS#8公钥,否则会报错的。
需要啊准备的东西如下:
商户号、api秘钥、微信证书
需要引入依赖
<!-- alipay--> <dependency> <groupId>com.alipay.sdk</groupId> <artifactId>alipay-sdk-java</artifactId> <!--<version>3.7.26.ALL</version>--> </dependency>
下面直接上代码
/** * 获取RSA加密公钥 * * @return 证书 */ @Override public Map<String, String> getPublicKey() { try { Map<String, String> params = new HashMap<String, String>(4); params.put("mch_id", WxPayConfig.MCH_ID); params.put("nonce_str", String.valueOf(System.currentTimeMillis())); params.put("sign_type", "MD5"); String createSign = WxPayKit.createSign(params, WxPayConfig.API_SECRET, SignType.MD5); params.put("sign", createSign); //获取证书路径 File file = ResourceUtils.getFile(WxPayConfig.CERT_PATH); //FileInputStream certStream = new FileInputStream(file); String publicKey = WeChatPayApi.getPublicKey(params, file.toString(), WxPayConfig.MCH_ID); Map<String, String> map = WXPayUtil.xmlToMap(publicKey); System.out.println(map.get("pub_key")); return map; } catch (Exception e) { e.printStackTrace(); throw new CustomException("系统繁忙,请稍后重试"); } }
/** * 证书地址: resource下 */ public static final String CERT_PATH = "classpath:cert/apiclient_cert.p12";
好了,有什么问题下方留言.......