上班随笔:破解验证码

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!!!!!!!!!";
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值