谁能把QQ验证码识别源代码翻译一下?C#/NET的(170分)

  • 主题发起人 主题发起人 tzvnet
  • 开始时间 开始时间
T

tzvnet

Unregistered / Unconfirmed
GUEST, unregistred user!
谁能把QQ验证码识别源代码翻译一下?C#/NET的,谁能用delphi简单的写下?

using System;

namespace QQ
{
/// <summary>
/// yzm 的摘要说明。
/// </summary>
public class yzm
{
public yzm(public System.Drawing.Bitmap pic)
{
this.bp = pic;
}
/// <summary>
/// 将一个int值存入到4个字节的字节数组(从高地址开始转换,最高地址的值以无符号整型参与&quot;与运算&quot;)
/// </summary>
/// <param name=&quot;thevalue&quot;>要处理的int值</param>
/// <param name=&quot;thebuff&quot;>存放信息的字符数组</param>
public static void getbytesfromint(int thevalue, byte[] thebuff)
{
long v1=0; long v2=0; long v3=0; long v4=0;
uint b1=(uint)4278190080; uint b2=(uint)16711680; uint b3=(uint)65280; uint b4=(uint)255;
v1=thevalue & b1;
v2=thevalue & b2;
v3=thevalue & b3;
v4=thevalue & b4;
thebuff[0]=(byte)(v1>>24);
thebuff[1]=(byte)(v2>>16);
thebuff[2]=(byte)(v3>>8);
thebuff[3]=(byte)v4;
}
/// <summary>
/// 将一个ushort值存入到2个字节的字节数组(从高地址开始转换,最高地址的值以无符号整型参与&quot;与运算&quot;)
/// </summary>
/// <param name=&quot;thevalue&quot;>要处理的ushort值</param>
/// <param name=&quot;thebuff&quot;>存放信息的字符数组</param>
public static void getbytesfromushort(ushort thevalue, byte[] thebuff)
{
ushort v1=0; ushort v2=0;
ushort b1=(ushort)65280; ushort b2=(ushort)255;
v1=(ushort)(thevalue & b1);
v2=(ushort)(thevalue & b2);
thebuff[0]=(byte)(v1>>8);
thebuff[1]=(byte)(v2);
}
/// <summary>
/// 将4个字节的字节数组转换成一个int值
/// </summary>
/// <param name=&quot;thebuff&quot;>字符数组</param>
/// <returns></returns>
public static int getintfrombyte(byte[] thebuff)
{
int jieguo=0;
long mid=0;
long m1=0; long m2=0; long m3=0; long m4=0;
m1=(thebuff[0]<<24);
m2=(thebuff[1]<<16);
m3=(thebuff[2]<<8);
m4=thebuff[3];
mid=m1+m2+m3+m4;
jieguo=(int)mid;
return jieguo;
}
/// <summary>
/// 将2个字节的字节数组转换成一个ushort值
/// </summary>
/// <param name=&quot;thebuff&quot;>字符数组</param>
/// <returns></returns>
public static ushort getushortfrombyte(byte[] thebuff)
{
int jieguo1=0;
jieguo1=(thebuff[0]<<8)+thebuff[1];
ushort jieguo=(ushort)jieguo1;
return jieguo;
}
/// <summary>
/// 将内存中的数据写入硬盘(保存特征库)
/// </summary>
/// <param name=&quot;thefile&quot;>保存的位置</param>
public static void writetofile(string thefile)
{
System.IO.FileStream fs = new System.IO.FileStream(thefile,System.IO.FileMode.OpenOrCreate,System.IO.FileAccess.ReadWrite);
byte[] buff0=new byte[4];
getbytesfromint(datanum,buff0);
fs.Write(buff0,0,4);
for(int ii=0;ii<datanum;ii++)
{
for(int jj=0;jj<20;jj++)
{
byte[] buff=new byte[2];
getbytesfromushort(datap[ii,jj],buff);
fs.Write(buff,0,2);
}
fs.WriteByte(dataxy[ii,0]);
fs.WriteByte(dataxy[ii,1]);
fs.WriteByte(datachar[ii]);
}
fs.Close();
}
/// <summary>
/// 从文件中读取信息,并保存在内存中相应的位置
/// </summary>
/// <param name=&quot;thefile&quot;>特征库文件</param>
public static void readfromfile(string thefile)
{
int allnum=0;
byte[] buff=new byte[4];
System.IO.FileStream fs = new System.IO.FileStream(thefile,System.IO.FileMode.Open,System.IO.FileAccess.Read);
fs.Read(buff,0,4);
allnum=getintfrombyte(buff);
byte[] buff0=new byte[2];
for(int ii=0;ii<allnum;ii++)
{
for(int jj=0;jj<20;jj++)
{
fs.Read(buff0,0,2);
datap[ii,jj]=getushortfrombyte(buff0);
}
fs.Read(buff0,0,1);
dataxy[ii,0]=buff0[0];
fs.Read(buff0,0,1);
dataxy[ii,1]=buff0[0];
fs.Read(buff0,0,1);
datachar[ii]=buff0[0];
}
datanum=allnum;
fs.Close();
}
/// <summary>
/// 验证码图片
/// </summary>
public System.Drawing.Bitmap bp =new System.Drawing.Bitmap(49,20);
/// <summary>
/// 特征库的长度
/// </summary>
public static int datanum=0;
/// <summary>
/// 特征库数据
/// </summary>
public static ushort[,] datap=new ushort[100000,20];
/// <summary>
/// 长度与高度
/// </summary>
public static byte[,] dataxy=new byte[100000,2];
/// <summary>
/// 对应的字符
/// </summary>
public static byte[] datachar=new byte[100000];
/// <summary>
/// 等待处理的数据
/// </summary>
public ushort[] datapic=new ushort[20];
/// <summary>
/// 有效长度
/// </summary>
public byte xlpic=0;
/// <summary>
/// 有效宽度
/// </summary>
public byte ylpic=0;
/// <summary>
/// 检索特征库中存在的记录
/// </summary>
public string getchar()
{
//如果查找不到,就返回空串
string jieguo=&quot;&quot;;
for(int ii=0;ii<datanum;ii++)
{
//统计一共有多少行的像素有差异,如果在4行以内就认为是存在该记录
//这种方法比较原始,但比较适合多线程时的运行,因为程序只进行简单的逻辑比较
//如果能够收集更多的特征库,识别率可以达到80%以上
//(此时可能需要将特征库的容量提高到15W个或以上)
//当然也可以改进品配算法(如使用关键点品配),以用较少的特征库达到较高的识别率,但
//那样有比较大的机会造成识别错误并且多线程时占用较多CPU时间。
int notsamenum=0;
if(dataxy[ii,0]!=xlpic || dataxy[ii,1]!=ylpic)
{
continue;
}
for(int jj=0;jj<20;jj++)
{
if(datap[ii,jj]!=datapic[jj])
{
notsamenum++;
}
}
if(notsamenum<4)
{
char cj=(char)datachar[ii];
return cj.ToString();
}
}
return jieguo;
}
/// <summary>
/// 检查特征库中是否已经存在相关记录
/// </summary>
bool ischardatain()
{
bool jieguo=false;
for(int ii=0;ii<datanum;ii++)
{
//统计一共有多少行的像素有差异,如果在4行以内就认为是存在该记录
//这种方法比较原始,但比较适合多线程时的运行,因为程序只进行简单的逻辑比较
//如果能够收集更多的特征库,识别率可以达到80%以上
//(此时可能需要将特征库的容量提高到15W个或以上)
//当然也可以改进品配算法(如使用关键点品配),以用较少的特征库达到较高的识别率,但
//那样有比较大的机会造成识别错误并且多线程时占用较多CPU时间。
int notsamenum=0;
if(System.Math.Abs(dataxy[ii,0]-xlpic)>1 || System.Math.Abs(dataxy[ii,1]-ylpic)>1)
{
continue;
}
for(int jj=0;jj<20;jj++)
{
if(datap[ii,jj]!=datapic[jj])
{
notsamenum++;
}
}
if(notsamenum<4)
{
string asdasd=((char)datachar[ii]).ToString();
return true;
}
}
return jieguo;
}
/// <summary>
/// 添加到特征库中,并暂时将对应的字符置为空格以待人工识别
/// </summary>
void adddatawithnullchar()
{
if(this.ischardatain())
{
return;
}
for(int ii=0;ii<20;ii++)
{
datap[datanum,ii]=this.datapic[ii];
}
//暂时将对应的字符置为空格以待人工识别
datachar[datanum]=32;
dataxy[datanum,0]=this.xlpic;
dataxy[datanum,1]=this.ylpic;
datanum++;
}
/// <summary>
/// 检查验证码图片是否能分成4个部分,如果可以就检查4个字符在特征库中是否已经存在,如果不存在,
/// 就添加到特征库中,并暂时将对应的字符置为空格以待人工识别
/// </summary>
public void writetodata()
{
bool[,] picpixel=new bool[49,20];
for(int ii=0;ii<49;ii++)
{
for(int jj=0;jj<20;jj++)
{
if(bp.GetPixel(ii,jj).GetBrightness()<0.999)
{
picpixel[ii,jj]=true;
}
}
}
int[] index=new int[8];
int indexnum=0;
bool black=false;
for(int ii=0;ii<49;ii++)
{
bool haveblack=false;
for(int jj=0;jj<20;jj++)
{
if(picpixel[ii,jj])
{
haveblack=true;
break;
}
}
if(haveblack && black==false)
{
index[indexnum]=ii;
indexnum++;
black=true;
}
if(!haveblack && black)
{
index[indexnum]=ii;
indexnum++;
black=false;
}
}
if(indexnum<7)
{
return;
}
if(indexnum==7)
{
index[7]=49;
}
//****
for(int ii=0;ii<4;ii++)
{
int x1=index[ii*2];
int x2=index[ii*2+1];
int y1=0,y2=19;
bool mb=false;
for(int jj=0;jj<20;jj++)
{
for(int kk=x1;kk<x2;kk++)
{
if(picpixel[kk,jj])
{
mb=true;
break;
}
}
if(mb)
{
y1=jj;
break;
}
}
mb=false;
for(int jj=19;jj>=0;jj--)
{
for(int kk=x1;kk<x2;kk++)
{
if(picpixel[kk,jj])
{
mb=true;
break;
}
}
if(mb)
{
y2=jj;
break;
}
}
//**以上是获取有效区域的范围
for(int jj=0;jj<20;jj++)
{
this.datapic[jj]=0;
this.datapic[jj]=0;
}
this.xlpic=(byte)(x2-x1);
//如果字符宽度超过16个像素就不予处理
if(xlpic>16)
{
continue;
}
this.ylpic=(byte)(y2-y1+1);
int ys=-1;
ushort[] addin=new ushort[]{1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768};
for(int jj=y1;jj<=y2;jj++)
{
ys++;
int xs=-1;
for(int kk=x1;kk<x2;kk++)
{
xs++;
if(picpixel[kk,jj])
{
this.datapic[ys]=(ushort)(this.datapic[ys]+addin[xs]);
}
}
}
this.adddatawithnullchar();
}
//****
}
/// <summary>
/// 识别图片
/// </summary>
/// <returns>返回识别结果(如果返回的字符串长度小于4就说明识别失败)</returns>
public string ocrpic()
{
string jieguo=&quot;&quot;;
bool[,] picpixel=new bool[49,20];
for(int ii=0;ii<49;ii++)
{
for(int jj=0;jj<20;jj++)
{
if(bp.GetPixel(ii,jj).GetBrightness()<0.999)
{
picpixel[ii,jj]=true;
}
}
}
int[] index=new int[8];
int indexnum=0;
bool black=false;
for(int ii=0;ii<49;ii++)
{
bool haveblack=false;
for(int jj=0;jj<20;jj++)
{
if(picpixel[ii,jj])
{
haveblack=true;
break;
}
}
if(haveblack && black==false)
{
index[indexnum]=ii;
indexnum++;
black=true;
}
if(!haveblack && black)
{
index[indexnum]=ii;
indexnum++;
black=false;
}
}
if(indexnum<7)
{
return jieguo;
}
if(indexnum==7)
{
index[7]=49;
}
//****
for(int ii=0;ii<4;ii++)
{
int x1=index[ii*2];
int x2=index[ii*2+1];
int y1=0,y2=19;
bool mb=false;
for(int jj=0;jj<20;jj++)
{
for(int kk=x1;kk<x2;kk++)
{
if(picpixel[kk,jj])
{
mb=true;
break;
}
}
if(mb)
{
y1=jj;
break;
}
}
mb=false;
for(int jj=19;jj>=0;jj--)
{
for(int kk=x1;kk<x2;kk++)
{
if(picpixel[kk,jj])
{
mb=true;
break;
}
}
if(mb)
{
y2=jj;
break;
}
}
//**以上是获取有效区域的范围
for(int jj=0;jj<20;jj++)
{
this.datapic[jj]=0;
this.datapic[jj]=0;
}
this.xlpic=(byte)(x2-x1);
//如果字符宽度超过16个像素就不予处理
if(xlpic>16)
{
continue;
}
this.ylpic=(byte)(y2-y1+1);
int ys=-1;
ushort[] addin=new ushort[]{1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768};
for(int jj=y1;jj<=y2;jj++)
{
ys++;
int xs=-1;
for(int kk=x1;kk<x2;kk++)
{
xs++;
if(picpixel[kk,jj])
{
this.datapic[ys]=(ushort)(this.datapic[ys]+addin[xs]);
}
}
}
jieguo=jieguo+this.getchar();
}
return jieguo;
}
}
}
 
这个是JS的MD5算法
function core_md5(x, len)
{
x[len >> 5] |= 0x80 << ((len) % 32);
x[(((len + 64) >>> 9) << 4) + 14] = len;
var a = 1732584193;
var b = - 271733879;
var c = - 1732584194;
var d = 271733878;
for (var i = 0; i < x.length; i += 16)
{
var olda = a;
var oldb = b;
var oldc = c;
var oldd = d;
a = md5_ff(a, b, c, d, x[i + 0], 7, - 680876936);
d = md5_ff(d, a, b, c, x[i + 1], 12, - 389564586);
c = md5_ff(c, d, a, b, x[i + 2], 17, 606105819);
b = md5_ff(b, c, d, a, x[i + 3], 22, - 1044525330);
a = md5_ff(a, b, c, d, x[i + 4], 7, - 176418897);
d = md5_ff(d, a, b, c, x[i + 5], 12, 1200080426);
c = md5_ff(c, d, a, b, x[i + 6], 17, - 1473231341);
b = md5_ff(b, c, d, a, x[i + 7], 22, - 45705983);
a = md5_ff(a, b, c, d, x[i + 8], 7, 1770035416);
d = md5_ff(d, a, b, c, x[i + 9], 12, - 1958414417);
c = md5_ff(c, d, a, b, x[i + 10], 17, - 42063);
b = md5_ff(b, c, d, a, x[i + 11], 22, - 1990404162);
a = md5_ff(a, b, c, d, x[i + 12], 7, 1804603682);
d = md5_ff(d, a, b, c, x[i + 13], 12, - 40341101);
c = md5_ff(c, d, a, b, x[i + 14], 17, - 1502002290);
b = md5_ff(b, c, d, a, x[i + 15], 22, 1236535329);

a = md5_gg(a, b, c, d, x[i + 1], 5, - 165796510);
d = md5_gg(d, a, b, c, x[i + 6], 9, - 1069501632);
c = md5_gg(c, d, a, b, x[i + 11], 14, 643717713);
b = md5_gg(b, c, d, a, x[i + 0], 20, - 373897302);
a = md5_gg(a, b, c, d, x[i + 5], 5, - 701558691);
d = md5_gg(d, a, b, c, x[i + 10], 9, 38016083);
c = md5_gg(c, d, a, b, x[i + 15], 14, - 660478335);
b = md5_gg(b, c, d, a, x[i + 4], 20, - 405537848);
a = md5_gg(a, b, c, d, x[i + 9], 5, 568446438);
d = md5_gg(d, a, b, c, x[i + 14], 9, - 1019803690);
c = md5_gg(c, d, a, b, x[i + 3], 14, - 187363961);
b = md5_gg(b, c, d, a, x[i + 8], 20, 1163531501);
a = md5_gg(a, b, c, d, x[i + 13], 5, - 1444681467);
d = md5_gg(d, a, b, c, x[i + 2], 9, - 51403784);
c = md5_gg(c, d, a, b, x[i + 7], 14, 1735328473);
b = md5_gg(b, c, d, a, x[i + 12], 20, - 1926607734);

a = md5_hh(a, b, c, d, x[i + 5], 4, - 378558);
d = md5_hh(d, a, b, c, x[i + 8], 11, - 2022574463);
c = md5_hh(c, d, a, b, x[i + 11], 16, 1839030562);
b = md5_hh(b, c, d, a, x[i + 14], 23, - 35309556);
a = md5_hh(a, b, c, d, x[i + 1], 4, - 1530992060);
d = md5_hh(d, a, b, c, x[i + 4], 11, 1272893353);
c = md5_hh(c, d, a, b, x[i + 7], 16, - 155497632);
b = md5_hh(b, c, d, a, x[i + 10], 23, - 1094730640);
a = md5_hh(a, b, c, d, x[i + 13], 4, 681279174);
d = md5_hh(d, a, b, c, x[i + 0], 11, - 358537222);
c = md5_hh(c, d, a, b, x[i + 3], 16, - 722521979);
b = md5_hh(b, c, d, a, x[i + 6], 23, 76029189);
a = md5_hh(a, b, c, d, x[i + 9], 4, - 640364487);
d = md5_hh(d, a, b, c, x[i + 12], 11, - 421815835);
c = md5_hh(c, d, a, b, x[i + 15], 16, 530742520);
b = md5_hh(b, c, d, a, x[i + 2], 23, - 995338651);

a = md5_ii(a, b, c, d, x[i + 0], 6, - 198630844);
d = md5_ii(d, a, b, c, x[i + 7], 10, 1126891415);
c = md5_ii(c, d, a, b, x[i + 14], 15, - 1416354905);
b = md5_ii(b, c, d, a, x[i + 5], 21, - 57434055);
a = md5_ii(a, b, c, d, x[i + 12], 6, 1700485571);
d = md5_ii(d, a, b, c, x[i + 3], 10, - 1894986606);
c = md5_ii(c, d, a, b, x[i + 10], 15, - 1051523);
b = md5_ii(b, c, d, a, x[i + 1], 21, - 2054922799);
a = md5_ii(a, b, c, d, x[i + 8], 6, 1873313359);
d = md5_ii(d, a, b, c, x[i + 15], 10, - 30611744);
c = md5_ii(c, d, a, b, x[i + 6], 15, - 1560198380);
b = md5_ii(b, c, d, a, x[i + 13], 21, 1309151649);
a = md5_ii(a, b, c, d, x[i + 4], 6, - 145523070);
d = md5_ii(d, a, b, c, x[i + 11], 10, - 1120210379);
c = md5_ii(c, d, a, b, x[i + 2], 15, 718787259);
b = md5_ii(b, c, d, a, x[i + 9], 21, - 343485551);
a = safe_add(a, olda);
b = safe_add(b, oldb);
c = safe_add(c, oldc);
d = safe_add(d, oldd);
}
if (mode == 16)
{
return Array(b, c);
}
else
{
return Array(a, b, c, d);
}
}

function md5_cmn(q, a, b, x, s, t)
{
return safe_add(bit_rol(safe_add(safe_add(a, q), safe_add(x, t)), s), b);
}
function md5_ff(a, b, c, d, x, s, t)
{
return md5_cmn((b & c) | ((~b) & d), a, b, x, s, t);
}

function md5_gg(a, b, c, d, x, s, t)
{
return md5_cmn((b & d) | (c & (~d)), a, b, x, s, t);
}
function md5_hh(a, b, c, d, x, s, t)
{
return md5_cmn(b ^ c ^ d, a, b, x, s, t);
}
function md5_ii(a, b, c, d, x, s, t)
{
return md5_cmn(c ^ (b | (~d)), a, b, x, s, t);
}
function safe_add(x, y)
{
var lsw = (x & 0xFFFF) + (y & 0xFFFF);
var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
return (msw << 16) | (lsw & 0xFFFF);
}

function bit_rol(num, cnt)
{
return (num << cnt) | (num >>> (32-cnt));
}

function md5_3(s)
{
var tmp = new Array;
tmp = core_md5(str2binl(s), s.length * chrsz)
tmp = core_md5(tmp, 16 * chrsz);
tmp = core_md5(tmp, 16 * chrsz);
return binl2hex(tmp);
}
 
后退
顶部