zoukankan      html  css  js  c++  java
  • 新浪微博登录密码加密函数 wsse加密算法说明

    1. // 新浪微博登录密码加密函数  
    2. // password   密码明文  
    3. // servertime 提交的参数之一  
    4. // nonce      提交的参数之一  
    5. // encode_password 输出的加密后的16进制字符串,40个字符  
    6. // 返回 encode_password 的长度, 失败则返回0  
    7.   
    8. PASSENCODE_API int SinaSha1Encode(char *password, char *servertime, char *nonce, char *encode_password)  
    9. {  
    10.     if (encode_password)  
    11.     {  
    12.         encode_password[0]=NULL;  
    13.   
    14.         //定义要sha1的字符串  
    15.         char pTemp[400]={0};  
    16.         //定义返回的sha1值  
    17.         char szHash[41] ={0};  
    18.   
    19.         strcpy(pTemp,password);  
    20.         SHA1((unsigned char*)pTemp, szHash, 41);  
    21.   
    22.         strcpy(pTemp,szHash);  
    23.         SHA1((unsigned char*)pTemp, szHash, 41);  
    24.   
    25.         strcpy(pTemp,szHash);  
    26.         if (servertime) strcat(pTemp,servertime);  
    27.         if (nonce) strcat(pTemp,nonce);  
    28.   
    29.         SHA1((unsigned char*)pTemp, szHash, 41);  
    30.   
    31.         strcpy(encode_password,szHash);  
    32.   
    33.         return strlen(encode_password);  
    34.     }  
    35.   
    36.     return 0;  
    37. }  
    38.   
    39. //登陆伪代码  
    40. //***********************************************  
    41. ByteBuffer bytes;  
    42.   
    43. string url;  
    44.   
    45. string username="zhanghao@sina.com";  
    46. string password="mima";  
    47. string encode_password;  
    48.   
    49. url="http://login.sina.com.cn/sso/prelogin.php?&entry=sso&username=";  
    50. url+=username;  
    51. url+="&callback=parent.sinaSSOController.loginCallBack";  
    52.   
    53. BOOL bHtmlText;  
    54. BOOL bUTF8;  
    55.   
    56. string retcode;  
    57. string servertime;  
    58. string nonce;  
    59.   
    60. if (GetUrlData(url.c_str(),bytes,NULL,0,bHtmlText,bUTF8) && bytes.getLength() && bHtmlText)  
    61. {  
    62.     string str=bytes.c_str();  
    63.       
    64.     string::size_type pos(0),posend(0);  
    65.   
    66.     if( (pos=str.find("{",posend))!=string::npos && (posend=str.find("}", ++pos))!=string::npos )  
    67.     {  
    68.         string result=str.substr(pos,posend-pos);   
    69.   
    70.         replace_all(result,"\"","");  
    71.   
    72.         TRACE(result.c_str());  
    73.   
    74.         vector<string> vars;  
    75.         Split(result,vars,",");  
    76.         if (vars.size())  
    77.         {  
    78.   
    79.             for (int i=0;i<vars.size();i++)  
    80.             {  
    81.                 vector<string> keys;  
    82.                 Split(vars[i],keys,":");  
    83.   
    84.                 if (keys.size()==2)  
    85.                 {  
    86.   
    87.                     if (keys[0]=="retcode")  
    88.                     {  
    89.                         retcode=keys[1];  
    90.                     }else  
    91.                     if (keys[0]=="servertime")  
    92.                     {  
    93.                         servertime=keys[1];  
    94.                     }else  
    95.                     if (keys[0]=="nonce")  
    96.                     {  
    97.                         nonce=keys[1];  
    98.                     }else{  
    99.                         TRACE("key:%s val:%s",keys[0].c_str(),keys[1].c_str());  
    100.                     }  
    101.   
    102.                 }  
    103.             }  
    104.   
    105.         }  
    106.           
    107.     }  
    108. }  
    109.   
    110. if (atoi(retcode.c_str())==0)  
    111. {  
    112.     EncodePasword(password,servertime,nonce,encode_password);  
    113.   
    114.     TRACE("servertime:%s",servertime.c_str());  
    115.     TRACE("nonce:%s",nonce.c_str());  
    116.     TRACE("encode_password:%s",encode_password.c_str());  
    117.   
    118.   
    119.     url="http://login.sina.com.cn/sso/login.php?client=ssologin.js(v1.3.12)";  
    120.   
    121.     string post;  
    122.   
    123. /*  
    124. service=miniblog&client=ssologin.js%28v1.3.12%29&entry=miniblog&encoding=utf-  
    125. 8&gateway=1&savestate=7&from=&useticket=1&username=diyiwl@sina.com&servertime=1306503150&nonce=IN4GY9&pwencode=wsse&password=74dd6f5f82e027412b6da345909f0df450b5f410&url=http%  
    126. 3A%2F%2Fweibo.com%2Fajaxlogin.php%3Fframelogin%3D1%26callback%3Dparent.sinaSSOController.feedBackUrlCallBack&returntype=META&ssosimplelogin=1  
    127. */  
    128.     post="service=miniblog&client=ssologin.js%28v1.3.12%29&entry=miniblog&encoding=GB2312&gateway=1&savestate=7&from=&useticket=1&username=";  
    129.     post+=username;  
    130.     post+="&servertime=";  
    131.     post+=servertime;  
    132.     post+="&nonce=";  
    133.     post+=nonce;  
    134.     post+="&pwencode=wsse&password=";  
    135.     post+=encode_password;  
    136.     post+="&url=http%3A%2F%2Fweibo.com%2Fajaxlogin.php%3Fframelogin%3D1%26callback%3Dparent.sinaSSOController.feedBackUrlCallBack&returntype=META&ssosimplelogin=1";  
    137.   
    138.     TRACE(url.c_str());  
    139.     TRACE(post.c_str());  
    140.   
    141.     if (GetUrlData(url.c_str(),bytes,post.c_str(),post.length(),bHtmlText,bUTF8) && bytes.getLength() && bHtmlText)  
    142.     {  
    143.         TRACE(bytes.c_str());  
    144.     }  
    145.   
    146. }  
    147. //********************************************** 

    新浪网已经废弃了原来的那种简单的登录方法,可能是因为新浪微博。现在新浪使用的登录方法复杂程度不亚于腾讯。下面我们简单介绍新浪微博的登录 过程。     在登录之前我们需要先从新浪服务器获取两个变量:servertime,nonce。其中servertime中新浪的服务器时间,nonce是一 个随机生成的字符串。获取的需要通过这样一个网络接口:http://login.sina.com.cn/sso/prelogin.php?entry=miniblog&callback=sinaSSOController.preloginCallBack&user="+username+"&client=ssologin.js(v1.3.12)
    其中username是你的用户名。
    获取到我们需要的内容后,接下来就是加密的过程了。加密使用的是通用的SHA1加密算法
    • 把密码进行一次SHA1加密,结果再进行一次SHA1加密
    • 把经过两次SHA1加密的结果附上servertime,nonce再进行一次SHA1加密
    • 加密的结果作为登录密码
    登录地址没有变,仍使用:http://login.sina.com.cn/sso/login.php?client=ssologin.js(v1.3.12)

    但参数要复杂一点了:
    service:选择服务,我们这里使用的新浪微博:miniblog
    client:ssologin.js%28v1.3.12%29
    entry:miniblog
    encoding:utf-8
    gateway:1
    savestate:0
    useticket:1,这里使用到了登录票据。
    username:你的用户名
    servertime:上步得到的服务器时间
    nonce:上步得到的随机字符串
    pwencode:wsse(没整明白是个什么东西,不过从来不变)
    password:加密后的密码
    url:一个回调地址
    returntype:META(使用中没有发生变化)
    ssosimplelogin:1
    example:
    8&gateway:1&savestate:0&useticket:1&username: 用户 名&servertime:1308647606&nonce:L8QJHE&pwencode:wsse&password:f435c677294851d13794359145e6f96826c1
    c3d2&url:http://www.baidu.com&returntype:META&ssosimplelogin:1

  • 相关阅读:
    eclipse插件egit安装使用
    github 项目版本控制
    div box container随主体内容自动扩展适应的实现
    持续构建与每日集成
    Xshell连接Linux下Oracle无法回退的解决办法
    Java Data JPA +hibernate 保存或者是查询遇到的坑
    C#控件DropDownList下拉列表默认打开
    window.open居中显示
    CSV文件转JSON
    Vue自定义事件,$on(eventName) 监听事件,$emit(eventName) 触发事件
  • 原文地址:https://www.cnblogs.com/goody9807/p/2094400.html
Copyright © 2011-2022 走看看