VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > Python基础教程 >
  • C#教程之腾讯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;
        }
复制代码

另附上解析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();
        }
复制代码

 


相关教程