コード例 #1
0
        /// <summary>
        ///创建md5摘要,规则是:按参数名称a-z排序,遇到空值的参数不参加签名。
        /// </summary>
        public virtual void CreateSign()
        {
            StringBuilder     sb            = new StringBuilder();
            string            mchPrivateKey = string.Empty;
            string            sign          = string.Empty;
            HashAlgorithmName?halg          = null;//RSA类别
            ArrayList         akeys         = new ArrayList(parameters.Keys);

            akeys.Sort();
            string signtype = string.Empty;

            foreach (string maink in akeys)
            {
                string valuek = (string)parameters[maink];
                if (maink == "sign_type")
                {
                    signtype = valuek;
                }
            }
            //如果不是RSA签名类型就是MD5签名算法
            //if (signtype == "MD5")
            //{
            //    foreach (string k in akeys)
            //    {
            //        string v = (string)parameters[k];
            //        if (null != v && "".CompareTo(v) != 0
            //            && "sign".CompareTo(k) != 0 && "key".CompareTo(k) != 0)
            //        {
            //            sb.Append(k + "=" + v + "&");
            //        }
            //    }
            //    sb.Append("key=" + this.getKey());
            //    sign = MD5Util.GetMD5(sb.ToString(), getCharset()).ToUpper();
            //}
            //else
            //{
            int cnt = akeys.Count;
            int i   = 0;

            foreach (string k in akeys)
            {
                string v = (string)parameters[k];
                if (null != v && "".CompareTo(v) != 0 && "sign".CompareTo(k) != 0)
                {
                    if (i == (cnt - 1))
                    {
                        sb.Append(k + "=" + v);
                    }
                    else
                    {
                        sb.Append(k + "=" + v + "&");
                    }
                }
                i++;
            }
            if (signtype == "RSA_1_1")
            {
                halg = HashAlgorithmName.SHA1;
            }
            if (signtype == "RSA_1_256")
            {
                halg = HashAlgorithmName.SHA256;
            }

            mchPrivateKey = RSAConverter.RSAPrivateKeyJava2DotNet(key.PrviateKey);
            sign          = RSAHelper.SignData(mchPrivateKey, sb.ToString(), halg);
            //}

            this.setParameter("sign", sign);
            //debug信息
            this.setDebugInfo(sb.ToString() + " => sign:" + sign);
        }
コード例 #2
0
        /// <summary>
        /// 是否平台签名,规则是:按参数名称a-z排序,遇到空值的参数不参加签名。
        /// </summary>
        /// <returns></returns>
        public virtual Boolean isTenpaySign()
        {
            StringBuilder     sb       = new StringBuilder();
            HashAlgorithmName?halg     = null;         //签名类别
            string            signtype = string.Empty; //签名类型
            bool      right_sign       = false;        //检验结果真假
            ArrayList akeys            = new ArrayList(parameters.Keys);

            akeys.Sort();
            foreach (string item in akeys)
            {
                string myvalue = (string)parameters[item];
                if (item == "sign_type")
                {
                    signtype = myvalue;
                }
            }
            //if (signtype == "MD5")
            //{
            //    foreach (string k in akeys)
            //    {
            //        string v = (string)parameters[k];
            //        if (null != v && "".CompareTo(v) != 0
            //            && "sign".CompareTo(k) != 0 && "key".CompareTo(k) != 0)
            //        {
            //            sb.Append(k + "=" + v + "&");
            //        }
            //    }

            //    sb.Append("key=" + this.getKey());
            //    string sign = MD5Util.GetMD5(sb.ToString(), getCharset()).ToLower();
            //    this.setDebugInfo(sb.ToString() + " => sign:" + sign);
            //    right_sign = getParameter("sign").ToLower().Equals(sign);
            //}
            //else
            //{
            if (signtype == string.Empty)
            {
                right_sign = false;
            }
            else
            {
                int cnt = akeys.Count;
                int i   = 0;
                foreach (string k in akeys)
                {
                    string v = (string)parameters[k];
                    if (null != v && "".CompareTo(v) != 0 && "sign".CompareTo(k) != 0)
                    {
                        if (i == (cnt - 1))
                        {
                            sb.Append(k + "=" + v);
                        }
                        else
                        {
                            sb.Append(k + "=" + v + "&");
                        }
                    }
                    i++;
                }
                if (signtype == "RSA_1_1")
                {
                    halg = HashAlgorithmName.SHA1;
                }
                if (signtype == "RSA_1_256")
                {
                    halg = HashAlgorithmName.SHA256;
                }
                string wftPublicKey = RSAConverter.RSAPublicKeyJava2DotNet(options.PublicKey);
                right_sign = RSAHelper.VerifyData(wftPublicKey, sb.ToString(), getParameter("sign"), halg);
            }
            //}
            return(right_sign);
        }