/// <summary> /// 将一键支付清算接口返回的结果进行解析,支付结果回调解析一样可以调用该方法 /// </summary> /// <param name="ybResult">易宝支付返回的结果</param> /// <returns></returns> public static string checkYbClearResult(String ybResult) { if (ybResult.IndexOf("data") >= 0) { //将支付结果json字符串反序列化为对象 RespondJson respJson = Newtonsoft.Json.JsonConvert.DeserializeObject <RespondJson>(ybResult); string yb_encryptkey = respJson.encryptkey; string yb_data = respJson.data; //将易宝返回的结果进行验签名 bool passSign = EncryptUtil.checkDecryptAndSign(yb_data, yb_encryptkey, yibaoPublickey, merchantPrivatekey); if (passSign) { string yb_aeskey = RSAFromPkcs8.decryptData(yb_encryptkey, merchantPrivatekey, "UTF-8"); string payresult_view = AES.Decrypt(yb_data, yb_aeskey); return(payresult_view); } else { return("验签未通过"); } } else { return(ybResult); } }
/// <summary> /// 将请求接口中的业务明文参数加密并请求一键支付接口,单不对返回的数据进行解密,用于获取清算对账单接口--商户通用接口 /// </summary> /// <param name="sd"></param> /// <param name="apiUri"></param> /// <returns></returns> private string createMerchantDataAndRequestYb2(SortedDictionary <string, object> sd, string apiUri, bool ispost) { //随机生成商户AESkey string merchantAesKey = AES.GenerateAESKey(); //生成RSA签名 string sign = EncryptUtil.handleRSA(sd, merchantPrivatekey); sd.Add("sign", sign); //将对象转换为json字符串 string bpinfo_json = Newtonsoft.Json.JsonConvert.SerializeObject(sd); string datastring = AES.Encrypt(bpinfo_json, merchantAesKey); //将商户merchantAesKey用RSA算法加密 string encryptkey = RSAFromPkcs8.encryptData(merchantAesKey, yibaoPublickey, "UTF-8"); String ybResult = ""; if (ispost) { ybResult = YJPayUtil.payAPIRequest(apimercahntprefix + apiUri, datastring, encryptkey, true); } else { ybResult = YJPayUtil.payAPIRequest(apimercahntprefix + apiUri, datastring, encryptkey, false); } return(YJPayUtil.checkYbClearResult(ybResult)); }
/// <summary> /// 对一键支付返回的业务数据进行验签 /// </summary> /// <param name="data"></param> /// <param name="encrypt_key"></param> /// <param name="yibaoPublickKey"></param> /// <param name="merchantPrivateKey"></param> /// <returns></returns> public static bool checkDecryptAndSign(string data, string encrypt_key, string yibaoPublickKey, string merchantPrivateKey) { /** 1.使用YBprivatekey解开aesEncrypt。 */ string AESKey = ""; try { AESKey = RSAFromPkcs8.decryptData(encrypt_key, merchantPrivateKey, "UTF-8"); } catch (Exception e) { /** AES密钥解密失败 */ return(false); } /** 2.用aeskey解开data。取得data明文 */ string realData = AES.Decrypt(data, AESKey); SortedDictionary <string, object> sd = Newtonsoft.Json.JsonConvert.DeserializeObject <SortedDictionary <string, object> >(realData); /** 3.取得data明文sign。 */ string sign = (string)sd["sign"]; /** 4.对map中的值进行验证 */ StringBuilder signData = new StringBuilder(); foreach (var item in sd) { /** 把sign参数隔过去 */ if (item.Key == "sign") { continue; } signData.Append(item.Value); } signData = signData.Replace("\r", ""); signData = signData.Replace("\n", ""); signData = signData.Replace(" ", ""); signData = signData.Replace(" ", ""); signData = signData.Replace("\": \"", "\":\""); signData = signData.Replace("\": ", "\":"); /**5. result为true时表明验签通过 */ bool result = RSAFromPkcs8.checkSign(Convert.ToString(signData), sign, yibaoPublickKey, "UTF-8"); return(result); }
/// <summary> /// 将SortedDictionary中的键值拼接成一个大字符串,然后使用RSA生成签名 /// </summary> /// <param name="sd"></param> /// <param name="privatekey"></param> /// <returns></returns> public static string handleRSA(SortedDictionary <string, object> sd, string privatekey) { StringBuilder sbuffer = new StringBuilder(); foreach (KeyValuePair <string, object> item in sd) { sbuffer.Append(item.Value); } Console.WriteLine(sbuffer.ToString()); string sign = ""; sign = RSAFromPkcs8.sign(sbuffer.ToString(), privatekey, "UTF-8"); return(sign); }
/// <summary> /// 解密易宝支付回调结果 /// </summary> /// <param name="data">易宝支付回调的业务数据密文</param> /// <param name="encryptkey">易宝支付回调密钥密文</param> /// <returns></returns> public static string checkYbCallbackResult(string data, string encryptkey) { string yb_encryptkey = encryptkey; string yb_data = data; //将易宝返回的结果进行验签名 bool passSign = EncryptUtil.checkDecryptAndSign(yb_data, yb_encryptkey, yibaoPublickey, merchantPrivatekey); if (passSign) { string yb_aeskey = RSAFromPkcs8.decryptData(yb_encryptkey, merchantPrivatekey, "UTF-8"); string payresult_view = AES.Decrypt(yb_data, yb_aeskey); //返回易宝支付回调的业务数据明文 return(payresult_view); } else { return("验签未通过"); } }