微信支付海关申报国际版 - 身份信息验证API https://pay.weixin.qq.com/wiki/doc/api/wxpay/en/declarecustom/chapter3_2.shtml
上面👆API有老哥使用过吗?
https://pay.weixin.qq.com/wiki/doc/apiv3_partner/wechatpay/wechatpay6_0.shtml 使用微信支付发布的工具加密后的信息还是提示平台私钥解密失败
敏感信息加密后在V3 API里可以正常调用, 但就这个接口一直调用不通, 有老哥碰到过嘛
这个接口,要求的敏感信息加密,padding模式跟国内的不一样,需要采用 `OPENSSL_PKCS1_PADDING`,而国内是`OPENSSL_PKCS1_OAEP_PADDING`
公钥文件也要注意不是是从微信支付后台下载的商户证书.
通过接口下载平台证书, 需要解密: https://api.mch.weixin.qq.com/v3/certificates
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.asymmetric.padding import PKCS1v15
from cryptography.x509 import load_pem_x509_certificate
from base64 import b64encode
def load_certificate(certificate_str):
try:
return load_pem_x509_certificate(
data=certificate_str.encode('UTF-8'),
backend=default_backend())
except:
return None
def rsa_encrypt(text, certificate):
data = text.encode('UTF-8')
public_key = certificate.public_key()
cipherbyte = public_key.encrypt(
plaintext=data,
padding=PKCS1v15()
)
return b64encode(cipherbyte).decode('UTF-8')
if __name__ == '__main__':
cert_str = """-----BEGIN CERTIFICATE-----
test...
test...
-----END CERTIFICATE-----"""
result = rsa_encrypt("test", load_certificate(cert_str))
# result = get_signature("test", cert_str)
with open("./test.txt", "w") as f:
f.write(result)