新浪微博JAVA代码模拟登陆

一共三个类:BigIntegerRSA、SinaLogo、SinaSSOEncoder


第一个类:加密

package weibo4j.app.login;


import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.InvalidKeyException;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.RSAPublicKeySpec;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;

import org.apache.commons.codec.binary.Hex;


public class BigIntegerRSA {
	
	public String rsaCrypt(String modeHex, String exponentHex, String messageg)
			throws IllegalBlockSizeException, BadPaddingException,
			NoSuchAlgorithmException, InvalidKeySpecException,
			NoSuchPaddingException, InvalidKeyException,
			UnsupportedEncodingException {
		KeyFactory factory = KeyFactory.getInstance("RSA");

		BigInteger m = new BigInteger(modeHex, 16); /* public exponent */
		BigInteger e = new BigInteger(exponentHex, 16); /* modulus */
		RSAPublicKeySpec spec = new RSAPublicKeySpec(m, e);

		RSAPublicKey pub = (RSAPublicKey) factory.generatePublic(spec);
		Cipher enc = Cipher.getInstance("RSA");
		enc.init(Cipher.ENCRYPT_MODE, pub);

		byte[] encryptedContentKey = enc.doFinal(messageg.getBytes("GB2312"));

		return new String(Hex.encodeHex(encryptedContentKey));
	}
}

第二个类:SinaSSOEncoder

package weibo4j.app.login;


public class SinaSSOEncoder {
	  private boolean i=false;

	    private int g=8;

	    public SinaSSOEncoder(){

	    }

	    public String encode(String psw,String servertime,String nonce){
	        String password;
	        password=hex_sha1(""+hex_sha1(hex_sha1(psw))+servertime+nonce);
	        return password;
	    }
	    private String hex_sha1(String j) {
	        return h(b(f(j,j.length()*g), j.length() * g));
	    }

	    private String h(int[] l){
	        String k = i ? "0123456789ABCDEF" : "0123456789abcdef";
	        String m = "";
	        for (int j = 0; j < l.length * 4; j++) {
	            m += k.charAt((l[j >> 2] >> ((3 - j % 4) * 8 + 4)) & 15) + "" + k.charAt((l[j >> 2] >> ((3 - j % 4) * 8)) & 15);
	        }
	        return m;
	    }

	    private int[] b(int[] A,int r){
	        A[r>>5]|=128<<(24-r%32);
	        A[((r+64>>9)<<4)+15]=r;
	        int[] B = new int[80];
	        int z = 1732584193;
	        int y = -271733879;
	        int v = -1732584194;
	        int u = 271733878;
	        int s = -1009589776;
	        for (int o = 0; o < A.length; o += 16) {
	            int q = z;
	            int p = y;
	            int n = v;
	            int m = u;
	            int k = s;
	            for (int l = 0; l < 80; l++) {
	                if (l < 16) {
	                    B[l] = A[o + l];
	                } else {
	                    B[l] = d(B[l - 3] ^ B[l - 8] ^ B[l - 14] ^ B[l - 16], 1);
	                }
	                int C = e(e(d(z, 5), a(l, y, v, u)), e(e(s, B[l]), c(l)));
	                s = u;
	                u = v;
	                v = d(y, 30);
	                y = z;
	                z = C;
	            }
	            z = e(z, q);
	            y = e(y, p);
	            v = e(v, n);
	            u = e(u, m);
	            s = e(s, k);
	        }
	        return new int[]{z,y,v,u,s};
	    }

	    private int a(int k,int j,int m,int l){
	        if(k<20){return(j&m)|((~j)&l);};
	        if(k<40){return j^m^l;};
	        if(k<60){return(j&m)|(j&l)|(m&l);};
	        return j^m^l;
	    }

	    private int c(int j){
	        return(j<20)?1518500249:(j<40)?1859775393:(j<60)?-1894007588:-899497514;
	    }

	    private int e(int j, int m) {
	        int l = (j & 65535) + (m & 65535);
	        int k = (j >> 16) + (m >> 16) + (l >> 16);
	        return (k << 16) | (l & 65535);
	    }

	    private int d(int j,int k){
	        return(j<<k)|(j>>>(32-k));
	    }

	    private int[] f(String m,int r){
	        int[] l;
	        int j = (1<<this.g)-1;
	        int len=((r+64>>9)<<4)+15;
	        int k;
	        for(k=0;k<m.length()*g;k+=g){
	            len = k>>5>len?k>>5:len;
	        }
	        l = new int[len+1];
	        for(k=0;k<l.length;k++){
	            l[k]=0;
	        }
	        for(k=0;k<m.length()*g;k+=g){
	            l[k>>5]|=(m.charAt(k/g)&j)<<(24-k%32);
	        }
	        return l;
	    }
}



第三个类:SinaLogo


package weibo4j.app.login;


import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.util.Date;

import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;

import org.apache.commons.httpclient.Cookie;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


public class SinaLogo {
	
	private static final Log logger = LogFactory.getLog(SinaLogo.class);
	
	static String SINA_PK = "EB2A38568661887FA180BDDB5CABD5F21C7BFD59C090CB2D24"
		+ "5A87AC253062882729293E5506350508E7F9AA3BB77F4333231490F915F6D63C55FE2F08A49B353F444AD39"
		+ "93CACC02DB784ABBB8E42A9B1BBFFFB38BE18D78E87A0E41B9B8F73A928EE0CCEE"
		+ "1F6739884B9777E4FE9E88A1BBE495927AC4A799B3181D6442443";
	
	MultiThreadedHttpConnectionManager connectionManager =new MultiThreadedHttpConnectionManager();
	HttpClient client = new HttpClient(connectionManager);  
	
	public HttpClient logonAndValidate(String userName,String pwd) throws HttpException, IOException {
		String su = encodeAccount(userName);
		String initPageURL = "http://login.sina.com.cn/sso/prelogin.php?entry=sso&"
				+ "callback=sinaSSOController.preloginCallBack&su=" + su
				+ "&rsakt=mod&client=ssologin.js(v1.4.5)" + "&_=" + getServerTime();
		GetMethod getMethod = newGetMethod(initPageURL);

		client.executeMethod(getMethod);
		InputStream is = getMethod.getResponseBodyAsStream();

		String response = inputStreamToString(is, null);

		String jsonBody = StringUtils.substringBetween(response, "(", ")");

		String nonce = "";
		long servertime = 0L;
		String rsakv = "";
		nonce = StringUtils.substringBetween(jsonBody, "nonce\":\"", "\"");
		rsakv = StringUtils.substringBetween(jsonBody, "rsakv\":\"", "\"");
		servertime = Long.parseLong(StringUtils.substringBetween(jsonBody, "servertime\":", ","));

		getMethod.releaseConnection();

		String logonPageURL = "http://login.sina.com.cn/sso/login.php?client=ssologin.js(v1.4.5)";
		PostMethod postMethod2Logon = newPostMethod(logonPageURL);
		String pwdString = servertime + "\t" + nonce + "\n" + pwd;
		String sp = "";
		try {
			sp = new BigIntegerRSA().rsaCrypt(SINA_PK, "10001", pwdString);
		} catch (InvalidKeyException e) {
			logger.error("AES加密密钥大于128!", e);
		} catch (IllegalBlockSizeException e) {
			logger.error(e);
		} catch (BadPaddingException e) {
			logger.error(e);
		} catch (NoSuchAlgorithmException e) {
			logger.error(e);
		} catch (InvalidKeySpecException e) {
			logger.error(e);
		} catch (NoSuchPaddingException e) {
			logger.error(e);
		}
		// SinaSSOEncoder sso = new SinaSSOEncoder();
		// String sp = sso.encode(account.getPasswd(), servertime, nonce);
		logger.info("su=" + su);
		logger.info("servertime=" + servertime);
		logger.info("nonce=" + nonce);
		logger.info("sp=" + sp);
		logger.info("su=" + su + "&servertime=" + servertime + "&nonce=" + nonce + "&sp=" + sp);
		postMethod2Logon
				.setRequestBody(new NameValuePair[] {
						new NameValuePair("entry", "weibo"),
						new NameValuePair("gateway", "1"),
						new NameValuePair("from", ""),
						new NameValuePair("savestate", "7"),
						new NameValuePair("useticket", "1"),
						new NameValuePair("ssosimplelogin", "1"),
						new NameValuePair("useticket", "1"),
						new NameValuePair("vsnf", "1"),
						new NameValuePair("vsnval", ""),
						new NameValuePair("su", su),
						new NameValuePair("service", "miniblog"),
						new NameValuePair("servertime", servertime + ""),
						new NameValuePair("nonce", nonce),
						new NameValuePair("pwencode", "rsa2"),
						new NameValuePair("rsakv", rsakv),
						new NameValuePair("sp", sp),
						new NameValuePair("encoding", "UTF-8"),
						new NameValuePair("prelt", "115"),
						new NameValuePair("url",
								"http://weibo.com/ajaxlogin.php?framelogin=1&callback=parent.sinaSSOController.feedBackUrlCallBack"),
						new NameValuePair("returntype", "META") });
		int statusCode = client.executeMethod(postMethod2Logon);
		if (statusCode == HttpStatus.SC_OK) {
			System.out.println("ok");
		}
		postMethod2Logon.releaseConnection();

		logger.info(postMethod2Logon.getResponseHeaders());
		// 验证是否登录成功
		Cookie cookie = new Cookie("weibo.com", "wvr", "3.6", "/", new Date(2099, 12, 31), false);
		client.getState().addCookie(cookie);

		Cookie[] cookies = client.getState().getCookies();
		boolean containsSue = false;
		boolean containsSup = false;
		for (Cookie singleCookie : cookies) {
			String domain = singleCookie.getDomain();
			logger.info(domain);
			if (domain.equals(".sina.com.cn")) {
				singleCookie.setDomain("weibo.com");
			}
			logger.info(singleCookie);
			if (singleCookie.getName().equals("SUE")) {
				containsSue = true;
			}
			if (singleCookie.getName().equals("SUP")) {
				containsSup = true;
			}
		}
		return client;
	}

	
	public void init(){
		MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
		client = new HttpClient(connectionManager);
		client.getParams().setCookiePolicy("compatibility");
		client.getParams().setParameter("http.protocol.single-cookie-header",
				Boolean.valueOf(true));
		client.getParams().setSoTimeout(20000);
		client.getHttpConnectionManager().getParams().setConnectionTimeout(
				20000);
		
	}
	
	// inputstream转化为String类型
	public static String inputStreamToString(InputStream is, String charset) throws IOException {
		int i = -1;
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		while ((i = is.read()) != -1) {
			baos.write(i);
		}
		if (null == charset) {
			return baos.toString();
		} else {
			return baos.toString(charset);
		}
	}

	protected PostMethod newPostMethod(String url) {
		PostMethod postMethod = new PostMethod(url);
		postMethod
				.setRequestHeader(
						"Accept",
						"image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*");
		postMethod.setRequestHeader("Referer", "http://weibo.com/");
		postMethod.setRequestHeader("Accept-Language", "zh-cn");
		postMethod.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		postMethod.setRequestHeader("Accept-Encoding", "gzip, deflate");
		postMethod
				.setRequestHeader(
						"User-Agent",
						"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; InfoPath.2)");
		postMethod.setRequestHeader("Connection", "Keep-Alive");
		postMethod.setRequestHeader("Cache-Control", "no-cache");
		return postMethod;
	}

	public static String decode(String string) {
		return string.replaceAll("%3A", ":").replaceAll("%2F", "/").replaceAll("%3D", "=")
				.replaceAll("%26", "&");
	}

	// 用户名编码
	@SuppressWarnings("deprecation")
	private String encodeAccount(String account) {
		return (new sun.misc.BASE64Encoder()).encode(URLEncoder.encode(account).getBytes());
	}

	// 六位随机数nonce的产生(不用)
	public String makeNonce(int len) {
		String x = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
		String str = "";
		for (int i = 0; i < len; i++) {
			str += x.charAt((int) (Math.ceil(Math.random() * 1000000) % x.length()));
		}
		return str;
	}

	// servertime的产生(不用)
	public String getServerTime() {
		long servertime = new Date().getTime() / 1000;
		return String.valueOf(servertime);
	}
	
	protected GetMethod newGetMethod(String url) {
		GetMethod getMethod = new GetMethod(url);
		getMethod.setRequestHeader("Accept","image/jpeg, application/x-ms-application, image/gif, application/xaml+xml, image/pjpeg, application/x-ms-xbap, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*");
		getMethod.setRequestHeader("Accept-Language", "zh-CN");
		getMethod.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=utf-8");
		getMethod.setRequestHeader("Accept-Encoding", "deflate");
		getMethod.setRequestHeader("User-Agent","Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.2; CIBA; MAXTHON 2.0)");
		getMethod.setRequestHeader("Connection", "Keep-Alive");
		getMethod.setRequestHeader("Cache-Control", "no-cache");
		return getMethod;
	}
}



最后可通过这三个logonAndValidate方法中的两个变量

boolean containsSue = false;
boolean containsSup = false;

都为true,则登陆验证成功!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值