Beispiel #1
0
        private JdPayDictionary GetEncryptDicContent <T>(IJdPayRequest <T> request, IDictionary <string, string> dic) where T : JdPayResponse
        {
            var signDic = new JdPayDictionary(dic)
            {
                { VERSION, request.GetApiVersion() },
                { MERCHANT, Options.Merchant },
            };

            var sign = JdPaySignature.RSASign(signDic, RSAPrivateParameters);

            var encyptDic = new JdPayDictionary
            {
                { VERSION, request.GetApiVersion() },
                { MERCHANT, Options.Merchant },
                { SIGN, sign }
            };

            foreach (var item in dic)
            {
                if (!string.IsNullOrEmpty(item.Value))
                {
                    encyptDic.Add(item.Key, DES3.EncryptECB(DesKey, item.Value));
                }
            }
            return(encyptDic);
        }
Beispiel #2
0
        public JdPayClient(
            IOptions <JdPayOptions> optionsAccessor,
            ILogger <JdPayClient> logger)
        {
            Options = optionsAccessor?.Value ?? new JdPayOptions();
            Logger  = logger;
            Client  = new HttpClientEx();

            if (string.IsNullOrEmpty(Options.Merchant))
            {
                throw new ArgumentNullException(nameof(Options.Merchant));
            }

            if (string.IsNullOrEmpty(Options.RsaPrivateKey))
            {
                throw new ArgumentNullException(nameof(Options.RsaPrivateKey));
            }

            if (string.IsNullOrEmpty(Options.RsaPublicKey))
            {
                throw new ArgumentNullException(nameof(Options.RsaPublicKey));
            }

            if (string.IsNullOrEmpty(Options.DesKey))
            {
                throw new ArgumentNullException(nameof(Options.DesKey));
            }

            RSAPrivateParameters = JdPaySignature.GetPrivateKeyParameters(Options.RsaPrivateKey);
            RSAPublicParameters  = JdPaySignature.GetPublicKeyParameters(Options.RsaPublicKey);
            DesKey = Convert.FromBase64String(Options.DesKey);
        }
Beispiel #3
0
        public JdPayNotifyClient(JdPayOptions options)
        {
            Options = options;

            if (!string.IsNullOrEmpty(Options.RsaPrivateKey))
            {
                RSAPrivateParameters = JdPaySignature.GetPrivateKeyParameters(Options.RsaPrivateKey);
            }

            if (!string.IsNullOrEmpty(Options.RsaPublicKey))
            {
                RSAPublicParameters = JdPaySignature.GetPublicKeyParameters(Options.RsaPublicKey);
            }
        }
Beispiel #4
0
        private T DecryptResponseXmlContent <T>(string xml) where T : JdPayResponse
        {
            if (string.IsNullOrEmpty(xml))
            {
                throw new Exception("Content is Empty!");
            }

            var entity = JdPayUtil.Deserialize <T>(xml);

            if (!string.IsNullOrEmpty(entity?.Encrypt))
            {
                var base64EncryptStr = Encoding.UTF8.GetString(Convert.FromBase64String(entity.Encrypt));
                var reqBody          = DES3.DecryptECB(DesKey, 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);
                }
                if (JdPaySignature.RSACheckContent(reqBodyStr, inputSign, RSAPublicParameters))
                {
                    entity = JdPayUtil.Deserialize <T>(reqBody);
                }
                else
                {
                    throw new Exception("sign check fail: check Sign and Data Fail!");
                }

                entity.Body = reqBody;
            }
            else
            {
                entity.Body = xml;
            }
            return(entity);
        }
Beispiel #5
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);
        }
Beispiel #6
0
        private string GetEncryptXmlContent <T>(IJdPayRequest <T> request, JdPayDictionary dic) where T : JdPayResponse
        {
            var xmldoc = JdPayUtil.SortedDictionary2AllXml(dic);
            var smlStr = JdPayUtil.ConvertXmlToString(xmldoc);
            var sha256SourceSignString = SHA256.Compute(smlStr);
            var encyptBytes            = JdPaySignature.Encrypt(sha256SourceSignString, RSAPrivateParameters);
            var sign    = Convert.ToBase64String(encyptBytes, Base64FormattingOptions.InsertLineBreaks);
            var data    = smlStr.Replace("</jdpay>", "<sign>" + sign + "</sign></jdpay>");
            var encrypt = DES3.EncryptECB(DesKey, data);
            // 字典排序
            var reqdic = new JdPayDictionary
            {
                { VERSION, request.GetApiVersion() },
                { MERCHANT, Options.Merchant },
                { ENCRYPT, Convert.ToBase64String(Encoding.UTF8.GetBytes(encrypt)) }
            };

            return(JdPayUtil.SortedDictionary2XmlStr(reqdic));
        }
Beispiel #7
0
        private void CheckNotifySign(JdPayDictionary content, object parameters)
        {
            if (content.Count == 0)
            {
                throw new Exception("sign check fail: Body is Empty!");
            }

            var sign = content["sign"];

            if (string.IsNullOrEmpty(sign))
            {
                throw new Exception("sign check fail: sign is Empty!");
            }

            if (!JdPaySignature.RSACheckContent(content, sign, RSAPublicParameters))
            {
                throw new Exception("sign check fail: check Sign and Data Fail");
            }
        }