使用支付宝公钥 初始化AliPay时报错 binascii.Error: Incorrect padding
q573794399 opened this issue · 8 comments
py版本3.9
支付宝沙箱环境
from alipay import AliPay
#应用密钥
app_private_key_string = '''-----BEGIN RSA2 PRIVATE KEY-----
MIIEowIBAAKCAQEAyFEhgE1SDlDhVhVysVNcQxPvrjtpEWCPrVOP279yyv+KLp+rXvkFmp24ea68kUb9j8r7a+B4howZJ/1eDxD/rzoqWPR5l5KdlHxCei0EcnRhgO6fUgkr/iCGT9CzjeUtl9U7oRZzWkIrxEBvcznrJ6PzDA+i7pkco+qYrwkdtDF22/yfhUKgdAWJdZFnxGVE6rCfNuwYwH5szOnKiPIw5Ndt4DOK9//wcuq9mqAVqRpjHrO/jB3U6RRB/SFwlHg8/6FARGDhtIxIWArjfnDUdS1wmjVxyvVzof7wKfCv69ktNxDM9Y622ayBbXZTWHs1xvh7PZUcegCH/ygStzgokQIDAQABAoIBAAiZ4zRdHY7bzjEiDqCqkaWXr4ztpB9I5CJvGhg3ajhpLIe/913Gl4UCDK+83v/l6nFIJ++D3ej5DwN2j5laA4CEoNjc9nOZqgZ8J2aTlENN1/nvRQwCtM7emqXGsk7/biu8ziimjCFuAe8VTTPHYt963K7B5R8Vl/M52YB6hbn86kUeRwzeDbCCn0WPoQFGSU5z7bkPsRoO2Vdtp3DgCHEkEBe2dOFl7Y30Iw87K/lgAWPxH0O4y6+DzeCD0hkhIXHrv3tkEVcZsbjyfwLk2wfppPZ6EEp9y+aJCmSA745a3hoOdyuhCu31wPC4tkpF5p2IIU3Sl6LQ16xybrwfDbkCgYEA68d7nLwj3iBAoz794WNWcQqQlUhqA0FQYqK81svJh+HscHZZ3JOjR4ur/rmFXE0APZQrQMHp1S35ioAV3bKaVziSt6gO0pgiNYHp1hhYAbwjIwuysmL2VEeIo4jHVpAX6MB11j7gvRmy2UBDJDfPKki3JQKXUE3+06jU0qj8A58CgYEA2X8TSADgqhOx/x+Rp2lPXblwoCFTIG+4Aq3/NEg26gqyHC7Zcf+r5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086S9wxlupAHq1tBwaPOeistQIDAQAB
-----END PUBLIC KEY-----
'''
#应用公钥
app_public_key='''-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAyFEhgE1SDlDhVhVysVNcQxPvrjtpEWCPrVOP279yyv+KLp+rXvkFmp24ea68kUb9j8r7a+B4howZJ/1eDxD/rzoqWPR5l5KdlHxCei0EcnRhgO6fUgkr/iCGT9CzjeUtl9U7oRZzWkIrxEBvcznrJ6PzDA+i7pkco+qYrwkdtDF22/yfhUKgdAWJdZFnxGVE6rCfNuwYwH5szOnKiPIw5Ndt4DOK9//wcuq9mqAVqRpjHrO/jB3U6RRB/SFwlHg8/6FARGDhtIxIWArjfnDUdS1wmjVxyvVzof7wKfCv69ktNxDM9Y622ayBbXZTWHs1xvh7PZUcegCH/ygStzgokQIDAQAB
-----END PUBLIC KEY-----
'''
alipay_pay = AliPay(
appid="",
app_notify_url=None, # 默认回调url
app_private_key_string=app_private_key_string,
alipay_public_key_string=alipay_public_key_string,
sign_type="RSA2", # RSA 或者 RSA2
verbose=True, # 输出调试数据
debug=True, # 默认False
)
如果在alipay_public_key_string=''这里填入的是支付宝公钥 那么就会报错
Traceback (most recent call last):
File "C:\Users\57379\PycharmProjects\pythonProject\SynologyDrive\django\支付宝支付\test.py", line 17, in
alipay_pay = AliPay(
File "C:\Users\57379\AppData\Local\Programs\Python\Python39\lib\site-packages\alipay_init_.py", line 93, in init
self.load_key()
File "C:\Users\57379\AppData\Local\Programs\Python\Python39\lib\site-packages\alipay_init.py", line 102, in _load_key
self._alipay_public_key = RSA.importKey(content)
File "C:\Users\57379\PycharmProjects\pythonProject\venv\lib\site-packages\Cryptodome\PublicKey\RSA.py", line 760, in import_key
(der, marker, enc_flag) = PEM.decode(tostr(extern_key), passphrase)
File "C:\Users\57379\PycharmProjects\pythonProject\venv\lib\site-packages\Cryptodome\IO\PEM.py", line 179, in decode
data = a2b_base64(''.join(lines[1:-1]))
binascii.Error: Incorrect padding
但是如果使用自己生成的 应用公钥 就能成功初始化
py版本使用过 3.9 和3.6都会报错
应该密钥生成使用过 支付宝开发平台开发助手和openssl 结果都是一样的
也看了文档里面提供的证书的格式 但是那里好像只有应用公钥 没有支付宝公钥 所以无法进行对比
我跟你是从同一个地方复制下来的, 但是内容不同, 估计每个账号的公钥都不一样。 你可以复制粘贴到这边吗。 我现在只能怀疑是你复制粘贴错了
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEArWj+ODDSzG8z2mdhW2YirjvlQwVdm/JVnNvYKPtNZUAjx1kA3XytTbefo+zJ8MJxZpVoTxY9sQ4JGODi/OYUzfA4ctQVXw+9DEuq8Yz2U0HTLFaMRSwrKz8kyrJQFsOXSEEdylAmeFtoBxRtuxX9klih090BpeA9RDemO00oqxcdnzq0uqCKjAQwqdVcz1qXFlH5TNJKU2hfHGFYLoD1Bvxjix3AVxH4SEmaYuHHZmIi7Va5bVuQXJpCnO0BIrQKgC0gUWtlzeFNu9EGlQ5Be/kUi4XqJbeXhSN3zHxjn8+me3rVS7/aXv+J086S9wxlupAHq1tBwaPOeistQIDAQAB
base64的意义在于将3个二进制转化成4个字节的可读自字符。可是你给我的长度是387, 我不知道问题出在哪里, 这个公钥绝对是有问题的
好的 我去问一下支付宝那边的 谢谢大佬解答
问题解决了 是支付宝的问题 让支付宝那边重置一下沙箱账号 然后生成的支付宝公钥就是正常的392位了
这也是活久见了