在商户进件加密敏感信息的时候直接使用的(apiclient_cert.pem)商户证书进行加密。但是发送到微信服务器之后,返回说解密失败。后来查看相关的文章。好像是我的加密证书用错了,文章中说应该使用平台证书,需要调用https://api.mch.weixin.qq.com/v3/certificates接口进行获取。
但是在构建请求并发送后获取的返回值
"code": "RESOURCE_NOT_EXISTS",
"message": "无可用的平台证书,请在商户平台-API安全申请使用微信支付公钥
现在不知道是什么请款,是应该使用什么证书进行加密。
//敏感信息加密
public static String rsaEncryptOAEP(String message) throws Exception,IOException {
ResourceLoader resourceLoader = new DefaultResourceLoader();
//文件路径拼接
Resource resource = resourceLoader.getResource("file:" + getPublicKeyPath());
//加载公钥证书
try (InputStream inputStream = resource.getInputStream()) {
CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
Certificate certificate = certificateFactory.generateCertificate(inputStream);
// 从证书中提取公钥
PublicKey publicKey = certificate.getPublicKey();
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] data = message.getBytes(StandardCharsets.UTF_8);
byte[] cipherdata = cipher.doFinal(data);
return Base64.getEncoder().encodeToString(cipherdata);
}catch (Exception e) {
System.out.println(e.toString());
return null;
}}
加密规则文档:https://pay.weixin.qq.com/doc/v3/partner/4013059044
相关提问链接:https://developers.weixin.qq.com/community/develop/doc/0004a08ab68a582fe720f2c9b61000?highLine=%25E8%25AF%25B7%25E6%25B1%2582%25E8%25BF%2594%25E5%259B%259E%25E5%258F%2582%25E6%2595%25B0%253A%2520%2520%257B%2522code%2522%253A%2522PARAM_ERROR%2522%252C%2522message%2522%253A%2522%25E5%25B9%25B3%25E5%258F%25B0%25E7%25A7%2581%25E9%2592%25A5%25E8%25A7%25A3%25E5%25AF%2586%25E5%25A4%25B1%25E8%25B4%25A5%2522%257D
商户后台->账户中心->api安全->平台公钥,不需要去调用API获取平台证书接口