C# 百度最新模拟登录源码 RSA加密

来源:互联网 发布:湖南大学网络教育 编辑:程序博客网 时间:2024/06/07 23:47

因为要做百度网盘的相关操作,,需要登录百度,网上搜了半天都是以前百度没用RSA加密时候的源码,,只能自己动手,说实话挺累的,花了一晚上时间终于可以了!
还有个新浪微博登录的,看了下网上能搜到,就不发了。希望对大家有用!
感谢苏菲的httphelper类 省了不少时间!

//主要代码 //获取tokenpublic bool GetToken(){    var url = "https://passport.baidu.com/v2/api/?getapi&";     var nvc = new NameValueCollection    {        {"apiver", "v3"},        {"callback", "customName"},        {"class", ""},        {"logintype", "basicLogin"},        {"tpl", "pp"},        {"tt", DateTime.Now.Ticks.ToString()},    };    var httpResult = new HttpHelper().GetHtml(        new HttpItem()        {            URL = url + HttpHelper.DataToString(nvc),            Method = "GET",            CookieContainer = cookies,            ResultCookieType = ResultCookieType.CookieContainer        });    if (httpResult.StatusCode.Equals(HttpStatusCode.OK))    {        var mc = new Regex(@"\((?<jsonStr>.*)\)").Match(httpResult.Html.Replace("\n", string.Empty));        if (mc.Success)        {            dynamic obj = JsonConvert.DeserializeObject<JObject>(mc.Groups["jsonStr"].Value);            var no = obj.errInfo.no.Value;            if (no.Equals("0"))            {                token = obj.data.token.Value;                return true;            }        }     }    return false;}//获取keypublic bool GetPubksyAndRsakey(){    var url = "https://passport.baidu.com/v2/getpublickey?";    var nvc = new NameValueCollection    {        {"apiver", "v3"},        {"callback", "customName"},        {"token", token},        {"tpl", "pp"},        {"tt", DateTime.Now.Ticks.ToString()},    };    var httpResult = new HttpHelper().GetHtml(        new HttpItem()        {            URL = url + HttpHelper.DataToString(nvc),            Method = "GET",            CookieContainer = cookies,            ResultCookieType = ResultCookieType.CookieContainer        });    if (httpResult.StatusCode.Equals(HttpStatusCode.OK))    {        var mc = new Regex(@"\((?<jsonStr>.*)\)").Match(httpResult.Html);        if (mc.Success)        {            dynamic obj = JsonConvert.DeserializeObject<JObject>(mc.Groups["jsonStr"].Value);            var no = obj.errno.Value;            if (no.Equals("0"))            {                pubksy = obj.pubkey.Value;                rsakey = obj.key.Value;                return true;            }        }        return true;    }    return false;}//获取验证码public Image GetValCode(){    var url = "https://passport.baidu.com/v2/?reggetcodestr&";    var nvc = new NameValueCollection    {        {"apiver", "v3"},        {"callback", "customName"},        {"fr", "login"},        {"token", token},        {"tpl", "pp"},        {"tt", DateTime.Now.Ticks.ToString()},    };    var httpResult = new HttpHelper().GetHtml(        new HttpItem()        {            URL = url + HttpHelper.DataToString(nvc),            Method = "GET",            CookieContainer = cookies,            ResultCookieType = ResultCookieType.CookieContainer        });    if (httpResult.StatusCode.Equals(HttpStatusCode.OK))    {        var mc = new Regex(@"\((?<jsonStr>.*)\)").Match(httpResult.Html.Replace("\n", string.Empty));        if (mc.Success)        {            dynamic obj = JsonConvert.DeserializeObject<JObject>(mc.Groups["jsonStr"].Value);            var no = obj.errInfo.no.Value;            if (no.Equals("0"))            {                verifyStr = obj.data.verifyStr.Value;                var verifySign = obj.data.verifySign.Value;                url = "https://passport.baidu.com/cgi-bin/genimage?";                httpResult = new HttpHelper().GetHtml(                    new HttpItem()                    {                        URL = url + verifyStr,                        Method = "GET",                        CookieContainer = cookies,                        ResultCookieType = ResultCookieType.CookieContainer,                        ResultType = ResultType.Byte                    });                if (httpResult.StatusCode.Equals(HttpStatusCode.OK))                {                    return Image.FromStream(new MemoryStream(httpResult.ResultByte));                }            }         }    }    return null;}//检查兼验证码public bool CheckValCode(string code){    var url = "https://passport.baidu.com/v2/?checkvcode&";    var nvc = new NameValueCollection    {        {"apiver", "v3"},        {"token", token},        {"tpl", "pp"},        {"fr", "login"},        {"token", token},        {"tpl", "pp"},        {"tt", DateTime.Now.Ticks.ToString()},        {"verifycode", code},        {"codestring", verifyStr},        {"callback", "customName"},    };    var httpResult = new HttpHelper().GetHtml(        new HttpItem()        {            URL = url + HttpHelper.DataToString(nvc),            Method = "GET",            CookieContainer = cookies,            ResultCookieType = ResultCookieType.CookieContainer        });    if (httpResult.StatusCode.Equals(HttpStatusCode.OK))    {        var mc = new Regex(@"\((?<jsonStr>.*)\)").Match(httpResult.Html.Replace("\n", string.Empty));        if (mc.Success)        {            dynamic obj = JsonConvert.DeserializeObject<JObject>(mc.Groups["jsonStr"].Value);            var no = obj.errInfo.no.Value;            if (no.Equals("0"))            {                valcode = code;                return true;            }        }    }    return false;}//登录public bool Login(string userName, string pswd){    pswd = GetPswd(pswd, pubksy.Replace("\n", "\\n"));    var url = "https://passport.baidu.com/v2/api/?login";    var nvc = new NameValueCollection    {        {"apiver", "v3"},        {"staticpage", "https://passport.baidu.com/static/passpc-account/html/v3Jump.html"},        {"charset", "UTF-8"},        {"token", token},        {"tpl", "pp"},        {"tt", DateTime.Now.Ticks.ToString()},        {"codestring", verifyStr},        {"safeflg", "0"},        {"u", "https://passport.baidu.com/"},        {"isPhone", "false"},        {"detect", "1"},        {"quick_user", "0"},        {"logintype", "basicLogin"},        {"logLoginType", "pc_loginBasic"},        {"loginmerge", "true"},        {"username", userName},        {"password", pswd},        {"verifycode", valcode},        {"mem_pass", "on"},        {"rsakey", rsakey},        {"crypttype", "12"},        {"ppui_logintime", "111111"},        {"callback", "customName"},    };    var httpResult = new HttpHelper().GetHtml(        new HttpItem        {            URL = url,            Method = "POST",            ContentType = "application/x-www-form-urlencoded",            Postdata = HttpHelper.DataToString(nvc),            PostDataType = PostDataType.String,            CookieContainer = cookies,            ResultCookieType = ResultCookieType.CookieContainer        });    if (httpResult.StatusCode.Equals(HttpStatusCode.OK))    {        Debug.WriteLine(httpResult.Html);        Debug.WriteLine(HttpHelper.GetAllCookiesToString(cookies));        return HttpHelper.GetAllCookies(cookies).FirstOrDefault(c => c.Name.Equals("STOKEN")) != null;    }    return false;}

下载源码

0 0
原创粉丝点击