首页 > 学院 > 开发设计 > 正文

C#模拟百度登录

2019-11-17 02:19:39
字体:
来源:转载
供稿:网友

C#模拟百度登录

目录:

1、fiddler解析百度登录地址

2、处理传入参数

1、fiddler解析百度登录地址

因工作需要,所以研究了下百度的登陆。首先打开https://passport.baidu.com/v2/?login,我们用fiddler很快就能找到百度的登录入口https://passport.baidu.com/v2/api/?login .如下图:

在登录入口https://passport.baidu.com/v2/api/?login 之前,百度先会去获取publickey和token。token是服务器和客户端关联的唯一id。publickey是一个rsa(非对称加密)用来加密输入的密码的。所以要模拟登录。必须要拿到这两个参数。

2、处理传入参数

通过fildder我们很快拿到了获取token和publickey的地址。

token(https://passport.baidu.com/v2/api/?getapi&tpl=pp&apiver=v3&tt=1433836782422&class=login&logintype=basicLogin&callback=bd__cbs__7fwpot)

publickey(https://passport.baidu.com/v2/api/?loginhistory&token=414cf195652963982d479ecf0cee814b&tpl=pp&apiver=v3&tt=1433836782658&callback=bd__cbs__57q1jk)

可以看出先拿到token,然后用这个token再去拿publickey。如下图:

这两个都拿到了。

注意:我们看到返回的pubkey是以-----BEGIN PUBLIC KEY----- 开始 和-----END PUBLIC KEY----- 结束的。这是pem格式。我们要转换成xml格式的。因为.net平台自带的RSACryptoServicePRovider解析的是xml字符串。所以有了下面的帮助类:

需要引用:BouncyCastle.Crypto.dll

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)        {            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();            byte[] cipherbytes;            rsa.FromXmlString(privatekey);            cipherbytes = rsa.Decrypt(Convert.FromBase64String(content), false);            return Encoding.UTF8.GetString(cipherbytes);        }    }

下面就可以用HttpWebRequest开始模拟登录了。当cookies中包含BAIDUID 则说明登录成功。还有就是访问https://passport.baidu.com/v2/api/?login,返回的字符串中 err_no=0 表示登录成功了。

附件:

demo


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表