最近用C#寫qq活動輔助類程式,碰到了會員簽到的gtk演算法不一樣,後來網上找了看,發現有php版的(https://www.oschina.net/code/snippet_1378052_48831) 後來參考了php版的查php相關的資料用C#寫了一個: /// <summary> /// 計算 ...
最近用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