zoukankan      html  css  js  c++  java
  • C#根据URL生成签名

    代码:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Security.Cryptography;
    using System.Text;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    namespace ADT.TuDou.FineManage
    {
        public partial class Atest : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                string url = Request.Url.ToString();
                string query = Request.Url.Query;
                if (!string.IsNullOrEmpty(query))
                {
                    Dictionary<string, string> dic = new Dictionary<string, string>();
    
                    query = query.Substring(1, query.Length - 1);
                    string[] kv = query.Split(new char[] { '&' }, StringSplitOptions.RemoveEmptyEntries);
                    foreach (string item in kv)
                    {
                        string[] kv2 = item.Split(new char[] { '=' }, StringSplitOptions.RemoveEmptyEntries);
                        dic.Add(kv2[0], kv2[1]);
                    }
    
                    IDictionary<string, string> sortedParams = new SortedDictionary<string, string>(dic); //dic.OrderBy(c => c.Key).ToDictionary(k => k.Key, v => v.Value);
    
                    IEnumerator<KeyValuePair<string, string>> iterator = sortedParams.GetEnumerator();
    
                    // 遍历排序后的字典,将所有参数按"key=value"格式拼接在一起
                    StringBuilder basestring = new StringBuilder();
                    while (iterator.MoveNext())
                    {
                        string key = iterator.Current.Key;
                        string value = iterator.Current.Value;
                        if (!string.IsNullOrEmpty(key) && !string.IsNullOrEmpty(value))
                        {
                            basestring.Append(key).Append("=").Append(value);
                        }
                    }
    
                    // 使用MD5对待签名串求签
                    MD5 md5 = MD5.Create();
                    byte[] bytes = md5.ComputeHash(Encoding.UTF8.GetBytes(basestring.ToString()));
    
                    // 将MD5输出的二进制结果转换为小写的十六进制
                    StringBuilder result = new StringBuilder();
                    for (int i = 0; i < bytes.Length; i++)
                    {
                        string hex = bytes[i].ToString("x");
                        if (hex.Length == 1)
                        {
                            result.Append("0");
                        }
                        result.Append(hex);
                    }
                    Response.Write(result.ToString());
                }
    
    
            }
        }
    }
    

      

  • 相关阅读:
    第63天python学习异常
    第62天python 学习TCP三次握手四次挥手详解
    文件操作
    内置函数
    函数递归
    函数补充
    函数
    购物车程序作业
    集合内置函数
    字典三级菜单
  • 原文地址:https://www.cnblogs.com/xsj1989/p/5650193.html
Copyright © 2011-2022 走看看