基于Pyhton下的支付宝RSA、RSA2签名



  • def buildAlipayUrl(postData, aliPayIsvPriKey):
        dict = json.loads(postData)
        signature=None
        sortData = ''
        for k,v in dict.items():
            sortData = sortData + k.strip() + '=' + str(v).strip() + '&'
        paramUrl = str(sortData[:-1]).replace('+','')
        pkey_template = '-----BEGIN PRIVATE KEY-----\n' + aliPayIsvPriKey + '\n-----END PRIVATE KEY-----'
        key = RSA.importKey(pkey_template)
        signer = PKCS1_v1_5.new(key)
        if dict['sign_type']=='RSA':
            signature = signer.sign(SHA.new(paramUrl.encode('utf-8')))
        else:
            signature = signer.sign(SHA256.new(paramUrl.encode('utf-8')))
        # base64 编码,转换为unicode表示并移除回车
        sign = base64.encodebytes(signature).decode('utf-8').replace('\n', '')
        # url = const.ALI_ADDRESS_PRODUCT['value'] + paramUrl + "&sign=" + quote(sign, safe='')
        url = 'https://openapi.alipay.com/gateway.do?' + paramUrl + "&sign=" + quote(sign, safe='')
        print(url)
        return url
    



Log in to reply