zoukankan      html  css  js  c++  java
  • 腾讯QQ会员中心g_tk32算法【C#版】

    最近用C#写qq活动辅助类程序,碰到了会员签到的gtk算法不一样,后来网上找了看,发现有php版的(https://www.oschina.net/code/snippet_1378052_48831)

    后来参考了php版的查php相关的资料用C#写了一个:

     /// <summary>
            /// 计算gtk32值
            /// </summary>
            /// <param name="skey"></param>
            /// <returns></returns>
            public static string GetGTK32(string skey)
            {
                var hash = 5381;
                var md5Key = "tencentQQVIP123443safde&!%^%1282";
                var start = hash << 5;
                var result = string.Empty;
                for (int i = 0; i < skey.Length; i++)
                {
                    var ascode = CharToASCII(skey.Substring(i, 1));
                    result += (hash << 5) + ascode;
                    hash = ascode;
                }
                var str = start + (result + md5Key);
                return GetMD5(str);
            }
    
            /// <summary>
            /// MD5加密
            /// </summary>
            /// <param name="text"></param>
            /// <returns></returns>
            public static string GetMD5(string text)
            {
                StringBuilder sb = new StringBuilder();
                using (MD5 md5 = MD5.Create())
                {
                    byte[] md5Byte = md5.ComputeHash(Encoding.Default.GetBytes(text));
                    for (int i = 0; i < md5Byte.Length; i++)
                    {
                        sb.Append(md5Byte[i].ToString("x2"));
                    }
                }
                return sb.ToString();
            }
    
            /// <summary>
            /// /*字符转化为ASCII*/
            /// </summary>
            /// <param name="character"></param>
            /// <returns></returns>
            static int CharToASCII(string character)
            {
                ASCIIEncoding asciiEncoding = new ASCIIEncoding();
                int intAsciiCode = asciiEncoding.GetBytes(character)[0];
                return intAsciiCode;
            }
    View Code

    另附上解析cookie中的skey和p_skey方法和gtk算法:

            /// <summary>
            /// 解析cookie,取到Skey
            /// </summary>
            /// <param name="cookies">腾讯QQ cookie</param>
            /// <returns></returns>
            public static string GetSkey(string cookies)
            {
                #region 字符串分割解析
                //var keyStr = "skey=";
                //var index = cookies.IndexOf(keyStr) + keyStr.Length;
                //var skey = cookies.Remove(0, index);
                //if (skey.Contains(";") && skey.Length > 10)
                //{
                //    var laindex = cookies.IndexOf(";");
                //    skey = skey.Remove(10);
                //} 
                #endregion
                var skey = Regex.Match(cookies, "skey=(.){10}?").Value.Remove(0, 5);
                if (skey.Length > 10)
                {
                    skey.Remove(10);
                }
                return skey;
            }
    
            /// <summary>
            /// 解析cookie,取到p_skey
            /// </summary>
            /// <param name="cookies">腾讯QQ cookie</param>
            /// <returns></returns>
            public static string Getp_skey(string cookies)
            {
                return Regex.Match(cookies, "p_skey=(.)+?_").Value.Remove(0, 7);
            }
    
            /// <summary>
            /// 算出g_tk
            /// </summary>
            /// <param name="sKey">cookie中的sKey值</param>
            /// <returns></returns>
            public static string GetGTK(string sKey)
            {
                var hash = 5381;
                for (int i = 0, len = sKey.Length; i < len; ++i)
                {
                    hash += (hash << 5) + sKey[i];
                }
                return (hash & 0x7fffffff).ToString();
            }
    View Code
  • 相关阅读:
    <script>元素
    女朋友问什么是动态规划,应该怎么回答?
    从输入URL到页面展示,这中间都发生了什么?
    TypeScript之父:JS不是竞争对手,曾在惧怕开源的微软文化中艰难求生
    Flash 终将谢幕:微软将于年底停止对 Flash 的支持
    尤雨溪:TypeScript不会取代JavaScript
    JVM参数设置、分析(转发)
    -XX:PermSize -XX:MaxPermSize 永久区参数设置
    堆的分配参数
    -Xmx 和 –Xms 设置最大堆和最小堆
  • 原文地址:https://www.cnblogs.com/heheblog/p/net_qqgtk.html
Copyright © 2011-2022 走看看