public void Submit_OnClick(object sender, EventArgs e) { _configInfo.IsAlipayPc = Utils.ToBool(DdlIsEnabled.SelectedValue); if (_configInfo.IsAlipayPc && PhOpenApi.Visible) { try { AlipaySignature.RSASignCharSet("test", TbPrivateKey.Text, "utf-8", false, "RSA2"); } catch (Exception ex) { Utils.SwalError(Page, "应用私钥格式不正确!", ex.Message); return; } } _configInfo.AlipayPcIsMApi = Utils.ToBool(DdlIsMApi.SelectedValue); _configInfo.AlipayPcAppId = TbAppId.Text; _configInfo.AlipayPcPid = TbPid.Text; _configInfo.AlipayPcMd5 = TbMd5.Text; _configInfo.AlipayPcPublicKey = TbPublicKey.Text; _configInfo.AlipayPcPrivateKey = TbPrivateKey.Text; Plugin.SetConfigInfo(_siteId, _configInfo); Utils.Redirect(PageIntegrationPay.GetRedirectUrl(_siteId)); }
public void RSA2SignTest() { string checkSignText = "N4rkiV/2bEI2Ji2KaaTDeP8WAsF49+iHVWPV7HKnc/I35x2eySGGmbcAtX3YqUDwtPtTbMIlVDm9e9lQTNz0z6ylq/w1pLbyry5Nr97VJzkHTMkf4YYwFR1KopPvk+ICyZEQLP4I02SlTipZ4easanehVG5n5lyCG894T7v6WgLT0VeL+YeE/6h4CuWgB5V13GxZhT0Yv5e8gNHnTudrrdlaIvE47FrqD9FbxXBV7vBleYmU7bn8liZnIz9DJKxrPSV+tvIheJJE6yf6rXfWoOK0hLanGArOUOWfjJha8KOvDrbTHd9X2sBxlk34BZbJysZnSiSItk50ikTL+/98pg=="; string text = "app_auth_token=&app_id=2019121860002580&charset=utf-8&format=JSON&method=alipay.trade.precreate×tamp=2019-12-26 22:13:22&version=1.0&biz_content={ \"out_trade_no\": \"093074c52de44ad2bc96cd3d81af1080\", \"total_amount\": 1.0, \"subject\": \"Preceate Test093074c52de44ad2bc96cd3d81af1080\", \"seller_id\": null, \"discountable_amount\": null, \"undiscountable_amount\": null, \"buyer_logon_id\": null, \"goods_detail\": null, \"body\": null, \"product_code\": null, \"operator_id\": null, \"store_id\": null, \"disable_pay_channels\": null, \"enable_pay_channels\": null, \"terminal_id\": null, \"extend_params\": null, \"timeout_express\": null, \"royalty_info\": null, \"settle_info\": null, \"sub_merchant\": null, \"alipay_store_id\": null, \"merchant_order_no\": null, \"ext_user_info\": null, \"business_params\": null, \"qr_code_timeout_express\": null}&sign_type=RSA2&sign=WiuQItk6a/XrCI2LSt+fvY1VGwdDAHVH4poo9IMYOpWs24KeKyfq2wPEvdbBBd/F1Oto/wD8FYatdrYSNK82XvJ/nbUD2/oxrBsr8Q9pkUJHqvAhm3z7p2aZZQptpqzcUx+WFwPF6YsgbOPdpe7HKf7G9Jn89Haz5mruLfu+zOPTRPCHErXgRGMW7ctBInnI3+u1ZmJKRp4W3gd6YtU9DZXWQWR4ar+UJdhCDLppLM+6pvQyRYacPDUzEUR17EQklNvStpw9xYOyolrRatRLbMpGbB5wTXjolAE3fq1U28J4+vVm0edfs4UBxdF8zs90mEMAf8EJ3BbLL4mogBc7tQ=="; string pattern = "(?:^|&)[a-zA-Z_]+=(?=&|$)"; Regex rgx = new Regex(pattern); text = rgx.Replace(text, ""); var queryString = HttpUtility.ParseQueryString(text); queryString.Remove("sign"); queryString.Remove("biz_content"); var orderedKeys = queryString.Cast <string>().Where(k => k != null).OrderBy(k => k); text = string.Join("&", orderedKeys.Select(k => string.Format("{0}={1}", k, queryString[k]))); string signText = AlipaySignature.RSASignCharSet(text, @"D:\Programing\Dev Repos\ZeusPay\alipayPrivateKey.txt", "utf-8", "RSA2"); //Assert.AreEqual(signText, checkSignText); }
public static String GetSign(Dictionary <String, String> map) { string query_string = GetOrderParam(map); //string query_string = "app_id=2015052600090779&biz_content={\"timeout_express\":\"30m\",\"seller_id\":\"\",\"product_code\":\"QUICK_MSECURITY_PAY\",\"total_amount\":\"0.01\",\"subject\":\"1\",\"body\":\"test2\",\"out_trade_no\":\"IQJZSRC1YMQB5HU\"}&charset=utf-8&format=json&method=alipay.trade.app.pay¬ify_url=http://domain.merchant.com/payment_notify&sign_type=RSA2×tamp=2017-1-10 17:58:01&version=1.0"; // string s = RSAFromPkcs8.sign(query_string, map["pri_key"], "UTF-8"); // string query_string = AlipaySignature.GetSignContent(map); string sign = AlipaySignature.RSASignCharSet(query_string, map["pri_key"], "UTF-8", false, "RSA"); bool isd = AlipaySignature.RSACheckContent(query_string, sign, "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC3Sz3pHsjBUFPrnWhps2ksCfdrkxw7yBRasR1iqGyq95HUG0RzZVwwE4DhBCDZb/1/hAchRC8wdJtPttVrGSGIqwRFG7x/srvKtMRcBwQsK0z87rqJxeew3jHB7RDvq8xVKAHFfd9MCyohU9W/Ngz2PeiNsd+fVLU/nsFciCdqkwIDAQAB", "UTF-8", "RSA", false); return(sign); }
public static async Task <PrecreateResult> Request(this AlipayClient client, PrecreateArg arg) { string text = JsonConvert.SerializeObject(arg); var jObj = (JObject)JsonConvert.DeserializeObject(text); JToken bizContent; if (!jObj.TryGetValue("bizContent", out bizContent)) { return(null); } jObj.Remove("bizContent"); var query = String.Join("&", jObj.Children().Cast <JProperty>() .OrderBy(c => c.Name) .Select(jp => jp.Name + "=" + jp.Value.ToString())); List <JToken> removeTokens = new List <JToken>(); foreach (var item in bizContent.Children().Cast <JProperty>()) { if (string.IsNullOrWhiteSpace(item.Value.ToString())) { removeTokens.Add(item); } } foreach (var item in removeTokens) { item.Remove(); } query += "&biz_content=" + bizContent.ToString().Replace("\r\n", ""); string pattern = "(?:^|&)[a-zA-Z_]+=(?=&|$)"; if (client.EncryptMode == EncryptMode.RSA2) { query += "&sign_type=RSA2"; } else if (client.EncryptMode == EncryptMode.RSA) { query += "&sign_type=RSA"; } query += $"&app_cert_sn={client.AppCertSN}&alipay_root_cert_sn={client.AlipayRootCertSN}"; Regex rgx = new Regex(pattern); text = rgx.Replace(query, ""); var queryString = HttpUtility.ParseQueryString(text); queryString.Remove("sign"); //queryString.Remove("biz_content"); var orderedKeys = queryString.Cast <string>().Where(k => k != null).OrderBy(k => k); text = string.Join("&", orderedKeys.Select(k => { if (k != "timestamp") { return(string.Format("{0}={1}", k, queryString[k].Replace(" ", ""))); } else { return(string.Format("{0}={1}", k, queryString[k])); } } )); text = text.Replace(" \"", ""); if (client.EncryptMode == EncryptMode.RSA2) { string sign = AlipaySignature.RSASignCharSet(text, client.PrivateKey, "GBK", "RSA2"); //string sign = AlipaySignature.RSASignCharSet(HttpUtility.HtmlEncode(text), client.PrivateKey, "utf-8", "RSA2"); query = text + $"&sign={sign}"; } else if (client.EncryptMode == EncryptMode.RSA) { string sign = AlipaySignature.RSASignCharSet(HttpUtility.HtmlEncode(text), client.PrivateKey, "GBK", "RSA"); query = text + $"&sign={sign}"; } queryString = HttpUtility.ParseQueryString(query); query = string.Join("&", queryString.Cast <string>().Select(k => string.Format("{0}={1}", k, HttpUtility.UrlEncode(queryString[k])))); var parameters = new Dictionary <string, string>(); foreach (var k in queryString.Cast <string>()) { parameters.Add(k, queryString[k]); } var encodedContent = new FormUrlEncodedContent(parameters); //.Select(jp => jp.Name + "=" + HttpUtility.UrlEncode(jp.Value.ToString()))); var http = new HttpClient(); //var response = await http.GetAsync(client.Host + "?" + query); var response = await http.PostAsync(client.Host, encodedContent); if (response.StatusCode == HttpStatusCode.OK) { var content = await response.Content.ReadAsStringAsync(); return(JsonConvert.DeserializeObject <PrecreateResult>(content)); } else { var content = await response.Content.ReadAsStringAsync(); return(null); } }