zoukankan      html  css  js  c++  java
  • 带"签名"的请求接口实现

    废话少说,直接上代码(⊙﹏⊙)

     class Program
        {
            //签名证书
            public static X509Certificate2 cerSigneCert;
            private static char[] hexChars;
            public Program()
            {
                cerSigneCert = new X509Certificate2(AppDomain.CurrentDomain.BaseDirectory + @"resources20016100001118204.p12", "");
            }
            static void Main(string[] args)
            {
                string xml = "xml字符串 <SIGNED_MSG />";
                string newXML = SHA1withRSA(xml);
                string url = "访问地址";
                string requestUrl = RequestUrl(url, newXML);
    
            }
    
            /// <summary>
            /// 签名
            /// PS:(SIGNED_MSG是属性的名称)
            /// </summary>
            /// <param name="xml"></param>
            /// <returns></returns>
            private static string SHA1withRSA(string xml)
            {
                string oldXML = xml.Replace("<SIGNED_MSG />", "");
                SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider();
                byte[] msg = sha1.ComputeHash(Encoding.GetEncoding("GBK").GetBytes(oldXML));
    
                RSAPKCS1SignatureFormatter signe = new RSAPKCS1SignatureFormatter();
    
                signe.SetKey(cerSigneCert.PrivateKey);//设置用于签名的私钥
                signe.SetHashAlgorithm("SHA1");
                var signeText = ToHex(signe.CreateSignature(msg));//创建签名
    
                string newXML = xml.Replace("<SIGNED_MSG />", "<SIGNED_MSG>" + signeText + "</SIGNED_MSG>");
                return newXML;
            }
    
            private static string ToHex(byte[] ba)
            {
                if (ba == null) return "";
                char[]
                    buf = new char[ba.Length * 2];
    
                int p = 0;
                foreach (byte b in ba)
                {
                    buf[p++] = hexChars[b >> 4];
                    buf[p++] = hexChars[b & 0x0f];
                }
                return new string(buf);
            }
    
    
            /// <summary>
            /// 发送请求,获取响应
            /// </summary>
            /// <param name="url"></param>
            /// <param name="data"></param>
            /// <returns></returns>
            public static string RequestUrl(string url, string data)//发送方法
            {
                var request = WebRequest.Create(url) as HttpWebRequest;
    
                request.ProtocolVersion = HttpVersion.Version11;
                // 这里设置了协议类型。
                request.KeepAlive = false;
    
                ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(RemoteCertificateValidate);
                ServicePointManager.CheckCertificateRevocationList = true;
                ServicePointManager.DefaultConnectionLimit = 100;
                ServicePointManager.Expect100Continue = false;
    
                request.Method = "post";
                request.ContentType = "text/xml";
                request.Headers.Add("charset:gbk");
                var encoding = Encoding.GetEncoding("GBK");
    
                if (data != null)
                {
                    byte[] buffer = encoding.GetBytes(data);
                    request.ContentLength = buffer.Length;
                    request.GetRequestStream().Write(buffer, 0, buffer.Length);
                }
                else
                {
    
                }
    
                using (HttpWebResponse wr = request.GetResponse() as HttpWebResponse)
                {
                    using (StreamReader reader = new StreamReader(wr.GetResponseStream(), encoding))
                    {
                        string strResult = reader.ReadToEnd();
                        return strResult;
                    }
                }
            }
    
            private static bool RemoteCertificateValidate(object sender, X509Certificate cert, X509Chain chain, SslPolicyErrors error)
            {
                System.Console.WriteLine("Warning, trust any certificate");
                //为了通过证书验证,总是返回true
                return true;
            }
    
        }

     有关XML转换,大家请参考上一篇文章: https://www.cnblogs.com/shuai7boy/p/10963734.html

  • 相关阅读:
    数组地址,数组首地址与数组首元素地址的区别
    memset,memcpy与strcpy
    OJ之大数与高精度题必备知识
    OJ之星期几算法(泽勒一致性)
    二分查找及其优化
    爱上vim之快捷键使用技巧与个性化配置
    shell之终极shell——zsh
    memset的一些坑
    OO终章
    hOmewOrk 第三单元 总结
  • 原文地址:https://www.cnblogs.com/shuai7boy/p/10965012.html
Copyright © 2011-2022 走看看