示例#1
0
 private string RSASign(string prestr, string privateKey, string input_charset)
 {
     try
     {
         return(RSAFromPkcs8.sign(prestr, privateKey, input_charset));
     }
     catch (Exception e)
     {
         return(e.Message);
     }
 }
示例#2
0
        private bool Verify(HttpRequestBase request, NameValueCollection requestVal, out AliPayReturnModel model)
        {
            bool result = false;
            SortedDictionary <string, string> sortedDic = new SortedDictionary <string, string>();

            foreach (var item in requestVal.AllKeys)
            {
                if (item.ToLower() != "sign" && item.ToLower() != "sign_type" && !string.IsNullOrEmpty(item))
                {
                    sortedDic.Add(item, requestVal[item]);
                }
            }

            string requestSign     = requestVal["sign"];
            string requestSigntype = requestVal["sign_type"];
            string param           = CreateURLParamString(sortedDic);

            EnumSignType signType = requestSigntype == "MD5" ? EnumSignType.MD5
                                    : requestSigntype == "RSA" ? EnumSignType.RSA
                                    : requestSigntype == "RSA2" ? EnumSignType.RSA2 : EnumSignType.MD5;

            if (signType == EnumSignType.MD5)
            {
                string sign = BuildRequestsign(param, signType);
                if (requestSign.Equals(sign))
                {
                    result = true;
                }
            }
            else if (signType == EnumSignType.RSA2)
            {
                result = AlipaySignature.RSACheckContent(param, requestSign, AlipayConfig.ALIPay_RSA2_ALI_PUBLICKEY, requestVal["charset"], requestSigntype, false);
            }
            else
            {
                result = RSAFromPkcs8.verify(param, requestSign, AlipayConfig.ALIPay_RSA_ALI_PUBLICKEY, "utf-8");
            }

            string responseText = GetResponseTxt(requestVal["notify_id"]);

            bool resultVal = result && responseText == "true";

            if (resultVal)
            {
                model = new AliPayReturnModel()
                {
                    OutTradeNo  = request.Form["out_trade_no"],
                    TradeNo     = request.Form["trade_no"],
                    TradeStatus = request.Form["trade_status"]
                };
                model.RequestForm = request.Form.ToString();
                decimal total_fee;
                decimal.TryParse(request.Form["total_fee"], out total_fee);
                model.TotalFee = total_fee;
            }
            else
            {
                model = new AliPayReturnModel()
                {
                    RequestForm = param
                };
            }

            return(resultVal);
        }