Exemple #1
0
        /// <summary>
        /// 生成请求时的签名
        /// </summary>
        /// <param name="sPara">请求给支付宝的参数数组</param>
        /// <returns>签名结果</returns>
        private static string BuildRequestMysign(Dictionary <string, string> sPara)
        {
            //把数组所有元素,按照“参数=参数值”的模式用“&”字符拼接成字符串
            string prestr = Core.CreateLinkString(sPara);

            //把最终的字符串签名,获得签名结果
            string mysign = "";

            switch (_sign_type)
            {
            case "MD5":
                mysign = AlipayMD5.Sign(prestr, _key, _input_charset);
                break;

            case "RSA":
                mysign = RSAFromPkcs8.sign(prestr, _private_key, _input_charset);
                break;

            case "0001":
                mysign = RSAFromPkcs8.sign(prestr, _private_key, _input_charset);
                break;

            default:
                mysign = "";
                break;
            }

            return(mysign);
        }
Exemple #2
0
        /// <summary>
        /// 获取返回时的签名验证结果
        /// </summary>
        /// <param name="inputPara">通知返回参数数组</param>
        /// <param name="sign">对比的签名结果</param>
        /// <returns>签名验证结果</returns>
        private bool GetSignVeryfy(SortedDictionary <string, string> inputPara, string sign)
        {
            Dictionary <string, string> sPara = new Dictionary <string, string>();

            //过滤空值、sign与sign_type参数
            sPara = Core.FilterPara(inputPara);

            //获取待签名字符串
            string preSignStr = Core.CreateLinkString(sPara);

            //获得签名验证结果
            bool isSgin = false;

            if (sign != null && sign != "")
            {
                switch (_sign_type)
                {
                case "RSA":
                    isSgin = RSAFromPkcs8.verify(preSignStr, sign, _public_key, _input_charset);
                    break;

                case "MD5":
                    isSgin = AlipayMD5.Verify(preSignStr, sign, _public_key, _input_charset);
                    break;

                default:
                    break;
                }
            }

            return(isSgin);
        }
Exemple #3
0
        /// <summary>
        /// 获取返回时的签名验证结果
        /// get the result of verification of returned notification
        /// </summary>
        /// <param name="inputPara">通知返回参数数组the params from the feedback notification</param>
        /// <param name="sign">对比的签名结果the sign to be compared</param>
        /// <returns>签名验证结果the result of verification</returns>
        private bool GetSignVeryfy(SortedDictionary <string, string> inputPara, string sign)
        {
            Dictionary <string, string> sPara = new Dictionary <string, string>();

            //过滤空值、sign与sign_type参数
            //Filter parameters with null value ,sign and sign_type
            sPara = Core.FilterPara(inputPara);

            //获取待签名字符串
            //Generate the pre-sign string
            string preSignStr = Core.CreateLinkString(sPara);

            //获得签名验证结果
            //get the result of verification
            bool isSgin = false;

            if (sign != null && sign != "")
            {
                switch (_sign_type)
                {
                case "RSA":
                    isSgin = RSAFromPkcs8.verify(preSignStr, sign, alipay_public_key, _input_charset);
                    break;

                default:
                    break;
                }
            }

            return(isSgin);
        }
Exemple #4
0
        /// <summary>
        /// 生成请求时的签名
        /// </summary>
        /// <param name="sPara">请求给支付宝的参数数组</param>
        /// <returns>签名结果</returns>
        private string BuildRequestMysign(Dictionary <string, string> sPara)
        {
            //把数组所有元素,按照“参数=参数值”的模式用“&”字符拼接成字符串
            string prestr = Core.CreateLinkString(sPara);

            //把最终的字符串签名,获得签名结果
            string mysign = "";

            switch (_signType)
            {
            case SignType.RSA:
                mysign = RSAFromPkcs8.Sign(prestr, _privateKey, _inputCharset);
                break;

            case SignType.MD5:
                mysign = AlipayMD5.Sign(prestr, _md5Key, _inputCharset);
                break;

            default:
                mysign = "";
                break;
            }

            return(mysign);
        }
Exemple #5
0
        ///// <summary>
        ///// 获取待签名字符串(调试用)
        ///// </summary>
        ///// <param name="inputPara">通知返回参数数组</param>
        ///// <returns>待签名字符串</returns>
        //private string GetPreSignStr(SortedDictionary<string, string> inputPara)
        //{
        //    Dictionary<string, string> sPara = new Dictionary<string, string>();

        //    //过滤空值、sign与sign_type参数
        //    sPara = Core.FilterPara(inputPara);

        //    //获取待签名字符串
        //    string preSignStr = Core.CreateLinkString(sPara);

        //    return preSignStr;
        //}

        /// <summary>
        /// 获取返回时的签名验证结果
        /// </summary>
        /// <param name="inputPara">通知返回参数数组</param>
        /// <param name="sign">对比的签名结果</param>
        /// <returns>签名验证结果</returns>
        public bool GetSignVeryfy(SortedDictionary <string, string> inputPara, string sign)
        {
            Dictionary <string, string> sPara = new Dictionary <string, string>();

            //过滤空值、sign与sign_type参数
            sPara = Core.FilterPara(inputPara);

            //获取待签名字符串
            string preSignStr = Core.CreateLinkString(sPara);
            //获得签名验证结果
            bool isSgin = false;

            if (!string.IsNullOrWhiteSpace(sign))
            {
                switch (_signType)
                {
                case SignType.RSA:
                    isSgin = RSAFromPkcs8.Verify(preSignStr, sign, _alipayPublicKey, _inputCharset);
                    break;

                case SignType.MD5:
                    isSgin = AlipayMD5.Verify(preSignStr, sign, _md5Key, _inputCharset);
                    break;

                default:
                    break;
                }
            }

            return(isSgin);
        }
Exemple #6
0
        /// <summary>
        /// 获取返回时的签名验证结果
        /// </summary>
        /// <param name="inputPara">通知返回参数数组</param>
        /// <param name="sign">对比的签名结果</param>
        /// <param name="isSort">是否对待签名数组排序</param>
        /// <returns>签名验证结果</returns>
        private bool GetSignVeryfy(Dictionary <string, string> inputPara, string sign, bool isSort)
        {
            try
            {
                Dictionary <string, string> sPara = new Dictionary <string, string>();

                //过滤空值、sign与sign_type参数
                sPara = Core.FilterPara(inputPara);

                if (isSort)
                {
                    //根据字母a到z的顺序把参数排序
                    sPara = Core.SortPara(sPara);
                }
                else
                {
                    sPara = SortNotifyPara(sPara);
                }

                //获取待签名字符串
                string preSignStr = Core.CreateLinkString(sPara);
                logger.InfoFormat("preSignStr:{0}", preSignStr);
                //获得签名验证结果
                bool isSgin = false;
                if (!string.IsNullOrEmpty(sign))
                {
                    logger.InfoFormat("_sign_type:{0}", _sign_type);
                    switch (_sign_type)
                    {
                    case "MD5":
                        isSgin = AlipayMD5.Verify(preSignStr, sign, _key, _input_charset);
                        break;

                    case "RSA":
                        isSgin = RSAFromPkcs8.verify(preSignStr, sign, _public_key, _input_charset);
                        break;

                    case "0001":
                        isSgin = RSAFromPkcs8.verify(preSignStr, sign, _public_key, _input_charset);
                        break;

                    default:
                        break;
                    }
                }

                return(isSgin);
            }
            catch (Exception exc)
            {
                logger.ErrorFormat("异常:{0})", exc.Message);
                return(false);

                throw;
            }
        }
Exemple #7
0
        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="inputPara">要解密数据</param>
        /// <returns>解密后结果</returns>
        public Dictionary <string, string> Decrypt(Dictionary <string, string> inputPara)
        {
            try
            {
                inputPara["notify_data"] = RSAFromPkcs8.decryptData(inputPara["notify_data"], _private_key, _input_charset);
            }
            catch (Exception e) { }

            return(inputPara);
        }
Exemple #8
0
        public bool AppVerify(string text, string sign)
        {
            //获取返回时的签名验证结果
            bool isSign = false;

            isSign = RSAFromPkcs8.verify(text, sign, _public_key, _input_charset);

            if (isSign)//验证成功
            {
                return(true);
            }
            else//验证失败
            {
                return(false);
            }
        }
Exemple #9
0
        /// <summary>
        /// 解析远程模拟提交后返回的信息
        /// </summary>
        /// <param name="strText">要解析的字符串</param>
        /// <returns>解析结果</returns>
        public static Dictionary <string, string> ParseResponse(string strText)
        {
            //以“&”字符切割字符串
            string[] strSplitText = strText.Split('&');
            //把切割后的字符串数组变成变量与数值组合的字典数组
            Dictionary <string, string> dicText = new Dictionary <string, string>();

            for (int i = 0; i < strSplitText.Length; i++)
            {
                //获得第一个=字符的位置
                int nPos = strSplitText[i].IndexOf('=');
                //获得字符串长度
                int nLen = strSplitText[i].Length;
                //获得变量名
                string strKey = strSplitText[i].Substring(0, nPos);
                //获得数值
                string strValue = strSplitText[i].Substring(nPos + 1, nLen - nPos - 1);
                //放入字典类数组中
                dicText.Add(strKey, strValue);
            }

            if (dicText["res_data"] != null)
            {
                //解析加密部分字符串(RSA与MD5区别仅此一句)
                if (_sign_type == "0001")
                {
                    dicText["res_data"] = RSAFromPkcs8.decryptData(dicText["res_data"], _private_key, _input_charset);
                }

                //token从res_data中解析出来(也就是说res_data中已经包含token的内容)
                XmlDocument xmlDoc = new XmlDocument();
                try
                {
                    xmlDoc.LoadXml(dicText["res_data"]);
                    string strRequest_token = xmlDoc.SelectSingleNode("/direct_trade_create_res/request_token").InnerText;
                    dicText.Add("request_token", strRequest_token);
                }
                catch (Exception exp)
                {
                    dicText.Add("request_token", exp.ToString());
                }
            }

            return(dicText);
        }
Exemple #10
0
        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="inputPara">要解密数据</param>
        /// <returns>解密后结果</returns>
        public Dictionary <string, string> Decrypt(Dictionary <string, string> inputPara)
        {
            try
            {
                var dd = inputPara["notify_data"];
                //logger.InfoFormat("Decrypt,之前:{0}", dd);
                var dr = RSAFromPkcs8.decryptData(dd, _private_key, _input_charset);
                //logger.InfoFormat("Decrypt,之后:{0}",  dr);
                inputPara["notify_data"] = dr;
            }
            catch (Exception exc)
            {
                logger.InfoFormat("DecryptException:{0}", exc.Message);
                throw;
            }

            return(inputPara);
        }
Exemple #11
0
        /// <summary>
        /// 生成请求时的签名Generate the sign
        /// </summary>
        /// <param name="sPara">请求给支付宝的参数数组Parameters to sign</param>
        /// <returns>签名结果sign generated</returns>
        private static string BuildRequestMysign(Dictionary <string, string> sPara)
        {
            //把数组所有元素,按照“参数=参数值”的模式用“&”字符拼接成字符串
            //Rearrange parameters in the data set alphabetically and connect rearranged parameters with & like "parametername=value"
            string prestr = Core.CreateLinkString(sPara);

            //把最终的字符串签名,获得签名结果
            //get the sign
            string mysign = "";

            switch (_sign_type)
            {
            case "RSA":
                mysign = RSAFromPkcs8.sign(prestr, _private_key, _input_charset);
                break;

            default:
                prestr = prestr + Config.md5key;
                mysign = Core.GetAbstractToMD5((Encoding.UTF8.GetBytes(prestr)));
                break;
            }

            return(mysign);
        }
Exemple #12
0
 /// <summary>
 /// 签名--动态读取XML,解析XML数据,返回签名
 /// </summary>
 /// <param name="strXML"></param>
 /// <returns></returns>
 public string RSASignFromXML(string strXML)
 {
     return(RSAFromPkcs8.sign(new AlipayPOS().SignString(strXML), _Private_key));
 }