Exemple #1
0
        /// <summary>
        /// 验签(排序验签)
        /// </summary>
        /// <param name="sArrary">待签名数组</param>
        /// <param name="signedString">签名(支付宝返回sign)</param>
        /// <param name="publickey">支付宝公钥</param>
        /// <param name="input_charset">编码格式</param>
        /// <returns>返回验签结果,true(相同),false(不相同)</returns>
        public static bool Verify(SortedDictionary <string, string> sArrary, string signedString, string publickey,
                                  string input_charset)
        {
            Dictionary <string, string> sPara = Function.FilterPara(sArrary);
            string content = Function.CreateLinkString(sPara);
            bool   b       = RSAFromPkcs8.verify(content, signedString, publickey, input_charset);

            return(b);
        }
        /// <summary>
        /// 生成要请求给支付宝的参数数组
        /// </summary>
        /// <param name="sParaTemp">请求前的参数数组</param>
        /// <param name="input_charset">编码格式</param>
        /// <param name="key">MD5校验码</param>
        /// <param name="sign_type">签名类型</param>
        /// <returns>要请求的参数数组</returns>
        private static Dictionary <string, string> BuildRequestPara(SortedDictionary <string, string> sParaTemp, string input_charset, string key, string sign_type)
        {
            //签名结果
            string mysign = "";

            //获得签名结果
            mysign = Function.BuildMysign(sParaTemp, key, sign_type, input_charset);

            Dictionary <string, string> sPara = Function.FilterPara(sParaTemp);

            sPara.Add("sign", mysign);
            return(sPara);
        }
        /// <summary>
        /// 返回token字符串
        /// </summary>
        /// <param name="strResult">创建订单返回信息</param>
        /// <param name="sellprivatekey">商户私钥</param>
        /// <param name="input_charset">编码格式</param>
        /// <returns>token字符串</returns>
        public static string Res_dataDecrypt(string strResult, string sellprivatekey, string input_charset)
        {
            //分解返回数据 用&拆分赋值给result
            string[] result = strResult.Split('&');

            //提取res_data参数
            string res_data = string.Empty;

            for (int i = 0; i < result.Length; i++)
            {
                string data = result[i];
                if (data.IndexOf("res_data=") >= 0)
                {
                    res_data = data.Replace("res_data=", string.Empty);

                    //解密(用"商户私钥"对"res_data"进行解密)
                    res_data = Function.Decrypt(res_data, sellprivatekey, input_charset);

                    //res_data 赋值 给 result[0]
                    result[i] = "res_data=" + res_data;
                }
            }


            //创建待签名数组
            SortedDictionary <string, string> sd = new SortedDictionary <string, string>();
            int    count  = 0;
            string sparam = "";
            string key    = "";
            string value  = "";

            for (int i = 0; i < result.Length; i++)
            {
                sparam = result[i];
                count  = sparam.IndexOf('=');
                key    = sparam.Substring(0, count);
                value  = sparam.Substring(count + 1, sparam.Length - (count + 1));
                sd.Add(key, value);
            }

            string sign = sd["sign"];

            //配置待签名数据
            Dictionary <string, string> dicData = Function.FilterPara(sd);
            string req_Data = Function.CreateLinkString(dicData);

            //验签,使用支付宝公钥
            bool vailSign = RSAFromPkcs8.verify(req_Data, sign, Config.Alipaypublick, input_charset);

            if (vailSign)//验签通过
            {
                //得到 request_token 的值
                string token = string.Empty;
                try
                {
                    token = Function.GetStrForXmlDoc(res_data, "direct_trade_create_res/request_token");
                }
                catch
                {
                    //提示 返回token值无效
                    return(string.Empty);
                }
                return(token);
            }
            else
            {
                //验签不通过,这里商户自己写逻辑
                return(string.Empty);
            }
        }