fzlee / alipay

Python Alipay(支付宝) SDK with SHA1/SHA256 support
Other
1.29k stars 378 forks source link

OpenSSL.crypto.Error: [('asn1 encoding routines', 'asn1_check_tlen', 'wrong tag'), ('asn1 encoding routines', 'asn1_d2i_ex_primitive', 'nested asn1 error'), ('asn1 encoding routines', 'asn1_template_noexp_d2i', 'nested asn1 error'), ('asn1 encoding routines', 'asn1_template_noexp_d2i', 'nested asn1 error'), ('PEM routines', 'PEM_ASN1_read_bio', 'ASN1 lib')] #133

Closed Firesuiry closed 3 years ago

Firesuiry commented 3 years ago

Describe the bug A clear and concise description of what the bug is.

Environment OS version:windows10 Python version:3.7 Alipay version:

To reproduce app_private_key_string = open(str(settings.BASE_DIR) + '/key/main/应用私钥2048.txt', 'r').read() app_public_key_cert_string = open(str(settings.BASE_DIR) + '/key/main/应用公钥2048.txt', 'r').read()

alipay_public_key_cert_string = open(str(settings.BASE_DIR) + '/key/main/alipayCertPublicKey_RSA2.crt', 'r', encoding='utf-8').read() alipay_root_cert_string = open(str(settings.BASE_DIR) + '/key/main/alipayRootCert.crt', 'r', encoding='utf-8').read()

alipay = DCAliPay( appid=settings.ALIPAY_APPID, app_notify_url="http://example.com/app_notify_url", app_private_key_string=app_private_key_string, app_public_key_cert_string=app_public_key_cert_string, alipay_public_key_cert_string=alipay_public_key_cert_string, alipay_root_cert_string=alipay_root_cert_string, debug=True, sign_type='RSA2', )

def pay(request):

电脑网站支付,需要跳转到https://openapi.alipaydev.com/gateway.do? + order_string

order_string = alipay.api_alipay_trade_page_pay(
    out_trade_no=f'xx{time.time()}',  # 生成随时间变动而变动的唯一订单号
    total_amount=str(0.01),  # 将Decimal类型转换为字符串交给支付宝
    subject="测试订单",
    body="Python入门视频",
    return_url="https://example.com",
    notify_url="https://example.com/notify"  # 可选, 不填则使用默认notify url
)

# 让用户进行支付的支付宝页面网址
url = settings.ALIPAY_URL + "?" + order_string

return JsonResponse({"code": 0, "message": "请求支付成功", "url": url})

报错信息: Traceback (most recent call last): File "D:\Users\46907\Anaconda3\envs\web\lib\site-packages\django\core\handlers\exception.py", line 34, in inner response = get_response(request) File "D:\Users\46907\Anaconda3\envs\web\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response response = self.process_exception_by_middleware(e, request) File "D:\Users\46907\Anaconda3\envs\web\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response response = wrapped_callback(request, *callback_args, *callback_kwargs) File "D:\develop\testDjnaog\app\views.py", line 184, in pay notify_url="https://example.com/notify" # 可选, 不填则使用默认notify url File "D:\Users\46907\Anaconda3\envs\web\lib\site-packages\alipay__init.py", line 263, in api_alipay_trade_page_pay notify_url=notify_url File "D:\Users\46907\Anaconda3\envs\web\lib\site-packages\alipay__init__.py", line 680, in build_body data["app_cert_sn"] = self.app_cert_sn File "D:\Users\46907\Anaconda3\envs\web\lib\site-packages\alipay\init.py", line 740, in app_cert_sn self._app_cert_sn = self.get_cert_sn(self._app_public_key_cert_string) File "D:\Users\46907\Anaconda3\envs\web\lib\site-packages\alipay\init__.py", line 700, in get_cert_sn cert = OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_PEM, cert) File "D:\Users\46907\Anaconda3\envs\web\lib\site-packages\OpenSSL\crypto.py", line 1794, in load_certificate _raise_current_error() File "D:\Users\46907\Anaconda3\envs\web\lib\site-packages\OpenSSL_util.py", line 54, in exception_from_error_queue raise exception_type(errors) OpenSSL.crypto.Error: [('asn1 encoding routines', 'asn1_check_tlen', 'wrong tag'), ('asn1 encoding routines', 'asn1_d2i_ex_primitive', 'nested asn1 error'), ('asn1 encoding routines', 'asn1_template_noexp_d2i', 'nested asn1 error'), ('asn1 encoding routines', 'asn1_template_noexp_d2i', 'nested asn1 error'), ('PEM routines', 'PEM_ASN1_read_bio', 'ASN1 lib')] Internal Server Error: /check_pay/ Traceback (most recent call last): File "D:\Users\46907\Anaconda3\envs\web\lib\site-packages\django\core\handlers\exception.py", line 34, in inner response = get_response(request) File "D:\Users\46907\Anaconda3\envs\web\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response response = self.process_exception_by_middleware(e, request) File "D:\Users\46907\Anaconda3\envs\web\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response response = wrapped_callback(request, callback_args, **callback_kwargs) File "D:\develop\testDjnaog\app\views.py", line 199, in check_pay response = alipay.api_alipay_trade_query(order_id) # response是一个字典 File "D:\Users\46907\Anaconda3\envs\web\lib\site-packages\alipay__init.py", line 303, in api_alipay_trade_query data = self.build_body("alipay.trade.query", biz_content) File "D:\Users\46907\Anaconda3\envs\web\lib\site-packages\alipay__init__.py", line 680, in build_body data["app_cert_sn"] = self.app_cert_sn File "D:\Users\46907\Anaconda3\envs\web\lib\site-packages\alipay\init.py", line 740, in app_cert_sn self._app_cert_sn = self.get_cert_sn(self._app_public_key_cert_string) File "D:\Users\46907\Anaconda3\envs\web\lib\site-packages\alipay\init__.py", line 700, in get_cert_sn cert = OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_PEM, cert) File "D:\Users\46907\Anaconda3\envs\web\lib\site-packages\OpenSSL\crypto.py", line 1794, in load_certificate _raise_current_error() File "D:\Users\46907\Anaconda3\envs\web\lib\site-packages\OpenSSL_util.py", line 54, in exception_from_error_queue raise exception_type(errors) OpenSSL.crypto.Error: [('asn1 encoding routines', 'asn1_check_tlen', 'wrong tag'), ('asn1 encoding routines', 'asn1_d2i_ex_primitive', 'nested asn1 error'), ('asn1 encoding routines', 'asn1_template_noexp_d2i', 'nested asn1 error'), ('asn1 encoding routines', 'asn1_template_noexp_d2i', 'nested asn1 error'), ('PEM routines', 'PEM_ASN1_read_bio', 'ASN1 lib')]

我看之前issue有些不同的地方: 我的alipayCertPublicKey_RSA2里面有有两段 而我看issue里面成功的那个里面只有一段

读取文件内容: 应用公钥2048.txt -----BEGIN CERTIFICATE----- XXXXXXXXXXXXX -----END CERTIFICATE----- 应用私钥2048.txt -----BEGIN RSA PRIVATE KEY----- XXXX -----END RSA PRIVATE KEY----- alipayCertPublicKey_RSA2.crt -----BEGIN CERTIFICATE----- XXXXXXXXXXXXX -----END CERTIFICATE----- -----BEGIN CERTIFICATE----- XXXXXXXXXXXXX -----END CERTIFICATE----- alipayRootCert.crt -----BEGIN CERTIFICATE----- XXXXXXXXXXXXX -----END CERTIFICATE-----

-----BEGIN CERTIFICATE----- XXXXXXXXXXXXX -----END CERTIFICATE-----

-----BEGIN CERTIFICATE----- XXXXXXXXXXXXX -----END CERTIFICATE-----

-----BEGIN CERTIFICATE----- XXXXXXXXXXXXX -----END CERTIFICATE-----

我现在怀疑是不是RSA和RSA2不一样导致的问题呢?

fzlee commented 3 years ago

1我的alipayCertPublicKey_RSA2里面有有两段` 问题应该出在这里, 因为是公钥, 可以贴上来看看吗

Firesuiry commented 3 years ago

-----BEGIN CERTIFICATE----- MIIDuDCCAqCgAwIBAgIQICEIFv4pYUUA9sCfRdSLejANBgkqhkiG9w0BAQsFADCBgjELMAkGA1UE BhMCQ04xFjAUBgNVBAoMDUFudCBGaW5hbmNpYWwxIDAeBgNVBAsMF0NlcnRpZmljYXRpb24gQXV0 aG9yaXR5MTkwNwYDVQQDDDBBbnQgRmluYW5jaWFsIENlcnRpZmljYXRpb24gQXV0aG9yaXR5IENs YXNzIDIgUjEwHhcNMjEwODE2MDE1ODI4WhcNMjMwODE2MDE1ODI4WjCBmDELMAkGA1UEBhMCQ04x MzAxBgNVBAoMKueEpuS9nOW4guWlvem6puWkmuenkeaKgOWVhui0uOaciemZkOWFrOWPuDEPMA0G A1UECwwGQWxpcGF5MUMwQQYDVQQDDDrmlK/ku5jlrp0o5Lit5Zu9Kee9kee7nOaKgOacr+aciemZ kOWFrOWPuC0yMDg4MjQxMTY2OTAxMDI0MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA uag+WfoISCIMrJgMuE7MSqmvIhsnG1X7Vcznuy/L9QgiZzKMf8FRivTAfhpq/aBXpVtSykR3y08v JJwAN8iNTsZscQxa/BV5bXUdV6CY96v0lpUqluvorVW3g5mYo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MyIH9+WKx9Ed6/WzeZmIy5ZWpX1TtTolo6 OJXQFeItMAjHxW/ZSZTok5IS3FuRhExturaInnzjYpx50a6kS34c5+c8hYq7sAtZ/CNLZmBnBCFD aMQqT8xFZJ5uolUaSeXxg7JFY1QsYp5RKvj4SjFwCGKJ2+hPPe9UyyltxOidNtxjaknOCeBHytOr -----END CERTIFICATE-----

Firesuiry commented 3 years ago

这是我下载的公钥

fzlee commented 3 years ago

你要用上面那一段, 这其实是一个证书链, 包含两个证书

将两份文本分别复制到a.txt 和b.txt

之后使用命令行 openssl x509 -in a或者b.txt -noout -text 可以看到证书的详情

Firesuiry commented 3 years ago

你要用上面那一段, 这其实是一个证书链, 包含两个证书

将两份文本分别复制到a.txt 和b.txt

之后使用命令行 openssl x509 -in a或者b.txt -noout -text 可以看到证书的详情

谢谢您 我再试试