/// <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> /// 字符串RSA公钥加密 /// </summary> /// <param name="data"></param> /// <param name="privatekey"></param> /// <param name="type"></param> /// <returns></returns> public static string handleRSA(string data, string publicKey, string type) { string sign = ""; //生成签名 sign = RSAFromPkcs8.encryptData(data.ToString(), publicKey, type); return(sign); }
/// <summary> /// 易宝支付 /// </summary> /// <returns></returns> public string YBPaySendRequesWriter(YBRequestPara para, bool isMobile) { SortedDictionary <string, object> sd = new SortedDictionary <string, object>(); sd.Add("merchantaccount", Config.merchantAccount); sd.Add("amount", para.amount); sd.Add("currency", 156); sd.Add("identityid", "user_" + para.userId.DESDecrypt()); sd.Add("identitytype", 2); sd.Add("orderid", para.orderid); sd.Add("orderexpdate", 60); sd.Add("productcatalog", "48"); sd.Add("productdesc", "速库美味商品"); sd.Add("productname", "速库美味商品"); //DateTime t1 = DateTime.Now; //DateTime t2 = new DateTime(1970, 1, 1); //double t = t1.Subtract(t2).TotalSeconds; //int transtime = (int)t; sd.Add("transtime", para.orderTime.DateTimeConvertTimeStamp().ParseInt()); sd.Add("userip", "255.255.255.255"); sd.Add("terminaltype", 3); sd.Add("terminalid", "nothing"); sd.Add("callbackurl", APIURLConfig.callbackUrl); sd.Add("fcallbackurl", isMobile ? APIURLConfig.fcallbackUrl_Mobile : APIURLConfig.fcallbackUrl); sd.Add("userua", ""); sd.Add("paytypes", "1|2"); //生成RSA签名 string sign = EncryptUtil.handleRSA(sd, Config.merchantPrivatekey); sd.Add("sign", sign); //将网页支付对象转换为json字符串 string merchantAesKey = APIURLConfig.merchantAesKey; string wpinfo_json = Newtonsoft.Json.JsonConvert.SerializeObject(sd); string datastring = AES.Encrypt(wpinfo_json, merchantAesKey); string encryptkey = RSAFromPkcs8.encryptData(merchantAesKey, Config.yibaoPublickey, "UTF-8"); string postParams = "data=" + HttpUtility.UrlEncode(datastring) + "&encryptkey=" + HttpUtility.UrlEncode(encryptkey) + "&merchantaccount=" + Config.merchantAccount; string url = (!isMobile ? APIURLConfig.payWebPrefix : APIURLConfig.payMobilePrefix) + (!isMobile ? APIURLConfig.pcwebURI : APIURLConfig.webpayURI) + "?" + postParams; return(url); //return YBPAY.CreatePayByPostHTML(HttpUtility.UrlEncode(datastring), // HttpUtility.UrlEncode(encryptkey), // Config.merchantAccount,isMobile); }
public static void testRSA() { /**RSA加密测试,RSA中的密钥对通过SSL工具生成,生成命令如下: * 1 生成RSA私钥: * openssl genrsa -out rsa_private_key.pem 1024 * 2 生成RSA公钥 * openssl rsa -in rsa_private_key.pem -pubout -out rsa_public_key.pem * * 3 将RSA私钥转换成PKCS8格式 * openssl pkcs8 -topk8 -inform PEM -in rsa_private_key.pem -outform PEM -nocrypt -out rsa_pub_pk8.pem * * 直接打开rsa_public_key.pem(公钥)和rsa_pub_pk8.pem(私钥)文件就可以获取密钥对内容,获取密钥对内容组成字符串时,注意将换行符删除 * */ //string publickey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDzOqfNunFxFtCZPlq7fO/jWwjqmTvAooVBB4y87BizSZ9dl/F7FpAxYc6MmX2TqivCvvORXgdlYdFWAhzXOnIUv9OGG///WPLe9TMs9kIwAZ/APUXauvC01oFLnYkzwPlAh0tQ1Au9arTE/OG1V1dKgf8BXHLPhKL4BmGBEUZBtQIDAQAB"; //string privatekey = "MIICeQIBADANBgkqhkiG9w0BAQEFAASCAmMwggJfAgEAAoGBAPM6p826cXEW0Jk+Wrt87+NbCOqZO8CihUEHjLzsGLNJn12X8XsWkDFhzoyZfZOqK8K+85FeB2Vh0VYCHNc6chS/04Yb//9Y8t71Myz2QjABn8A9Rdq68LTWgUudiTPA+UCHS1DUC71qtMT84bVXV0qB/wFccs+EovgGYYERRkG1AgMBAAECgYEA2PmnPdgnYKnolfvQ9tXiLaBFGPpvGk4grz0r6FB5TF7N4rErwxECunq0xioaowK4HPc40qHd2SvkkWQ7FCjYIDsnMk1oOhxNKn0J3FG0n5Cg1/dFai4eoXHs/nKn3SVZ8YZC1T2cMtN2srectLqNqhB8aQEe8xmykyUlUpg/qmECQQD9vkwjUotG5oUUrOj6etcB4WcdyyH0FtThKgyoJUDwgBv6lGGzWyFJEREvp47IgV+FgC7zeP2mL4MhgnD3tNCZAkEA9WRrjOLBNc379XZpoDsH7rZjobVvhnTrEuRDx/whqZ+vk64EPrEW81XYh647bAbJlFn2jPhY+IUHkrxFEFT/fQJBAMoLNOULXQtfkqgb5odMONeue0Ul8itB4tBHgzyALW1TFPQ6InGGJsLfbCfd67uMCFts7fXAaXhibK/KBdm3iEECQQChwVAjzlUN4nnzk9qMhFz2PcPvFGovd2J9UXpcmRaXeWuDLXIe4Rz/ydaxmWgSDWdTIvoicpIzP31+fBwKZ/0BAkEAy0bh4weKmYF29//rK0sxmY8RtqkQeFrwWbqx1daa1w0DfWlNSvy47zyW1G5/AdZU6JSpXxlxdlM/HSDw+v7kcA=="; string publickey = Config.merchantPublickey; string privatekey = Config.merchantPrivatekey; //加密字符串 string data = "1234567890123456"; Console.WriteLine("加密前字符串内容:" + data); //加密 string encrypteddata = RSAFromPkcs8.encryptData(data, publickey, "UTF-8"); Console.WriteLine("加密后的字符串为:" + encrypteddata); Console.WriteLine("解密后的字符串内容:" + RSAFromPkcs8.decryptData(encrypteddata, privatekey, "UTF-8")); //签名 string signdata = "YB010000001441234567286038508081299"; Console.WriteLine("签名前的字符串内容:" + signdata); string sign = RSAFromPkcs8.sign(signdata, privatekey, "UTF-8"); Console.WriteLine("签名后的字符串:" + sign); Console.WriteLine("RSA算法测试结束"); Console.ReadLine(); }
public static void testPay() { //一键支付URL前缀 string apiprefix = APIURLConfig.payMobilePrefix; //网页支付地址 string creditpayURI = APIURLConfig.webpayURI; //商户账户编号 string merchantAccount = Config.merchantAccount; //商户公钥(该商户公钥需要在易宝一键支付商户后台报备) string merchantPublickey = Config.merchantPublickey; //商户私钥(商户公钥对应的私钥) string merchantPrivatekey = Config.merchantPrivatekey; //易宝支付分配的公钥(进入商户后台公钥管理,报备商户的公钥后分派的字符串) string yibaoPublickey = Config.yibaoPublickey; //随机生成商户AESkey string merchantAesKey = AES.GenerateAESKey(); Random ra = new Random(); int amount = 2; //支付金额为分 int currency = 156; string identityid = "1234567" + 50 * ra.Next(); //用户身份标识 int identitytype = 0; string orderid = "1234567" + 50 * ra.Next(); int orderexpdate = 60; string productcatalog = "1"; //商品类别码,商户支持的商品类别码由易宝支付运营人员根据商务协议配置 string productdesc = "我叫MT"; string productname = "符石"; DateTime t1 = DateTime.Now; DateTime t2 = new DateTime(1970, 1, 1); double t = t1.Subtract(t2).TotalSeconds; int transtime = (int)t; string userip = "172.18.66.218"; int terminaltype = 3; String terminalid = "05-16-DC-59-C2-34"; //商户提供的商户后台系统异步支付回调地址 string callbackurl = "http://172.18.66.107:8082/payapi-java-demo/callback"; //商户提供的商户前台系统异步支付回调地址 string fcallbackurl = "http://172.18.66.107:8082/payapi-java-demo/fcallback"; //用户浏览器ua string userua = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1"; SortedDictionary <string, object> sd = new SortedDictionary <string, object>(); sd.Add("merchantaccount", merchantAccount); sd.Add("amount", amount); sd.Add("currency", currency); sd.Add("identityid", identityid); sd.Add("identitytype", identitytype); sd.Add("orderid", orderid); sd.Add("orderexpdate", orderexpdate); sd.Add("productcatalog", productcatalog); sd.Add("productdesc", productdesc); sd.Add("productname", productname); sd.Add("transtime", transtime); sd.Add("userip", userip); sd.Add("terminaltype", terminaltype); sd.Add("terminalid", terminalid); sd.Add("callbackurl", callbackurl); sd.Add("fcallbackurl", fcallbackurl); sd.Add("userua", userua); //生成RSA签名 string sign = EncryptUtil.handleRSA(sd, merchantPrivatekey); Console.WriteLine("生成的签名为:" + sign); sd.Add("sign", sign); //将网页支付对象转换为json字符串 string wpinfo_json = Newtonsoft.Json.JsonConvert.SerializeObject(sd); Console.WriteLine("网页支付明文数据json格式为:" + wpinfo_json); string datastring = AES.Encrypt(wpinfo_json, merchantAesKey); Console.WriteLine("网页支付业务数据经过AES加密后的值为:" + datastring); //将商户merchantAesKey用RSA算法加密 Console.WriteLine("merchantAesKey为:" + merchantAesKey); string encryptkey = RSAFromPkcs8.encryptData(merchantAesKey, yibaoPublickey, "UTF-8"); Console.WriteLine("encryptkey为:" + encryptkey); //打开浏览器访问一键支付网页支付链接地址,请求方式为get string postParams = "data=" + HttpUtility.UrlEncode(datastring) + "&encryptkey=" + HttpUtility.UrlEncode(encryptkey) + "&merchantaccount=" + merchantAccount; string url = apiprefix + creditpayURI + "?" + postParams; Console.WriteLine("网页支付链接地址为:" + url); Console.WriteLine("网页支付链接地址长度为:" + url.Length); //System.Diagnostics.Process.Start("iexplore.exe", url);//打开IE浏览器 System.Diagnostics.Process.Start("firefox.exe", url); //打开firefox浏览器 System.Diagnostics.Process.Start(url); Console.ReadLine(); }
protected void Page_Load(object sender, EventArgs e) { // string apiprefix = APIURLConfig.apiprefix; //网页支付地址 // string creditpayURI = APIURLConfig.webpayURI; //一键支付URL前缀 string apiprefix = APIURLConfig.payWebPrefix; //网页支付地址 string pcPayURI = APIURLConfig.pcwebURI; //商户账户编号 string merchantAccount = Config.merchantAccount; //商户公钥(该商户公钥需要在易宝一键支付商户后台报备) string merchantPublickey = Config.merchantPublickey; //商户私钥(商户公钥对应的私钥) string merchantPrivatekey = Config.merchantPrivatekey; //易宝支付分配的公钥(进入商户后台公钥管理,报备商户的公钥后分派的字符串) string yibaoPublickey = Config.yibaoPublickey; //随机生成商户AESkey string merchantAesKey = payapi_mobile_demo.AES.GenerateAESKey(); int amount = Convert.ToInt32(Request["p3_Amt1"].ToString()) * 100; //支付金额为分 int currency = 156; string identityid = DateTime.Now.ToString("yyyyMMddhhmmss"); //用户身份标识 int identitytype = 0; Random ra = new Random(); string orderid = "kjzf789123456" + 50 * ra.Next(); string other = "00-23-5A-15-99-42"; //mac地址 string productcatalog = "1"; //商品类别码,商户支持的商品类别码由易宝支付运营人员根据商务协议配置 string productdesc = "789游戏中心"; string productname = "金币"; DateTime t1 = DateTime.Now; DateTime t2 = new DateTime(1970, 1, 1); double t = t1.Subtract(t2).TotalSeconds; int transtime = (int)t; string userip = GameRequest.GetUserIP(); //商户提供的商户后台系统异步支付回调地址 string callbackurl = ConfigurationManager.AppSettings["yibao_Notify"].ToString(); //商户提供的商户前台系统异步支付回调地址 string fcallbackurl = ConfigurationManager.AppSettings["yibao_CallBack"].ToString(); //用户浏览器ua string userua = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1"; SortedDictionary <string, object> sd = new SortedDictionary <string, object>(); sd.Add("merchantaccount", merchantAccount); sd.Add("amount", amount); sd.Add("currency", currency); sd.Add("identityid", identityid); sd.Add("identitytype", identitytype); sd.Add("orderid", orderid); sd.Add("other", other); sd.Add("productcatalog", productcatalog); sd.Add("productdesc", productdesc); sd.Add("productname", productname); sd.Add("transtime", transtime); sd.Add("userip", userip); sd.Add("callbackurl", callbackurl); sd.Add("fcallbackurl", fcallbackurl); sd.Add("userua", userua); //生成RSA签名 string sign = EncryptUtil.handleRSA(sd, merchantPrivatekey); //Console.WriteLine("生成的签名为:" + sign); sd.Add("sign", sign); //将网页支付对象转换为json字符串 string wpinfo_json = Newtonsoft.Json.JsonConvert.SerializeObject(sd); // Console.WriteLine("网页支付明文数据json格式为:" + wpinfo_json); string datastring = payapi_mobile_demo.AES.Encrypt(wpinfo_json, merchantAesKey); //Console.WriteLine("网页支付业务数据经过AES加密后的值为:" + datastring); //将商户merchantAesKey用RSA算法加密 // Console.WriteLine("merchantAesKey为:" + merchantAesKey); string encryptkey = RSAFromPkcs8.encryptData(merchantAesKey, yibaoPublickey, "UTF-8"); // Console.WriteLine("encryptkey为:" + encryptkey); //打开浏览器访问一键支付网页支付链接地址,请求方式为get string postParams = "data=" + HttpUtility.UrlEncode(datastring) + "&encryptkey=" + HttpUtility.UrlEncode(encryptkey) + "&merchantaccount=" + merchantAccount; string url = apiprefix + pcPayURI + "?" + postParams; OnLineOrder online = new OnLineOrder(); int uid = accountsfacade.GetAccountsId(Request["yijianAccountNo"].ToString()); online.Accounts = Request["yijianAccountNo"].ToString(); online.UserID = uid; online.OrderAmount = decimal.Parse((Convert.ToInt32(amount) / 100).ToString()); online.OrderID = orderid; online.OrderStatus = 0; online.ShareID = 19; online.CardTotal = 1; online.CardTypeID = 1; online.TelPhone = ""; online.IPAddress = GameRequest.GetUserIP(); Message msg = treasurefacade.RequestOrder(online); if (!msg.Success) { Response.Redirect("/Tips.aspx?msg=" + msg.Content); } else { Response.Redirect(url); } // Console.WriteLine("网页支付链接地址为:" + url); // Console.WriteLine("网页支付链接地址长度为:" + url.Length); // System.Diagnostics.Process.Start("firefox.exe", url);//打开firefox浏览器 // System.Diagnostics.Process.Start(url); //Console.ReadLine(); }
protected void Page_Load(object sender, EventArgs e) { Request.Cookies.Remove("ErrorMsg"); fastYeepayUser = Request.Form["fastYeepayUser"] ?? string.Empty; fastYeepayMoney = Request.Form["fastYeepayMoney"] ?? string.Empty; payOtherMoney = Request.Form["selfMoney"] ?? string.Empty; if (fastYeepayMoney.Equals("0")) { fastYeepayMoney = "100"; } if (!string.IsNullOrEmpty(payOtherMoney)) { fastYeepayMoney = payOtherMoney; } if (fastYeepayMoney.IndexOf(',') != -1) { fastYeepayMoney = fastYeepayMoney.Substring(0, fastYeepayMoney.Length - 1); } if (!string.IsNullOrEmpty(fastYeepayUser) && !string.IsNullOrEmpty(fastYeepayMoney)) { AccountsInfo accountsInfo = accountsfacade.GetAccountsByAccontsName(fastYeepayUser); if (accountsInfo == null) { HttpCookie UserCookie2 = new HttpCookie("ErrorMsg"); UserCookie2["error"] = HttpUtility.UrlEncode("用户名不存在"); UserCookie2.Expires = DateTime.Now.AddMinutes(7); Response.Cookies.Add(UserCookie2); Response.Redirect("/showpayInfo.html"); } try { int.Parse(fastYeepayMoney); } catch (Exception) { return; } //订单号 string orderid = "KJZF-" + DateTime.Now.ToString("yyyyMMddhhmmss"); HttpCookie UserCookie = new HttpCookie("PayOrder"); UserCookie["order"] = orderid; UserCookie["orderMoney"] = fastYeepayMoney; UserCookie["orderUser"] = HttpUtility.UrlEncode(fastYeepayUser); UserCookie.Expires = DateTime.Now.AddMinutes(7); Response.Cookies.Add(UserCookie); OnLineOrder online = new OnLineOrder(); online.Accounts = fastYeepayUser; online.UserID = accountsInfo.UserID; online.OrderAmount = decimal.Parse((Convert.ToInt32(fastYeepayMoney)).ToString()); online.OrderID = orderid; online.OrderStatus = 0; online.ShareID = 20; online.CardTotal = 1; online.CardTypeID = 1; online.TelPhone = ""; online.IPAddress = GameRequest.GetUserIP(); Message msg = treasurefacade.RequestOrder(online); if (msg.Success) { //一键支付URL前缀 string apiprefix = APIURLConfig.payWebPrefix; //网页支付地址 string pcPayURI = APIURLConfig.pcwebURI; //商户账户编号 string merchantAccount = Config.merchantAccount; //商户公钥(该商户公钥需要在易宝一键支付商户后台报备) string merchantPublickey = Config.merchantPublickey; //商户私钥(商户公钥对应的私钥) string merchantPrivatekey = Config.merchantPrivatekey; //易宝支付分配的公钥(进入商户后台公钥管理,报备商户的公钥后分派的字符串) string yibaoPublickey = Config.yibaoPublickey; //随机生成商户AESkey string merchantAesKey = payapi_mobile_demo.AES.GenerateAESKey(); int amount = Convert.ToInt32(fastYeepayMoney) * 100; //支付金额为分 int currency = 156; string identityid = DateTime.Now.ToString("yyyyMMddhhmmss"); //用户身份标识 int identitytype = 0; string other = "00-23-5A-15-99-42"; //mac地址 string productcatalog = "1"; //商品类别码,商户支持的商品类别码由易宝支付运营人员根据商务协议配置 string productdesc = "789游戏中心"; string productname = "金币"; DateTime t1 = DateTime.Now; DateTime t2 = new DateTime(1970, 1, 1); double t = t1.Subtract(t2).TotalSeconds; int transtime = (int)t; string userip = GameRequest.GetUserIP(); //商户提供的商户后台系统异步支付回调地址 string callbackurl = ConfigurationManager.AppSettings["fastYeepayNotify"].ToString(); //商户提供的商户前台系统异步支付回调地址 string fcallbackurl = ConfigurationManager.AppSettings["fastYeepayCallback"].ToString(); //用户浏览器ua string userua = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1"; SortedDictionary <string, object> sd = new SortedDictionary <string, object>(); sd.Add("merchantaccount", merchantAccount); sd.Add("amount", amount); sd.Add("currency", currency); sd.Add("identityid", identityid); sd.Add("identitytype", identitytype); sd.Add("orderid", orderid); sd.Add("other", other); sd.Add("productcatalog", productcatalog); sd.Add("productdesc", productdesc); sd.Add("productname", productname); sd.Add("transtime", transtime); sd.Add("userip", userip); sd.Add("callbackurl", callbackurl); sd.Add("fcallbackurl", fcallbackurl); sd.Add("userua", userua); //生成RSA签名 string sign = EncryptUtil.handleRSA(sd, merchantPrivatekey); //Console.WriteLine("生成的签名为:" + sign); sd.Add("sign", sign); //将网页支付对象转换为json字符串 string wpinfo_json = Newtonsoft.Json.JsonConvert.SerializeObject(sd); string datastring = payapi_mobile_demo.AES.Encrypt(wpinfo_json, merchantAesKey); //将商户merchantAesKey用RSA算法加密 string encryptkey = RSAFromPkcs8.encryptData(merchantAesKey, yibaoPublickey, "UTF-8"); //打开浏览器访问一键支付网页支付链接地址,请求方式为get string postParams = "data=" + HttpUtility.UrlEncode(datastring) + "&encryptkey=" + HttpUtility.UrlEncode(encryptkey) + "&merchantaccount=" + merchantAccount; string url = apiprefix + pcPayURI + "?" + postParams; Response.Redirect(url); } else { HttpCookie UserCookie2 = new HttpCookie("ErrorMsg"); UserCookie2["error"] = HttpUtility.UrlEncode(msg.Content); UserCookie2.Expires = DateTime.Now.AddMinutes(7); Response.Cookies.Add(UserCookie2); Response.Redirect("/showpayInfo.html"); } } }