关闭

破解校内的图片识别码。

标签: 破解stringbuffersystemclass磁盘
1840人阅读 评论(0) 收藏 举报

 

using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

namespace XiaoNei.VerifyImage
{
    
public static class Verify
    
{
        
private static int[,] imageDatas;
        
private static String code;
        
private static Bitmap image;

        
/**//// <summary>
        
/// 获取验证码
        
/// </summary>
        
/// <param name="filename">输入文件名</param>
        
/// <returns>验证码</returns>

       
        
public static String getCode(String filename)      
        
{
            Bitmap image 
= new Bitmap(filename);
            readDatas(image);
            code 
= "" + getBestCode(getSimple(35)) +
                    getBestCode(getSimple(
313)) +
                    getBestCode(getSimple(
321)) +
                    getBestCode(getSimple(
329));
            Console.WriteLine(code);

            
//测试识别是否正确,文件名中含有验证码
            
//if (filename.IndexOf(code) < 0)
               
// Console.WriteLine("Error");
            return code;
        }


        
/**//// <summary>
        
/// 初始化数据
        
/// </summary>

        private static void readDatas(Bitmap image)
        
{

            imageDatas 
= new int[image.Height, image.Width];
            Color pixelColor;
            
for (int h = 0; h < image.Height; h++)
            
{
                
for (int w = 0; w < image.Width; w++)
                
{
                    pixelColor 
= image.GetPixel(w, h);

                    imageDatas[h, w] 
= pixelColor.R > 0 ? 1 : 0;
                }
//for
            }
//for
        }

        
/**//// <summary>
        
/// 取数字块
        
/// </summary>
        
/// <param name="x">横坐标</param>
        
/// <param name="y">纵坐标</param>
        
/// <returns>一维数组</returns>

        private static int[] getSimple(int x, int y)
        
{
            
int[] simple = new int[9 * 6];
            
int count = 0;
            
for (int h = x; h < x + 9; h++)
            
{
                
for (int w = y; w < y + 6; w++)
                
{
                    simple[count
++= imageDatas[h, w];
                }

            }

            
return simple;
        }

        
/**//// <summary>
        
/// 最和谐的数字
        
/// </summary>
        
/// <param name="x">数字块</param>
        
/// <returns>结果</returns>

        private static int getBestCode(int[] x)
        
{
            
int bestNumber = -1;
            
double bestVal = 0.0;
            
for (int index = 0; index < 10; index++)
            
{
                
double val = compareSimple(NumberData.getNumber(index), x);
                
if (bestVal <= val)
                
{
                    bestVal 
= val;
                    bestNumber 
= index;
                }

            }

            
return bestNumber;
        }

        
/**//// <summary>
        
/// 获取两个数字图像数组的和谐百分比
        
/// </summary>
        
/// <param name="t">匹配数字数组</param>
        
/// <param name="x">待匹配数字数组</param>
        
/// <returns>和谐百分比</returns>

        private static double compareSimple(int[] t, int[] x)
        
{
            
int count = 0;
            
int val = 0;
            
for (int i = 0; i < t.Length; i++)
            
{
                
if (1 == t[i])
                
{
                    count
++;
                    
if (1 == x[i])
                    
{
                        val
++;
                    }

                }

            }

            
return (val + 0.0/ count;
        }

    }



}


最近在做一个校内刷人气的小工具,顺便把图像识别搞了一下。 把源代码贴出来。请高手指教。

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Net; 
using System.IO;
using System.Text;

namespace XiaoNei.VerifyImage
{
   
public  class DownLoadImage
    
{
        
/// <summary>
        
/// 从图片地址下载图片到本地磁盘
        
/// </summary>
        
/// <param name="ToLocalPath">图片本地磁盘地址</param>
        
/// <param name="Url">图片网址</param>
        
/// <returns></returns>

        public static bool SavePhotoFromUrl(string FileName, string Url)
        
{
            
bool Value = false;
            WebResponse response 
= null;
            Stream stream 
= null;

            
try
            
{
                HttpWebRequest request 
= (HttpWebRequest)WebRequest.Create(Url);

                response 
= request.GetResponse();
                stream 
= response.GetResponseStream();

                
if (!response.ContentType.ToLower().StartsWith("text/"))
                
{
                    Value 
= SaveBinaryFile(response, FileName);

                }


            }

            
catch (Exception err)
            
{
                
string aa = err.ToString();
            }

            
return Value;
        }

        
/// <summary>
        
/// Save a binary file to disk.
        
/// </summary>
        
/// <param name="response">The response used to save the file</param>

        // 将二进制文件保存到磁盘
        private static bool SaveBinaryFile(WebResponse response, string FileName)
        
{
            
bool Value = true;
            
byte[] buffer = new byte[1024];

            
try
            
{
                
if (File.Exists(FileName))
                    File.Delete(FileName);
                Stream outStream 
= System.IO.File.Create(FileName);
                Stream inStream 
= response.GetResponseStream();

                
int l;
                
do
                
{
                    l 
= inStream.Read(buffer, 0, buffer.Length);
                    
if (l > 0)
                        outStream.Write(buffer, 
0, l);
                }

                
while (l > 0);

                outStream.Close();
                inStream.Close();
            }

            
catch
            
{
                Value 
= false;
            }

            
return Value;
        }



    }

}

 

 

using System;
using System.Collections.Generic;
using System.Text;

namespace XiaoNei.VerifyImage
{
    
public class NumberData
    
{

        
public static int[] getNumber(int n)
        
{
            
switch (n)
            
{
                
case 0return num0; }
                
case 1return num1; }
                
case 2return num2; }
                
case 3return num3; }
                
case 4return num4; }
                
case 5return num5; }
                
case 6return num6; }
                
case 7return num7; }
                
case 8return num8; }
                
case 9return num9; }
                
defaultreturn num0; }
            }

        }





        
public static int[] num0 = {
        
0,0,1,1,0,0,
        
0,1,0,0,1,0,
        
1,0,0,0,0,1,
        
1,0,0,0,0,1,
        
1,0,0,0,0,1,
        
1,0,0,0,0,1,
        
1,0,0,0,0,1,
        
0,1,0,0,1,0,
        
0,0,1,1,0,0
    }
;

        
public static int[] num1 = {
        
0,0,1,1,0,0,
        
0,1,0,1,0,0,
        
0,0,0,1,0,0,
        
0,0,0,1,0,0,
        
0,0,0,1,0,0,
        
0,0,0,1,0,0,
        
0,0,0,1,0,0,
        
0,0,0,1,0,0,
        
0,1,1,1,1,1
    }
;

        
public static int[] num2 = {
        
1,1,1,1,0,0,
        
0,0,0,0,1,0,
        
0,0,0,0,1,0,
        
0,0,0,0,1,0,
        
0,0,0,1,0,0,
        
0,0,1,0,0,0,
        
0,1,0,0,0,0,
        
1,0,0,0,0,0,
        
1,1,1,1,1,0
    }
;

        
public static int[] num3 = {
        
1,1,1,1,0,0,
        
0,0,0,0,1,0,
        
0,0,0,0,1,0,
        
0,0,0,1,0,0,
        
0,1,1,0,0,0,
        
0,0,0,1,0,0,
        
0,0,0,0,1,0,
        
0,0,0,0,1,0,
        
0,1,1,1,0,0
    }
;

        
public static int[] num4 = {
        
0,0,0,0,1,0,
        
0,0,0,1,1,0,
        
0,0,1,0,1,0,
        
0,1,0,0,1,0,
        
1,0,0,0,1,0,
        
1,1,1,1,1,1,
        
0,0,0,0,1,0,
        
0,0,0,0,1,0,
        
0,0,0,0,1,0
    }
;

        
public static int[] num5 = {
        
1,1,1,1,1,0,
        
1,0,0,0,0,0,
        
1,0,0,0,0,0,
        
1,1,1,0,0,0,
        
0,0,0,1,0,0,
        
0,0,0,0,1,0,
        
0,0,0,0,1,0,
        
0,0,0,1,0,0,
        
1,1,1,0,0,0
    }
;
        
public static int[] num6 = {
        
0,0,1,1,1,1,
        
0,1,0,0,0,0,
        
1,0,0,0,0,0,
        
1,0,1,1,0,0,
        
1,1,0,0,1,0,
        
1,0,0,0,0,1,
        
1,0,0,0,0,1,
        
0,1,0,0,1,0,
        
0,0,1,1,0,0
    }
;
        
public static int[] num7 = {
        
1,1,1,1,1,1,
        
0,0,0,0,0,1,
        
0,0,0,0,1,0,
        
0,0,0,1,0,0,
        
0,0,0,1,0,0,
        
0,0,1,0,0,0,
        
0,0,1,0,0,0,
        
0,1,0,0,0,0,
        
0,1,0,0,0,0
    }
;

        
public static int[] num8 = {
        
0,1,1,1,1,0,
        
1,0,0,0,0,1,
        
1,0,0,0,0,1,
        
0,1,0,0,1,0,
        
0,1,1,1,1,0,
        
1,0,0,0,0,1,
        
1,0,0,0,0,1,
        
1,0,0,0,0,1,
        
0,1,1,1,1,0
    }
;

        
public static int[] num9 = {
        
0,0,1,1,0,0,
        
0,1,0,0,1,0,
        
1,0,0,0,0,1,
        
1,0,0,0,0,1,
        
0,1,0,0,1,1,
        
0,0,1,1,0,1,
        
0,0,0,0,0,1,
        
0,0,0,0,1,0,
        
1,1,1,1,0,0
    }
;

    }


}


 

 

 

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:247830次
    • 积分:3288
    • 等级:
    • 排名:第10293名
    • 原创:50篇
    • 转载:26篇
    • 译文:7篇
    • 评论:79条
    最新评论