Esempio n. 1
0
        private T DecryptResponseXml <T>(string xml) where T : JdPayResponse
        {
            var entity = JdPayUtil.Deserialize <T>(typeof(T), xml);

            if (!string.IsNullOrEmpty(entity?.Encrypt))
            {
                var key = Convert.FromBase64String(Options.DesKey);
                var base64EncryptStr = Encoding.UTF8.GetString(Convert.FromBase64String(entity.Encrypt));
                var reqBody          = Des3.Des3DecryptECB(key, base64EncryptStr);

                var reqBodyDoc = new XmlDocument();
                reqBodyDoc.LoadXml(reqBody);

                var inputSign = JdPayUtil.GetValue(reqBodyDoc, "sign");
                var jdpayRoot = reqBodyDoc.SelectSingleNode("jdpay");
                var signNode  = jdpayRoot.SelectSingleNode("sign");
                jdpayRoot.RemoveChild(signNode);

                var reqBodyStr = JdPayUtil.ConvertXmlToString(reqBodyDoc);
                var xmlh       = xml.Substring(0, xml.IndexOf("<jdpay>"));
                if (!string.IsNullOrEmpty(xmlh))
                {
                    reqBodyStr = reqBodyStr.Replace("<?xml version=\"1.0\" encoding=\"UTF-8\"?>", xmlh);
                }
                var sha256SourceSignString = Sha256.Encrypt(reqBodyStr);
                var decryptByte            = JdPaySignature.Decrypt(inputSign, Options.RsaPublicKey);
                var decryptStr             = Des3.BytesToString(decryptByte);
                if (sha256SourceSignString.Equals(decryptStr))
                {
                    entity = JdPayUtil.Deserialize <T>(typeof(T), reqBody);
                }
                else
                {
                    throw new Exception("sign check fail: check Sign and Data Fail!");
                }

                entity.Body = reqBody;
            }
            else
            {
                entity.Body = xml;
            }
            return((T)entity);
        }
Esempio n. 2
0
        private string BuildEncryptXml <T>(IJdPayRequest <T> request, JdPayDictionary dic) where T : JdPayResponse
        {
            var xmldoc = JdPayUtil.SortedDictionary2AllXml(dic);
            var smlStr = JdPayUtil.ConvertXmlToString(xmldoc);
            var sha256SourceSignString = Sha256.Encrypt(smlStr);
            var encyptBytes            = JdPaySignature.Encrypt(sha256SourceSignString, Options.RsaPrivateKey);
            var sign    = Convert.ToBase64String(encyptBytes, Base64FormattingOptions.InsertLineBreaks);
            var data    = smlStr.Replace("</jdpay>", "<sign>" + sign + "</sign></jdpay>");
            var key     = Convert.FromBase64String(Options.DesKey);
            var encrypt = Des3.Des3EncryptECB(key, data);
            // 字典排序
            var reqdic = new JdPayDictionary
            {
                { VERSION, request.GetApiVersion() },
                { MERCHANT, Options.Merchant },
                { ENCRYPT, Convert.ToBase64String(Encoding.UTF8.GetBytes(encrypt)) }
            };

            return(JdPayUtil.SortedDictionary2XmlStr(reqdic));
        }
Esempio n. 3
0
        public static string Encrypt(string strText)
        {
            try
            {
                byte[] keys = Encoding.GetEncoding("utf-8").GetBytes(_3dkey);
                byte[] data = Encoding.GetEncoding("utf-8").GetBytes(strText);
                byte[] iv   = Encoding.GetEncoding("utf-8").GetBytes("12345678");

                byte[] result = Des3.Des3EncodeECB(keys, iv, data);
                if (result != null)
                {
                    return(ToHexString(result));
                }

                return(string.Empty);
            }
            catch (System.Exception ex)
            {
                ExceptionHandler.HandleException(ex);
                return(string.Empty);
            }
        }
Esempio n. 4
0
        /// <summary>
        /// 行情客户端用户登陆
        /// </summary>
        /// <param name="tradeAccount">登陆账号</param>
        /// <param name="tradePwd">登陆密码</param>
        /// <param name="mac">mac地址</param>
        /// <returns>Loginfo</returns>
        public Loginfo GetLoginEx(string tradeAccount, string tradePwd, string mac)
        {
            //实例化实体类
            Loginfo loginfo = new Loginfo();

            loginfo.LoginID = "-1";
            try
            {
                tradePwd = Des3.Des3EncodeCBC(tradePwd);
                //判断用户是否存在 函数1
                if (ComFunction.ListLogin(ref loginfo, tradeAccount, tradePwd, mac))
                {
                    loginfo.LoginID         = System.Guid.NewGuid().ToString().Replace("-", "");
                    loginfo.QuotesAddressIP = ComFunction.ip;
                    loginfo.QuotesPort      = Convert.ToInt32(ComFunction.port);
                }
            }
            catch (Exception ex)
            {
                ComFunction.WriteErr(ex);
                loginfo.LoginID = "-1";
            }
            return(loginfo);
        }
Esempio n. 5
0
    /// <summary>
    /// 类测试
    /// </summary>
    public static void Test()
    {
        System.Text.Encoding utf8 = System.Text.Encoding.UTF8;

        //key为abcdefghijklmnopqrstuvwx的Base64编码
        string ydata = "00068900002||20180927095839|||3002800173|0200000777|01|0155129175|||";

        byte[] key = Convert.FromBase64String("YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4");
        byte[] iv  = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };     //当模式为ECB时,IV无用

        byte[] data = utf8.GetBytes(ydata);
        System.Console.WriteLine("原始报文:");
        System.Console.WriteLine(ydata);
        System.Console.WriteLine();

        byte[] str1 = Des3.Des3EncodeECB(key, iv, data);
        byte[] str2 = Des3.Des3DecodeECB(key, iv, str1);
        System.Console.WriteLine("ECB加密报文:");
        System.Console.WriteLine(Convert.ToBase64String(str1));
        System.Console.WriteLine("ECB解密报文:");
        System.Console.WriteLine(System.Text.Encoding.UTF8.GetString(str2));

        System.Console.WriteLine();


        byte[] str3 = Des3.Des3EncodeCBC(key, iv, data);
        byte[] str4 = Des3.Des3DecodeCBC(key, iv, str3);
        System.Console.WriteLine("CBC加密报文:");
        System.Console.WriteLine(Convert.ToBase64String(str3));
        System.Console.WriteLine("CBC解密报文:");
        System.Console.WriteLine(utf8.GetString(str4));

        System.Console.WriteLine();

        Console.ReadLine();
    }
Esempio n. 6
0
        public string ChargeByJdpay(string productName, decimal amount, string orderNo, string returnUrl)
        {
            var config = ConfigInfo;

            if (!config.IsJdpay)
            {
                return(null);
            }

            //var callbackUrl = Utils.AddProtocolToUrl(Pay.GetUrl(PageUtility.OuterApiUrl, returnUrl));
            var callbackUrl = returnUrl;

            var orderInfoDic = new SortedDictionary <string, string>
            {
                { "version", "V2.0" },
                { "merchant", config.JdpayMerchant },
                { "device", "111" },
                { "tradeNum", orderNo },
                { "tradeName", productName },
                { "tradeDesc", "交易描述" },
                { "tradeTime", DateTime.Now.ToString("yyyyMMddHHmmss", DateTimeFormatInfo.InvariantInfo) },
                { "amount", Convert.ToInt32(amount * 100).ToString() },
                { "currency", "CNY" },
                { "note", "备注" },
                { "callbackUrl", callbackUrl },
                { "notifyUrl", string.Empty },
                { "ip", Utils.GetIpAddress() },
                { "specCardNo", string.Empty },
                { "specId", string.Empty },
                { "specName", string.Empty },
                { "userType", string.Empty },
                //{"userId", config.JdpayUserId},
                { "userId", Utils.GetShortGuid() },
                { "expireTime", string.Empty },
                { "orderType", "1" },
                { "industryCategoryCode", string.Empty }
            };

            var priKey          = config.JdpayPrivateKey;
            var desKey          = config.JdpayDesKey;
            var unSignedKeyList = new List <string> {
                "sign"
            };
            var signStr = SignUtil.signRemoveSelectedKeys(orderInfoDic, priKey, unSignedKeyList);

            orderInfoDic.Add("sign", signStr);
            byte[] key = Convert.FromBase64String(desKey);
            //当模式为ECB时,IV无用,java默认使用的ECB
            if (!string.IsNullOrEmpty(orderInfoDic["device"]))
            {
                //String desStr = Des3.Des3EncryptECB(key, orderInfoDic["device"));
                orderInfoDic["device"] = Des3.Des3EncryptECB(key, orderInfoDic["device"]);
                //String str = Des3.Des3DecryptECB(key, desStr);
            }
            orderInfoDic["tradeNum"] = Des3.Des3EncryptECB(key, orderInfoDic["tradeNum"]);
            if (!string.IsNullOrEmpty(orderInfoDic["tradeName"]))
            {
                orderInfoDic["tradeName"] = Des3.Des3EncryptECB(key, orderInfoDic["tradeName"]);
            }
            if (!string.IsNullOrEmpty(orderInfoDic["tradeDesc"]))
            {
                orderInfoDic["tradeDesc"] = Des3.Des3EncryptECB(key, orderInfoDic["tradeDesc"]);
            }
            orderInfoDic["tradeTime"] = Des3.Des3EncryptECB(key, orderInfoDic["tradeTime"]);
            orderInfoDic["amount"]    = Des3.Des3EncryptECB(key, orderInfoDic["amount"]);
            orderInfoDic["currency"]  = Des3.Des3EncryptECB(key, orderInfoDic["currency"]);
            if (!string.IsNullOrEmpty(orderInfoDic["note"]))
            {
                orderInfoDic["note"] = Des3.Des3EncryptECB(key, orderInfoDic["note"]);
            }
            orderInfoDic["callbackUrl"] = Des3.Des3EncryptECB(key, orderInfoDic["callbackUrl"]);
            orderInfoDic["notifyUrl"]   = Des3.Des3EncryptECB(key, orderInfoDic["notifyUrl"]);
            orderInfoDic["ip"]          = Des3.Des3EncryptECB(key, orderInfoDic["ip"]);
            if (!string.IsNullOrEmpty(orderInfoDic["userType"]))
            {
                orderInfoDic["userType"] = Des3.Des3EncryptECB(key, orderInfoDic["userType"]);
            }
            if (!string.IsNullOrEmpty(orderInfoDic["userId"]))
            {
                orderInfoDic["userId"] = Des3.Des3EncryptECB(key, orderInfoDic["userId"]);
            }
            if (!string.IsNullOrEmpty(orderInfoDic["expireTime"]))
            {
                orderInfoDic["expireTime"] = Des3.Des3EncryptECB(key, orderInfoDic["expireTime"]);
            }
            if (!string.IsNullOrEmpty(orderInfoDic["orderType"]))
            {
                orderInfoDic["orderType"] = Des3.Des3EncryptECB(key, orderInfoDic["orderType"]);
            }
            if (!string.IsNullOrEmpty(orderInfoDic["industryCategoryCode"]))
            {
                orderInfoDic["industryCategoryCode"] = Des3.Des3EncryptECB(key, orderInfoDic["industryCategoryCode"]);
            }
            if (!string.IsNullOrEmpty(orderInfoDic["specCardNo"]))
            {
                orderInfoDic["specCardNo"] = Des3.Des3EncryptECB(key, orderInfoDic["specCardNo"]);
            }
            if (!string.IsNullOrEmpty(orderInfoDic["specId"]))
            {
                orderInfoDic["specId"] = Des3.Des3EncryptECB(key, orderInfoDic["specId"]);
            }
            if (!string.IsNullOrEmpty(orderInfoDic["specName"]))
            {
                orderInfoDic["specName"] = Des3.Des3EncryptECB(key, orderInfoDic["specName"]);
            }

            StringBuilder sbHtml = new StringBuilder();

            sbHtml.Append("<form id='jdpaysubmit' name='jdpaysubmit' action='https://wepay.jd.com/jdpay/saveOrder' method='post'>");

            foreach (KeyValuePair <string, string> temp in orderInfoDic)
            {
                sbHtml.Append("<input type='hidden' name='" + temp.Key + "' value='" + temp.Value + "'/>");
            }

            sbHtml.Append("<script>document.forms['jdpaysubmit'].submit();</script>");

            return(sbHtml.ToString());
        }
        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="LoginID">用户登陆标识ID</param>
        /// <param name="PwdType">0表示修改登陆密码,1表示修改资金密码</param>
        /// <param name="oldpwd">旧密码</param>
        /// <param name="newpwd">新密码</param>
        /// <returns></returns>
        public ResultDesc ModifyUserPassword(string LoginID, int PwdType, string oldpwd, string newpwd)
        {
            ResultDesc rsdc = new ResultDesc();

            try
            {
                TradeUser tdUser = new TradeUser();

                List <string> sqlList = new List <string>();


                if (!ComFunction.ExistUserLoginID(LoginID, ref tdUser))
                {
                    rsdc.Result     = false;
                    rsdc.ReturnCode = ResCode.UL003;
                    rsdc.Desc       = ResCode.UL003Desc;
                    return(rsdc);
                }
                //密码转换
                oldpwd = Des3.Des3EncodeCBC(oldpwd);
                newpwd = Des3.Des3EncodeCBC(newpwd);

                string sql1 = string.Empty;
                if (PwdType == 0)//登陆密码
                {
                    if (tdUser.LoginPwd == oldpwd)
                    {
                        sql1 = string.Format("UPDATE Base_User  SET  LoginPwd='{0}'  WHERE userid='{1}' AND LoginPwd='{2}'", newpwd, tdUser.UserID, oldpwd);
                        sqlList.Add(sql1);
                    }
                    else
                    {
                        rsdc.Result = false;
                        rsdc.Desc   = "登陆密码不一致,修改失败";
                        return(rsdc);
                    }
                }
                else//资金密码
                {
                    if (tdUser.CashPwd == oldpwd)
                    {
                        sql1 = string.Format("UPDATE Base_User  SET  CashPwd='{0}'  WHERE userid='{1}' AND CashPwd='{2}'", newpwd, tdUser.UserID, oldpwd);
                        sqlList.Add(sql1);
                    }
                    else
                    {
                        rsdc.Result = false;
                        rsdc.Desc   = "资金密码不一致,修改失败";
                        return(rsdc);
                    }
                }
                if (!ComFunction.SqlTransaction(sqlList))
                {
                    LogNet4.WriteMsg(string.Format("修改密码失败,SQL语句执行失败,SQL语句是:{0}", sql1));
                    rsdc.Result = false;
                    rsdc.Desc   = "用户密码不对,修改密码失败";
                    return(rsdc);
                }
                rsdc.Result = true;
                rsdc.Desc   = "修改密码,成功";
            }
            catch (Exception ex)
            {
                ComFunction.WriteErr(ex);
                rsdc.Result = false;
                rsdc.Desc   = "修改密码,失败";
            }
            return(rsdc);
        }
Esempio n. 8
0
 static void Main(string[] args)
 {
     Des3.Test();
 }