https://passport.baidu.com/v2/api/?getapi&tpl=zongheng&apiver=v3&tt=1441287460704&class=login&gid=26D2A76-9D6C-4EBE-8687-25C6591194B2&logintype=basicLogin&callback=bd__cbs__v9tdyy获取参数token
3、用token访问该地址,获取raskeyhttps://passport.baidu.com/v2/getpublickey?token=5d0fc87cb0de39982c06795d3356e239&tpl=zongheng&apiver=v3&tt=1441311621601&gid=59723A6-1178-4614-B7AF-779917BC4453&callback=bd__cbs__gaci47和publickey,注意将地址中的值替换。
Newtonsoft.Json.Net35.dll(Josn数据解析)
BouncyCastle.Crypto.dll
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.OpenSsl;
using Org.BouncyCastle.Security;
namespace 纵横小说自动评论
{
/// <summary>
/// rsakey密码加密帮助
/// </summary>
public class RsaHelper
{
public static string PemToxml(string pem) {
if (pem.StartsWith("-----BEGIN RSA PRIVATE KEY-----") || pem.StartsWith("-----BEGIN PRIVATE KEY-----"))
{
return GetXmlRsaKey(pem, obj =>
{
if ((obj as RsaPrivateCrtKeyParameters) != null)
return DotNetUtilities.ToRSA((RsaPrivateCrtKeyParameters)obj);
var keyPair = (AsymmetricCipherKeyPair)obj; return DotNetUtilities.ToRSA((RsaPrivateCrtKeyParameters)keyPair.Private);
}, rsa => rsa.ToXmlString(true));
}
if (pem.StartsWith("-----BEGIN PUBLIC KEY-----"))
{
return GetXmlRsaKey(pem, obj =>
{
var publicKey = (RsaKeyParameters)obj;
return DotNetUtilities.ToRSA(publicKey);
}, rsa => rsa.ToXmlString(false));
}
throw new InvalidKeyException("Unsupported PEM format...");
}
private static string GetXmlRsaKey(string pem, Func<object, RSA> getRsa, Func<RSA, string> getKey)
{
using (var ms = new MemoryStream())
using (var sw = new StreamWriter(ms))
using (var sr = new StreamReader(ms))
{
sw.Write(pem);
sw.Flush();
ms.Position = 0;
var pr = new PemReader(sr);
object keyPair = pr.ReadObject();
using (RSA rsa = getRsa(keyPair))
{
var xml = getKey(rsa);
return xml;
}
}
}
/// <summary>
/// RSA加密
/// </summary>
/// <param name="publickey"></param>
/// <param name="content"></param>
/// <returns></returns>
public static string RSAEncrypt(string publickey, string content)
{
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
byte[] cipherbytes;
rsa.FromXmlString(publickey);
cipherbytes = rsa.Encrypt(Encoding.UTF8.GetBytes(content), false);
return Convert.ToBase64String(cipherbytes);
}
/// <summary>
/// RSA解密
/// </summary>
/// <param name="privatekey"></param>
/// <param name="content"></param>
/// <returns></returns>
public static string RSADecrypt(string privatekey, string content)
{
privatekey =
@"<RSAKeyValue><Modulus>5m9m14XH3oqLJ8bNGw9e4rGpXpcktv9MSkHSVFVMjHbfv+SJ5v0ubQQxa5YjLN4vc49z7SVju8s0X4gZ6AzZTn06jzWOgyPRV54Q4I0DCYadWW4Ze3e+BOtwgVU1Og3qHKn8vygoj40J6U85Z/PTJu3hN1m75Zr195ju7g9v4Hk=</Modulus><Exponent>AQAB</Exponent><P>/hf2dnK7rNfl3lbqghWcpFdu778hUpIEBixCDL5WiBtpkZdpSw90aERmHJYaW2RGvGRi6zSftLh00KHsPcNUMw==</P><Q>6Cn/jOLrPapDTEp1Fkq+uz++1Do0eeX7HYqi9rY29CqShzCeI7LEYOoSwYuAJ3xA/DuCdQENPSoJ9KFbO4Wsow==</Q><DP>ga1rHIJro8e/yhxjrKYo/nqc5ICQGhrpMNlPkD9n3CjZVPOISkWF7FzUHEzDANeJfkZhcZa21z24aG3rKo5Qnw==</DP><DQ>MNGsCB8rYlMsRZ2ek2pyQwO7h/sZT8y5ilO9wu08Dwnot/7UMiOEQfDWstY3w5XQQHnvC9WFyCfP4h4QBissyw==</DQ><InverseQ>EG02S7SADhH1EVT9DD0Z62Y0uY7gIYvxX/uq+IzKSCwB8M2G7Qv9xgZQaQlLpCaeKbux3Y59hHM+KpamGL19Kg==</InverseQ><D>vmaYHEbPAgOJvaEXQl+t8DQKFT1fudEysTy31LTyXjGu6XiltXXHUuZaa2IPyHgBz0Nd7znwsW/S44iql0Fen1kzKioEL3svANui63O3o5xdDeExVM6zOf1wUUh/oldovPweChyoAdMtUzgvCbJk1sYDJf++Nr0FeNW1RB1XG30=</D></RSAKeyValue>"; RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
byte[] cipherbytes;
rsa.FromXmlString(privatekey);
cipherbytes = rsa.Decrypt(Convert.FromBase64String(content), false);
return Encoding.UTF8.GetString(cipherbytes);
}
}
}好了,用这个类将密码加密后就Post所有数据吧,下面是具体实现代码
//获取token private Regex _regex = new Regex(@"/{.*/}", RegexOptions.IgnoreCase); /// <summary> /// 根据BAIDUID获取token /// </summary> public string GetToken() { HttpCookie cookie = new HttpCookie("BDRCVFR[gltLrB7qNCt]"); cookie.Value = "mk3SLVN4HKm"; cookie.Expires = DateTime.Now.AddDays(1); string url_getToken = string.Format("https://passport.baidu.com/v2/api/?getapi&tpl=zongheng&apiver=v3&tt={0}&class=login&gid=0743012-DD22-4632-B84E-B054B933DDA0&logintype=basicLogin&callback=bd__cbs__xjugf7", Utility.GetTimeStamp()); string token_get = helper.GetPageResponse_Get(url_getToken, Utility.UrlDecode("http://passport.zongheng.com/?location=http%3A%2F%2Fwww.zongheng.com%2F"), "*/*"); if (_regex.IsMatch(token_get)) token_get = _regex.Match(token_get).Value; var resultToken = JsonConvert.DeserializeObject<ResultToken>(token_get); if (string.IsNullOrEmpty(resultToken.Data.Token)) { ShowLog("Parameter-:获取token=" + resultToken.Data.Token + "失败/n"); return null; } return resultToken.Data.Token; } //获取raskey public string GetRasKey(string token) { string url_RasKey = string.Format("https://passport.baidu.com/v2/getpublickey?token={0}&tpl=zongheng&apiver=v3&tt={1}&gid=399423B-A0F5-42A0-B07F-CC5290F8F95D&callback=bd__cbs__xjugf7", token, Utility.GetTimeStamp()); string publicKey = helper.GetPageResponse_Get(url_RasKey, Utility.UrlDecode("http://passport.zongheng.com/?location=http%3A%2F%2Fwww.zongheng.com%2F"), "*/*"); //获取raskey if (_regex.IsMatch(publicKey)) publicKey = _regex.Match(publicKey).Value; var result_publicKey = JsonConvert.DeserializeObject<PublicRsaKey>(publicKey); var rsakey = result_publicKey.Key; //将密码加密 var pemToXml = RsaHelper.PemToXml(result_publicKey.Pubkey); pwd = RsaHelper.RSAEncrypt(pemToXml, pwd); //日志 if (!string.IsNullOrEmpty(rsakey)) return rsakey; ShowLog("Parameter-:获取publickey=" + result_publicKey.Pubkey + "失败!"); ShowLog("Parameter-:获取raskey=" + rsakey + "失败!"); return null; }/// <summary> /// 登录实现 /// </summary> public LoginStatus Login(string verifycode, string codestring, string token, string raskey, int userIndex) { string loginresult = ""; string url_Post = "https://passport.baidu.com/v2/api/?login"; long tt= Utility.GetTimeStamp(); string postData = string.Format("verifycode={0}&username={1}&u=http://zongheng.baidu.com/sendbduss.do?source=0&location=http%3A%2F%2Fwww.zongheng.com%2F&_t={2}&tt={3}&tpl=zongheng&token={4}&staticpage=http://passport.zongheng.com/v3Jump.html&safeflg=0&rsakey={5}&quick_user=0&ppui_logintime=20266&passWord={6}&mem_pass=on&logLoginType=pc_loginBasic&logintype=basicLogin&loginmerge=true&isPhone=false&gid=0743012-DD22-4632-B84E-B054B933DDA0&detect=1&crypttype=12&codestring={7}&charset=utf-8&callback=parent.bd__pcbs__ok0875&apiver=v3", verifycode, HttpUtility.UrlEncode(userName), tt,tt, HttpUtility.UrlEncode(token), HttpUtility.UrlEncode(raskey), HttpUtility.UrlEncode(pwd), codestring); loginresult =helper.GetPageResponse_Post(url_Post, postData, Utility.UrlDecode("http://passport.zongheng.com/?location=http%3A%2F%2Fwww.zongheng.com%2F")); //分析loginresult string err = @"(?<=err_no=)[^&]+?(?=&)"; Match regx = Regex.Match(loginresult, err); string codestr = "(?<=codeString=)[^&]+?(?=&)"; string codetype = "(?<=vcodetype=)[^&]+?(?=&)"; Match regx2 = Regex.Match(loginresult, codestr); Match regx3 = Regex.Match(loginresult, codetype); string errno = regx.Value; _codestring = regx2.Value; vcodeType= regx3.Value; //根据返回的错误号提示信息,并且请求验证码 switch (errno) { case "257": return LoginStatus.codeGet; case "7": ShowLog("账号或密码错误..."); ChangeStauts("失败", userIndex); return LoginStatus.loginFail;; case "0": ShowLog("登录成功..."); ChangeStauts("成功", userIndex); break; default: ShowLog("未知错误..."); ChangeStauts("错误", userIndex); return LoginStatus.Error;; } return LoginStatus.loginSucceed; }今天就到这里,明天说说验证码的问题,不过很大部分情况下,除非你的密码几次输入错误或者异地登录,不然一般不会让你输入验证码,如有问题欢迎评论交流