灰色空间

导航

简单实用方法!!

 简单验证码生成!!

 

protected void Page_Load(object sender, EventArgs e)
    {
      
            string CharList = "0123456789";
            int[] size = { 10, 12, 14 };
            string[] fm = { "宋体", "楷体_GB2312", "黑体" };
            int lastSize = 0;

            DateTime dt = System.DateTime.Now;
            Random rand = new Random();
            int x = rand.Next(10000, 100000);
            string src = x.ToString();
            Bitmap bmp = new Bitmap(100, 20);

            Graphics g = Graphics.FromImage(bmp);
            g.FillRectangle(Brushes.White, 0, 0, 100, 20);
            Color lastcolor = Color.Blue;
            bool info = false;
            for (int i = 0; i < 5; i++)
            {
              char  ch=CharList[Convert.ToInt32(src[i].ToString())];
                    
             

              string temp = "";
              temp += ch;
              int sz = size[rand.Next(0, 3)];
              if (i == 3 && !info) //总共5字,到了第4个还没的重叠的话那第四个要设成14号字
                  //保证重叠发生
                  sz = 14;
              int cr = rand.Next(0, 200);
              int cg = rand.Next(0, 200);
              int cb = rand.Next(0, 200);
              Color c = Color.FromArgb(cr, cg, cb);
              int sub = 0;
              if (lastSize == 14)
              {//如果上一个字是14号,那么下一字向左入侵保证重叠
                  //对大字号重叠的可视性要比小字号强.
                  //重叠时最好将当前字符的颜色设为上一字符颜色
                  c = lastcolor;
                  info = true;
                  sub = 8;
                 
              }
         
               
                this.lblMessage.Text+=ch.ToString();
             
              g.DrawString(ch.ToString(),
           new Font(new FontFamily(fm[rand.Next(0, fm.Length)]), sz),
           new SolidBrush(c),
           new Point(i * 20 - sub, 0));
              //这里因为字号不同,间隔也不同,但每个字的起始点相同,可以修改根据上一个字
              //的大小再调整起始点.
              lastSize = sz;
              lastcolor = c;
              Response.Cookies.Add(new HttpCookie("code", this.lblMessage.Text.ToString()));
             
            }
            this.lblMessage.Text = "";
            bmp.Save("d:/aaa.gif");
            this.Image1.ImageUrl = "d:/aaa.gif";
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            if (Request.Cookies["code"] == null)
            {
                this.Label1.Text = "您的浏览器设置已被禁用 Cookies,您必须设置浏览器允许使用 Cookies 选项后才能使用本系统。";
               
                return;
            }

            if (Request.Cookies["code"].Value.ToString().Trim()==this.TextBox1.Text.Trim())
            {
                Label1.Text = "验证码正确!!";
                Response.Cookies["code"].Value =null;
               
            }

            else
                Label1.Text = "验证码错误,请输入正确的验证码。当前的验证码为" + Request.Cookies["code"].Value.ToString().Trim();
          
                return;
        }


验证码原理:
在生成验证码图片的时候,同时生成了一个Session,其值就是验证码图片中的数字值。

同时,提供输入框让用户输入,提交输入值后,与已有的Session值进行比较,根据判断结果做相应判断。

 

随机生成中文汉字的基本原理!!

 

static void Main(string[] args)
        {
            Encoding gb = Encoding.GetEncoding("gb2312");

            //调用函数产生4个随机中文汉字编码
            object[] bytes = CreateRegionCode(4);

            //根据汉字编码的字节数组解码出中文汉字
            string str1 = gb.GetString((byte[])Convert.ChangeType(bytes[0], typeof(byte[])));
            string str2 = gb.GetString((byte[])Convert.ChangeType(bytes[1], typeof(byte[])));
            string str3 = gb.GetString((byte[])Convert.ChangeType(bytes[2], typeof(byte[])));
            string str4 = gb.GetString((byte[])Convert.ChangeType(bytes[3], typeof(byte[])));

            //输出的控制台
            Console.WriteLine(str1 + str2 + str3 + str4);
            Thread.Sleep(3000);

        }
         public static object[] CreateRegionCode(int strlength)
        {
            //定义一个字符串数组储存汉字编码的组成元素
            string[] rBase=new String [16]{"0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"};
           
            Random rnd=new Random();
       
            //定义一个object数组用来
            object[] bytes=new object[strlength];
 
            /**//*每循环一次产生一个含两个元素的十六进制字节数组,并将其放入bject数组中
             每个汉字有四个区位码组成
             区位码第1位和区位码第2位作为字节数组第一个元素
             区位码第3位和区位码第4位作为字节数组第二个元素
            */
            for(int i=0;i<strlength;i++)
            {
                //区位码第1位
                int r1=rnd.Next(11,14);
                string str_r1=rBase[r1].Trim();
 
                //区位码第2位
                rnd=new Random(r1*unchecked((int)DateTime.Now.Ticks)+i);//更换随机数发生器的种子避免产生重复值
                int r2;
                if (r1==13)
                {
                    r2=rnd.Next(0,7);
                }
                else
                {
                    r2=rnd.Next(0,16);
                }
                string str_r2=rBase[r2].Trim();
 
                //区位码第3位
                rnd=new Random(r2*unchecked((int)DateTime.Now.Ticks)+i);
                int r3=rnd.Next(10,16);
                string str_r3=rBase[r3].Trim();
 
                //区位码第4位
                rnd=new Random(r3*unchecked((int)DateTime.Now.Ticks)+i);
                int r4;
                if (r3==10)
                {
                    r4=rnd.Next(1,16);
                }
                else if (r3==15)

                {
                    r4=rnd.Next(0,15);
                }
                else
                {
                    r4=rnd.Next(0,16);
                }
                string str_r4=rBase[r4].Trim();
 
                //定义两个字节变量存储产生的随机汉字区位码
                byte byte1=Convert.ToByte(str_r1 + str_r2,16);
                byte byte2=Convert.ToByte(str_r3 + str_r4,16);
                //将两个字节变量存储在字节数组中
                byte[] str_r=new byte[]{byte1,byte2};
 
                //将产生的一个汉字的字节数组放入object数组中
                bytes.SetValue(str_r,i);
               
              }
 
            return bytes;
 
            }

 

   #region 从excel中导入数据到ds
       private DataSet BindDsFromExcel(string strFileDir)
         {
           string strConn;
           strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+strFileDir+";Extended Properties='Excel 8.0;HDR=False;IMEX=1'";
          OleDbConnection OleConn = new OleDbConnection(strConn);
           OleConn.Open();
            sql = "SELECT * FROM [Sheet1$]";
          OleDbDataAdapter OleDaExcel = new OleDbDataAdapter(sql,OleConn);
            DataSet  OleDsExcle = new DataSet();
           OleDaExcel.Fill(OleDsExcle,"aa");
           OleConn.Close();
           return OleDsExcle;
       }

        #endregion

 

破解图片的方法 

 

  GF.OCR OCR1;
        private void button1_Click(object sender, EventArgs e)
        {
            CookieContainer currentcookie = new CookieContainer();
            string htmldoc = httpService.getpage("http://www.xx.com", ref currentcookie, null);
            //   webBrowser1.DocumentText=htmldoc;

            OCR1 = new OCR();
            OCR1.Abort = false;
            OCR1.ActivationKey = "Demo";
            OCR1.AllCharacterFile = "AllCharacters.nn";
            OCR1.AllCharacterMapFile = "AllCharacters.map";
            OCR1.AnalyzeLowerCaseCharacters = true;
            OCR1.AnalyzeNumericCharacters = true;
            OCR1.AnalyzeSpecialCharacters = true;
            OCR1.BarcodeType = GF.BarcodeTypes.Code39Extended;

            OCR1.BitmapImageFile = "";
            OCR1.Brightness = 7;
            OCR1.CheckSum = true;
            OCR1.DefaultFolder = "";
            OCR1.Despeckle = 1;
            OCR1.ErrorCharacter = '?';
            OCR1.ErrorCorrection = true;
            OCR1.ExtendedPictureBox = null;
            OCR1.Language = GF.LanguageType.English;
            OCR1.LowerCaseCharacterFile = "NumericCharacters.nn";
            OCR1.LowerCaseCharacterMapFile = "NumericCharacters.map";
            OCR1.MaximumCharacters = 5000;
            OCR1.MaximumHeight = 150;
            OCR1.MaximumSize = 10000;
            OCR1.MaximumWidth = 150;
            OCR1.NumericCharacterFile = "NumericCharacters.nn";
            OCR1.NumericCharacterMapFile = "NumericCharacters.map";
            OCR1.OCRType = GF.OCRTypes.Text;
            OCR1.OrderID = "Demo";
            OCR1.ProductName = "Demo";
            OCR1.RegistrationCodes = "Demo";
            OCR1.RemoveHorizontal = 0;
            OCR1.RemoveVertical = 0;
            OCR1.SectionHorizontalSpace = 1.5;
            OCR1.SectionVerticalSpace = 1.5;
            OCR1.SpecialCharacterFile = "NumericCharacters.nn";
            OCR1.SpecialCharacterMapFile = "NumericCharacters.map";
            OCR1.Statistics = false;
            OCR1.UpperCaseCharacterFile = "NumericCharacters.nn";
            OCR1.UpperCaseCharacterMapFile = "NumericCharacters.map";
            string resultString = null;
            try
            {
                resultString = Regex.Match(htmldoc, @"(?<url>/m4center/verifyImage"?statusID="d*&amp;bindID="d*&amp;serverID="d*)").Value;

                resultString = "http://www.xx.com/" + resultString;
                this.toolStripStatusLabel1.Text = resultString;
                Image img = httpService.getStream(resultString, ref currentcookie, null);
                pictureBox1.Image = img;
                                                  //  上 右  下
                Rectangle cloneRect = new Rectangle(13, 4, 78, 27);        //
                Bitmap bimg = (Bitmap)img;
                bimg = bimg.Clone(cloneRect, img.PixelFormat);

                pictureBox2.Image = bimg;

               // int dgGrayValue = 150;
                int dgGrayValue =vc.GetDgGrayValue(bimg);
               vc.GrayScale(bimg);
              
              // vc.ClearNoise(dgGrayValue, bimg);

               vc.ClearNoise(dgGrayValue, 1, bimg);

            //vc.BlackWhite(bimg, dgGrayValue);
            
                
               pictureBox3.Image = bimg;
              
             

                
                OCR1.BitmapImage = bimg;
                OCR1.Process();
                this.toolStripStatusLabel1.Text = OCR1.Text;
                OCR1.Dispose();
                return;
            }
            catch (ArgumentException ex)
            {
                // Syntax error in the regular expression
            }

        //送值  

string postdata = string.Format("service=direct/0/Login/form&sp=S0&Form0=serialField,passwordField,annexPwdField,

submit&serialField={0}&passwordField={1}&annexPwdField={2}&submit=+%B5%C7%C2%BC+",

     mobile.Trim(), passwd, vcode);
  string htmldoc = httpService.postpage(homepageurl, postdata, ref currentcookie, proxy);
          
          


        }
    }

 

posted on 2008-08-07 11:31  小桐  阅读(352)  评论(0编辑  收藏  举报