腾讯webqq的登录时,只需向服务器提交1次密码和验证码,之后服务器会返回两个会话id,就像令牌一样,每次ajax向服务器拉取信息时,都会携带这两个令牌,直至用户注销登录或意外退出。
上次曾把密码与验证码混合MD5加密的js脚本提取出来,这里再贴一下。
md5.js
var chrsz=8; var mode=32; var hexcase=1; function preprocess(A){ var B=""; B+=A.verifycode.value; B=B.toUpperCase(); A.p.value=md5(md5_3(A.p.value)+B); return true; } function md5_3(B){ var A=new Array; A=core_md5(str2binl(B),B.length*chrsz); A=core_md5(A,16*chrsz); A=core_md5(A,16*chrsz); return binl2hex(A); } function md5(A){ return hex_md5(A); } function hex_md5(A){ return binl2hex(core_md5(str2binl(A),A.length*chrsz)); } function str_hmac_md5(A,B){ return binl2str(core_hmac_md5(A,B)); } function core_md5(K,F){ K[F>>5]|=128<<((F)%32); K[(((F+64)>>>9)<<4)+14]=F; var J=1732584193; var I=-271733879; var H=-1732584194; var G=271733878; for(var C=0;C<K.length;C+=16){ var E=J; var D=I; var B=H; var A=G; J=md5_ff(J,I,H,G,K[C+0],7,-680876936); G=md5_ff(G,J,I,H,K[C+1],12,-389564586); H=md5_ff(H,G,J,I,K[C+2],17,606105819); I=md5_ff(I,H,G,J,K[C+3],22,-1044525330); J=md5_ff(J,I,H,G,K[C+4],7,-176418897); G=md5_ff(G,J,I,H,K[C+5],12,1200080426); H=md5_ff(H,G,J,I,K[C+6],17,-1473231341); I=md5_ff(I,H,G,J,K[C+7],22,-45705983); J=md5_ff(J,I,H,G,K[C+8],7,1770035416); G=md5_ff(G,J,I,H,K[C+9],12,-1958414417); H=md5_ff(H,G,J,I,K[C+10],17,-42063); I=md5_ff(I,H,G,J,K[C+11],22,-1990404162); J=md5_ff(J,I,H,G,K[C+12],7,1804603682); G=md5_ff(G,J,I,H,K[C+13],12,-40341101); H=md5_ff(H,G,J,I,K[C+14],17,-1502002290); I=md5_ff(I,H,G,J,K[C+15],22,1236535329); J=md5_gg(J,I,H,G,K[C+1],5,-165796510); G=md5_gg(G,J,I,H,K[C+6],9,-1069501632); H=md5_gg(H,G,J,I,K[C+11],14,643717713); I=md5_gg(I,H,G,J,K[C+0],20,-373897302); J=md5_gg(J,I,H,G,K[C+5],5,-701558691); G=md5_gg(G,J,I,H,K[C+10],9,38016083); H=md5_gg(H,G,J,I,K[C+15],14,-660478335); I=md5_gg(I,H,G,J,K[C+4],20,-405537848); J=md5_gg(J,I,H,G,K[C+9],5,568446438); G=md5_gg(G,J,I,H,K[C+14],9,-1019803690); H=md5_gg(H,G,J,I,K[C+3],14,-187363961); I=md5_gg(I,H,G,J,K[C+8],20,1163531501); J=md5_gg(J,I,H,G,K[C+13],5,-1444681467); G=md5_gg(G,J,I,H,K[C+2],9,-51403784); H=md5_gg(H,G,J,I,K[C+7],14,1735328473); I=md5_gg(I,H,G,J,K[C+12],20,-1926607734); J=md5_hh(J,I,H,G,K[C+5],4,-378558); G=md5_hh(G,J,I,H,K[C+8],11,-2022574463); H=md5_hh(H,G,J,I,K[C+11],16,1839030562); I=md5_hh(I,H,G,J,K[C+14],23,-35309556); J=md5_hh(J,I,H,G,K[C+1],4,-1530992060); G=md5_hh(G,J,I,H,K[C+4],11,1272893353); H=md5_hh(H,G,J,I,K[C+7],16,-155497632); I=md5_hh(I,H,G,J,K[C+10],23,-1094730640); J=md5_hh(J,I,H,G,K[C+13],4,681279174); G=md5_hh(G,J,I,H,K[C+0],11,-358537222); H=md5_hh(H,G,J,I,K[C+3],16,-722521979); I=md5_hh(I,H,G,J,K[C+6],23,76029189); J=md5_hh(J,I,H,G,K[C+9],4,-640364487); G=md5_hh(G,J,I,H,K[C+12],11,-421815835); H=md5_hh(H,G,J,I,K[C+15],16,530742520); I=md5_hh(I,H,G,J,K[C+2],23,-995338651); J=md5_ii(J,I,H,G,K[C+0],6,-198630844); G=md5_ii(G,J,I,H,K[C+7],10,1126891415); H=md5_ii(H,G,J,I,K[C+14],15,-1416354905); I=md5_ii(I,H,G,J,K[C+5],21,-57434055); J=md5_ii(J,I,H,G,K[C+12],6,1700485571); G=md5_ii(G,J,I,H,K[C+3],10,-1894986606); H=md5_ii(H,G,J,I,K[C+10],15,-1051523); I=md5_ii(I,H,G,J,K[C+1],21,-2054922799); J=md5_ii(J,I,H,G,K[C+8],6,1873313359); G=md5_ii(G,J,I,H,K[C+15],10,-30611744); H=md5_ii(H,G,J,I,K[C+6],15,-1560198380); I=md5_ii(I,H,G,J,K[C+13],21,1309151649); J=md5_ii(J,I,H,G,K[C+4],6,-145523070); G=md5_ii(G,J,I,H,K[C+11],10,-1120210379); H=md5_ii(H,G,J,I,K[C+2],15,718787259); I=md5_ii(I,H,G,J,K[C+9],21,-343485551); J=safe_add(J,E); I=safe_add(I,D); H=safe_add(H,B); G=safe_add(G,A); } if(mode==16){ return Array(I,H); }else{ return Array(J,I,H,G); } } function md5_cmn(F,C,B,A,E,D){ return safe_add(bit_rol(safe_add(safe_add(C,F),safe_add(A,D)),E),B); } function md5_ff(C,B,G,F,A,E,D){ return md5_cmn((B&G)|((~B)&F),C,B,A,E,D); } function md5_gg(C,B,G,F,A,E,D){ return md5_cmn((B&F)|(G&(~F)),C,B,A,E,D); } function md5_hh(C,B,G,F,A,E,D){ return md5_cmn(B^G^F,C,B,A,E,D); } function md5_ii(C,B,G,F,A,E,D){ return md5_cmn(G^(B|(~F)),C,B,A,E,D); } function core_hmac_md5(C,F){ var E=str2binl(C); if(E.length>16){ E=core_md5(E,C.length*chrsz); } var A=Array(16),D=Array(16); for(var B=0;B<16;B++){ A[B]=E[B]^909522486; D[B]=E[B]^1549556828; } var G=core_md5(A.concat(str2binl(F)),512+F.length*chrsz); return core_md5(D.concat(G),512+128); } function safe_add(A,D){ var C=(A&65535)+(D&65535); var B=(A>>16)+(D>>16)+(C>>16); return(B<<16)|(C&65535); } function bit_rol(A,B){ return(A<<B)|(A>>>(32-B)); } function str2binl(D){ var C=Array(); var A=(1<<chrsz)-1; for(var B=0;B<D.length*chrsz;B+=chrsz){ C[B>>5]|=(D.charCodeAt(B/chrsz)&A)<<(B%32); } return C; } function binl2str(C){ var D=""; var A=(1<<chrsz)-1; for(var B=0;B<C.length*32;B+=chrsz){ D+=String.fromCharCode((C[B>>5]>>>(B%32))&A); } return D; } function binl2hex(C){ var B=hexcase?"0123456789ABCDEF":"0123456789abcdef"; var D=""; for(var A=0;A<C.length*4;A++){ D+=B.charAt((C[A>>2]>>((A%4)*8+4))&15)+B.charAt((C[A>>2]>>((A%4)*8))&15); } return D; }
看了jdk源码中md5算法的源文件,发现webqq的加密算法,多了一个3次md5的函数,于是自己写了个java的实现,下面是jdk的标准md5算法实现与webqq加密的实现
jdk 1.6 标准 MD5.java
public class MD5 { /* * A Java implementation of the RSA Data Security, Inc. MD5 Message Digest * Algorithm, as defined in RFC 1321. Based on the JavaScript implementation * of Paul Johnston Copyright (C) Paul Johnston 1999 - 2000. See * http://pajhome.org.uk/site/legal.html for details. Java Version by Thomas * Weber (Orange Interactive GmbH) */ /* * Convert a 32-bit number to a hex string with ls-byte first */ String hex_chr = "0123456789abcdef"; private String rhex(int num) { String str = ""; for (int j = 0; j <= 3; j++) str = str + hex_chr.charAt((num >> (j * 8 + 4)) & 0x0F) + hex_chr.charAt((num >> (j * 8)) & 0x0F); return str; } /* * Convert a string to a sequence of 16-word blocks, stored as an array. * Append padding bits and the length, as described in the MD5 standard. */ private int[] str2blks_MD5(String str) { int nblk = ((str.length() + 8) >> 6) + 1; int[] blks = new int[nblk * 16]; int i = 0; for (i = 0; i < nblk * 16; i++) { blks[i] = 0; } for (i = 0; i < str.length(); i++) { blks[i >> 2] |= str.charAt(i) << ((i % 4) * 8); } blks[i >> 2] |= 0x80 << ((i % 4) * 8); blks[nblk * 16 - 2] = str.length() * 8; return blks; } /* * Add integers, wrapping at 2^32 */ private int add(int x, int y) { return ((x & 0x7FFFFFFF) + (y & 0x7FFFFFFF)) ^ (x & 0x80000000) ^ (y & 0x80000000); } /* * Bitwise rotate a 32-bit number to the left */ private int rol(int num, int cnt) { return (num << cnt) | (num >>> (32 - cnt)); } /* * These functions implement the basic operation for each round of the * algorithm. */ private int cmn(int q, int a, int b, int x, int s, int t) { return add(rol(add(add(a, q), add(x, t)), s), b); } private int ff(int a, int b, int c, int d, int x, int s, int t) { return cmn((b & c) | ((~b) & d), a, b, x, s, t); } private int gg(int a, int b, int c, int d, int x, int s, int t) { return cmn((b & d) | (c & (~d)), a, b, x, s, t); } private int hh(int a, int b, int c, int d, int x, int s, int t) { return cmn(b ^ c ^ d, a, b, x, s, t); } private int ii(int a, int b, int c, int d, int x, int s, int t) { return cmn(c ^ (b | (~d)), a, b, x, s, t); } /* * Take a string and return the hex representation of its MD5. */ public String calcMD5(String str) { int[] x = str2blks_MD5(str); int a = 0x67452301; int b = 0xEFCDAB89; int c = 0x98BADCFE; int d = 0x10325476; for (int i = 0; i < x.length; i += 16) { int olda = a; int oldb = b; int oldc = c; int oldd = d; a = ff(a, b, c, d, x[i + 0], 7, 0xD76AA478); d = ff(d, a, b, c, x[i + 1], 12, 0xE8C7B756); c = ff(c, d, a, b, x[i + 2], 17, 0x242070DB); b = ff(b, c, d, a, x[i + 3], 22, 0xC1BDCEEE); a = ff(a, b, c, d, x[i + 4], 7, 0xF57C0FAF); d = ff(d, a, b, c, x[i + 5], 12, 0x4787C62A); c = ff(c, d, a, b, x[i + 6], 17, 0xA8304613); b = ff(b, c, d, a, x[i + 7], 22, 0xFD469501); a = ff(a, b, c, d, x[i + 8], 7, 0x698098D8); d = ff(d, a, b, c, x[i + 9], 12, 0x8B44F7AF); c = ff(c, d, a, b, x[i + 10], 17, 0xFFFF5BB1); b = ff(b, c, d, a, x[i + 11], 22, 0x895CD7BE); a = ff(a, b, c, d, x[i + 12], 7, 0x6B901122); d = ff(d, a, b, c, x[i + 13], 12, 0xFD987193); c = ff(c, d, a, b, x[i + 14], 17, 0xA679438E); b = ff(b, c, d, a, x[i + 15], 22, 0x49B40821); a = gg(a, b, c, d, x[i + 1], 5, 0xF61E2562); d = gg(d, a, b, c, x[i + 6], 9, 0xC040B340); c = gg(c, d, a, b, x[i + 11], 14, 0x265E5A51); b = gg(b, c, d, a, x[i + 0], 20, 0xE9B6C7AA); a = gg(a, b, c, d, x[i + 5], 5, 0xD62F105D); d = gg(d, a, b, c, x[i + 10], 9, 0x02441453); c = gg(c, d, a, b, x[i + 15], 14, 0xD8A1E681); b = gg(b, c, d, a, x[i + 4], 20, 0xE7D3FBC8); a = gg(a, b, c, d, x[i + 9], 5, 0x21E1CDE6); d = gg(d, a, b, c, x[i + 14], 9, 0xC33707D6); c = gg(c, d, a, b, x[i + 3], 14, 0xF4D50D87); b = gg(b, c, d, a, x[i + 8], 20, 0x455A14ED); a = gg(a, b, c, d, x[i + 13], 5, 0xA9E3E905); d = gg(d, a, b, c, x[i + 2], 9, 0xFCEFA3F8); c = gg(c, d, a, b, x[i + 7], 14, 0x676F02D9); b = gg(b, c, d, a, x[i + 12], 20, 0x8D2A4C8A); a = hh(a, b, c, d, x[i + 5], 4, 0xFFFA3942); d = hh(d, a, b, c, x[i + 8], 11, 0x8771F681); c = hh(c, d, a, b, x[i + 11], 16, 0x6D9D6122); b = hh(b, c, d, a, x[i + 14], 23, 0xFDE5380C); a = hh(a, b, c, d, x[i + 1], 4, 0xA4BEEA44); d = hh(d, a, b, c, x[i + 4], 11, 0x4BDECFA9); c = hh(c, d, a, b, x[i + 7], 16, 0xF6BB4B60); b = hh(b, c, d, a, x[i + 10], 23, 0xBEBFBC70); a = hh(a, b, c, d, x[i + 13], 4, 0x289B7EC6); d = hh(d, a, b, c, x[i + 0], 11, 0xEAA127FA); c = hh(c, d, a, b, x[i + 3], 16, 0xD4EF3085); b = hh(b, c, d, a, x[i + 6], 23, 0x04881D05); a = hh(a, b, c, d, x[i + 9], 4, 0xD9D4D039); d = hh(d, a, b, c, x[i + 12], 11, 0xE6DB99E5); c = hh(c, d, a, b, x[i + 15], 16, 0x1FA27CF8); b = hh(b, c, d, a, x[i + 2], 23, 0xC4AC5665); a = ii(a, b, c, d, x[i + 0], 6, 0xF4292244); d = ii(d, a, b, c, x[i + 7], 10, 0x432AFF97); c = ii(c, d, a, b, x[i + 14], 15, 0xAB9423A7); b = ii(b, c, d, a, x[i + 5], 21, 0xFC93A039); a = ii(a, b, c, d, x[i + 12], 6, 0x655B59C3); d = ii(d, a, b, c, x[i + 3], 10, 0x8F0CCC92); c = ii(c, d, a, b, x[i + 10], 15, 0xFFEFF47D); b = ii(b, c, d, a, x[i + 1], 21, 0x85845DD1); a = ii(a, b, c, d, x[i + 8], 6, 0x6FA87E4F); d = ii(d, a, b, c, x[i + 15], 10, 0xFE2CE6E0); c = ii(c, d, a, b, x[i + 6], 15, 0xA3014314); b = ii(b, c, d, a, x[i + 13], 21, 0x4E0811A1); a = ii(a, b, c, d, x[i + 4], 6, 0xF7537E82); d = ii(d, a, b, c, x[i + 11], 10, 0xBD3AF235); c = ii(c, d, a, b, x[i + 2], 15, 0x2AD7D2BB); b = ii(b, c, d, a, x[i + 9], 21, 0xEB86D391); a = add(a, olda); b = add(b, oldb); c = add(c, oldc); d = add(d, oldd); } return rhex(a) + rhex(b) + rhex(c) + rhex(d); } }
webqq 加密的java 实现
MD5.java
/** * java版webqq3密码与验证码的MD5加密算法 * * @author 作者 E-mail:hangxin1940@gmail.com * @version 创建时间:2011-7-23 19:34:30 */ public class MD5 { /* * A Java implementation of the RSA Data Security, Inc. MD5 Message Digest * Algorithm, as defined in RFC 1321. Based on the JavaScript implementation * of Paul Johnston Copyright (C) Paul Johnston 1999 - 2000. See * http://pajhome.org.uk/site/legal.html for details. Java Version by Thomas * Weber (Orange Interactive GmbH) */ /* * Convert a 32-bit number to a hex string with ls-byte first */ String hex_chr = "0123456789abcdef"; private String rhex(int[] num) { String str = ""; for (int n : num) { for (int j = 0; j <= 3; j++) str = str + hex_chr.charAt((n >> (j * 8 + 4)) & 0x0F) + hex_chr.charAt((n >> (j * 8)) & 0x0F); } // 默认全部大写 return str.toUpperCase(); } /* * 这里改动了 * Add integers, wrapping at 2^32 */ private int add(int x, int y) { int C = (x & 65535) + (y & 65535); int B = (x >> 16) + (y >> 16) + (C >> 16); return (B << 16) | (C & 65535); } /* * Bitwise rotate a 32-bit number to the left */ private int rol(int num, int cnt) { return (num << cnt) | (num >>> (32 - cnt)); } /* * These functions implement the basic operation for each round of the * algorithm. */ private int cmn(int q, int a, int b, int x, int s, int t) { return add(rol(add(add(a, q), add(x, t)), s), b); } private int ff(int a, int b, int c, int d, int x, int s, int t) { return cmn((b & c) | ((~b) & d), a, b, x, s, t); } private int gg(int a, int b, int c, int d, int x, int s, int t) { return cmn((b & d) | (c & (~d)), a, b, x, s, t); } private int hh(int a, int b, int c, int d, int x, int s, int t) { return cmn(b ^ c ^ d, a, b, x, s, t); } private int ii(int a, int b, int c, int d, int x, int s, int t) { return cmn(c ^ (b | (~d)), a, b, x, s, t); } /* * 标准md5加密 */ public String calcMD5(String str) { int[] i = str2bin(str); int[] r = coreMD5(i, str.length() * 8); return rhex(r); } /* * 三遍md5加密,与webqq的实现一样 */ public String calcMD5_3(String str) { int[] i = str2bin(str); int[] r = coreMD5(i, str.length() * 8); r = coreMD5(r, 8 * 16); r = coreMD5(r, 8 * 16); return rhex(r); } /* * 这里改动比较大 * Convert a string to a sequence of 16-word blocks, stored as an * array. Append padding bits and the length, as described in the MD5 * standard. */ private int[] str2bin(String strlen) { int nblk = (strlen.length() / 4) + (strlen.length() % 4 > 0 ? 1 : 0); int[] blks = new int[nblk]; for (int k = 0; k < strlen.length(); k++) { blks[k >> 2] |= strlen.charAt(k) << ((k % 4) * 8); } return blks; } /* * 这里改动比较大 * Take a string and return the hex representation of its MD5. */ private int[] coreMD5(int[] n, int length) { int[] x = new int[(((length + 64) >>> 9) << 4) + 14 + 2]; for (int i = 0; i < n.length; i++) { x[i] = n[i]; } x[length >> 5] |= 0x80 << ((length) % 32); x[(((length + 64) >>> 9) << 4) + 14] = length; int j = 1732584193; int i = -271733879; int h = -1732584194; int g = 271733878; for (int y = 0; y < x.length - 1; y += 16) { int e = j; int d = i; int b = h; int a = g; j = ff(j, i, h, g, x[y + 0], 7, -680876936); g = ff(g, j, i, h, x[y + 1], 12, -389564586); h = ff(h, g, j, i, x[y + 2], 17, 606105819); i = ff(i, h, g, j, x[y + 3], 22, -1044525330); j = ff(j, i, h, g, x[y + 4], 7, -176418897); g = ff(g, j, i, h, x[y + 5], 12, 1200080426); h = ff(h, g, j, i, x[y + 6], 17, -1473231341); i = ff(i, h, g, j, x[y + 7], 22, -45705983); j = ff(j, i, h, g, x[y + 8], 7, 1770035416); g = ff(g, j, i, h, x[y + 9], 12, -1958414417); h = ff(h, g, j, i, x[y + 10], 17, -42063); i = ff(i, h, g, j, x[y + 11], 22, -1990404162); j = ff(j, i, h, g, x[y + 12], 7, 1804603682); g = ff(g, j, i, h, x[y + 13], 12, -40341101); h = ff(h, g, j, i, x[y + 14], 17, -1502002290); i = ff(i, h, g, j, x[y + 15], 22, 1236535329); j = gg(j, i, h, g, x[y + 1], 5, -165796510); g = gg(g, j, i, h, x[y + 6], 9, -1069501632); h = gg(h, g, j, i, x[y + 11], 14, 643717713); i = gg(i, h, g, j, x[y + 0], 20, -373897302); j = gg(j, i, h, g, x[y + 5], 5, -701558691); g = gg(g, j, i, h, x[y + 10], 9, 38016083); h = gg(h, g, j, i, x[y + 15], 14, -660478335); i = gg(i, h, g, j, x[y + 4], 20, -405537848); j = gg(j, i, h, g, x[y + 9], 5, 568446438); g = gg(g, j, i, h, x[y + 14], 9, -1019803690); h = gg(h, g, j, i, x[y + 3], 14, -187363961); i = gg(i, h, g, j, x[y + 8], 20, 1163531501); j = gg(j, i, h, g, x[y + 13], 5, -1444681467); g = gg(g, j, i, h, x[y + 2], 9, -51403784); h = gg(h, g, j, i, x[y + 7], 14, 1735328473); i = gg(i, h, g, j, x[y + 12], 20, -1926607734); j = hh(j, i, h, g, x[y + 5], 4, -378558); g = hh(g, j, i, h, x[y + 8], 11, -2022574463); h = hh(h, g, j, i, x[y + 11], 16, 1839030562); i = hh(i, h, g, j, x[y + 14], 23, -35309556); j = hh(j, i, h, g, x[y + 1], 4, -1530992060); g = hh(g, j, i, h, x[y + 4], 11, 1272893353); h = hh(h, g, j, i, x[y + 7], 16, -155497632); i = hh(i, h, g, j, x[y + 10], 23, -1094730640); j = hh(j, i, h, g, x[y + 13], 4, 681279174); g = hh(g, j, i, h, x[y + 0], 11, -358537222); h = hh(h, g, j, i, x[y + 3], 16, -722521979); i = hh(i, h, g, j, x[y + 6], 23, 76029189); j = hh(j, i, h, g, x[y + 9], 4, -640364487); g = hh(g, j, i, h, x[y + 12], 11, -421815835); h = hh(h, g, j, i, x[y + 15], 16, 530742520); i = hh(i, h, g, j, x[y + 2], 23, -995338651); j = ii(j, i, h, g, x[y + 0], 6, -198630844); g = ii(g, j, i, h, x[y + 7], 10, 1126891415); h = ii(h, g, j, i, x[y + 14], 15, -1416354905); i = ii(i, h, g, j, x[y + 5], 21, -57434055); j = ii(j, i, h, g, x[y + 12], 6, 1700485571); g = ii(g, j, i, h, x[y + 3], 10, -1894986606); h = ii(h, g, j, i, x[y + 10], 15, -1051523); i = ii(i, h, g, j, x[y + 1], 21, -2054922799); j = ii(j, i, h, g, x[y + 8], 6, 1873313359); g = ii(g, j, i, h, x[y + 15], 10, -30611744); h = ii(h, g, j, i, x[y + 6], 15, -1560198380); i = ii(i, h, g, j, x[y + 13], 21, 1309151649); j = ii(j, i, h, g, x[y + 4], 6, -145523070); g = ii(g, j, i, h, x[y + 11], 10, -1120210379); h = ii(h, g, j, i, x[y + 2], 15, 718787259); i = ii(i, h, g, j, x[y + 9], 21, -343485551); j = add(j, e); i = add(i, d); h = add(h, b); g = add(g, a); } int[] r = { j, i, h, g }; return r; } }