`
tyb
  • 浏览: 28128 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

上班随笔:破解验证码

阅读更多

photourl为验证码图片路径

import org.apache.http.impl.client.DefaultHttpClient;
import java.awt.image.BufferedImage;

DefaultHttpClient httpclient = = new DefaultHttpClient();
BufferedImage bi = cracktest.getPhotoFromIntenet(httpclient,photourl);

String code = cracktest.crack(bi);


类cracktest .java

package com.test;

import java.awt.color.ColorSpace;
import java.awt.image.BufferedImage;
import java.awt.image.ColorConvertOp;
import java.io.IOException;
import java.io.InputStream;

import javax.imageio.ImageIO;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;


/**
 * 验证码破解类
 *
 */
public class CrackTest {
	final private int[] firstNum = new int[] { 11, 3, 19, 15 };
	final private int[] secondNum = new int[] { 26, 3, 34, 15 };
	final private int[] thirNum = new int[] { 41, 3, 49, 15 };
	final private int[] fourNum = new int[] { 56, 3, 64, 15 };
	final private int[][] totalNum = new int[][] { firstNum, secondNum,thirNum, fourNum };
	final private String Y5 = "6789111234567891112133456789111213143478131434781314347891011121314348910111213349101112";
	final private String Y1 = "78678567345678910111213143456789101112131434567891011121314";	
	final private String Y3 = "5111245611121334561112131434891314348913143456789101112131445678910111213567101112";	
	final private String Y7 = "343434121314349101112131434789101112131434678910345634";
	final private String Y4 = "9101189101167891011456710114561011345678910111213143456789101112131434567891011121314";
	final private String Y9 = "5678124567891213345678910121314349101113143491213143456789101112131445678910111213467891011";
	final private String Y6 = "678910114567891011121334567891011121314345813143478131434578910111213143458910111213459101112";
	final private String Y8 = "567101112456789101112133456789101112131434891314348913143456789101112131445678910111213567101112";
	final private String Y2 = "561314456121314345611121314341011121314349101113143456789101314456789131456781314";
	final private String Y0 = "567891011124567891011121334567891011121314341314341314345678910111213144567891011121356789101112";
	final private String[] totalY = new String[]{Y0,Y1,Y2,Y3,Y4,Y5,Y6,Y7,Y8,Y9};
	
	public BufferedImage gray2(InputStream in) {
		BufferedImage sr = null;
		try {
			BufferedImage src = ImageIO.read(in);
			ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
			ColorConvertOp op = new ColorConvertOp(cs, null);
			sr = op.filter(src, null);

			// ImageIO.write(src, "JPEG", new File(result));
		} catch (IOException e) {
			e.printStackTrace();
		}
		return sr;
	}
	
	public String crack(BufferedImage src) throws CrackFailException{
		StringBuffer result = new StringBuffer();
		for (int ti = 0; ti < totalNum.length; ti++) {
			StringBuffer sb = new StringBuffer();
			int xs = totalNum[ti][0];
			int ys = totalNum[ti][1];
			int xe = totalNum[ti][2];
			int ye = totalNum[ti][3];
			int co = 0;
			for (int i = xs; i < xe; i++) {
				for (int j = ys; j < ye; j++) {
					if (src.getRGB(i, j) <= -7000000) {
						sb.append(j);
					}
				}
			}
			
			for (int i = 0; i < totalY.length; i++) {
				if(compareString(totalY[i], sb+"")){
					result.append(i);
					break;
				}
			}
		}
		if(result.length()<4){
			throw new CrackFailException();
		}
		return result+"";
	}
	
	public static boolean compareString(String str1,String str2){
		return str1.substring(0, 10).equals(str2.subSequence(0, 10)) || str1.substring(0, 10).indexOf(str2.substring(0,10))>=0 
		|| str2.substring(0, 10).indexOf(str1.substring(0,10))>=0;
	}
	
	public BufferedImage getPhotoFromIntenet(DefaultHttpClient httpclient,String photourl){
		BufferedImage bi = null;
		try {
			HttpPost httpost1 = new HttpPost(photourl);
			HttpResponse response1 = httpclient.execute(httpost1);
			HttpEntity entity = response1.getEntity();
			if (entity != null){
			    bi = gray2(entity.getContent());
			}

		} catch (Exception e) {//e.printStackTrace();
			System.out.println(e.getMessage());
		}finally{
			
		}
		return bi;
	}
	
}

class CrackFailException extends Exception{
	public String toString(){
		return "<007>Crack Fail!!!!!!!!!";
	}
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics