Beispiel #1
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 #2
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 #3
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));
        }