• 腾讯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
  • 相关阅读:
    python之闭包,装饰器
    python之函数名称空间,作用域,嵌套函数
    python之函数基础
    Python之文件操作
    Linux之系统优化配置
    VMware安装CentOS操作系统详细步骤
    拷贝、浅拷贝、深拷贝解答
    python之字符串,列表,字典,元组,集合内置方法总结
    东方超环(EAST)世界纪录
    Vue通信、传值的多种方式,详解(都是干货)
  • 原文地址:https://www.cnblogs.com/heheblog/p/net_qqgtk.html
Copyright © 2020-2023  润新知