http://www.sufeinet.com/plugin.php?id=keke_group

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

分布式系统框架(V2.0) 轻松承载百亿数据,千万流量!讨论专区 - 源码下载 - 官方教程

HttpHelper爬虫框架(V2.7-含.netcore) HttpHelper官方出品,爬虫框架讨论区 - 源码下载 - 在线测试和代码生成

HttpHelper爬虫类(V2.0) 开源的爬虫类,支持多种模式和属性 源码 - 代码生成器 - 讨论区 - 教程- 例子

查看: 5890|回复: 2

[验证码破解] 一个验证码的实现参考代码

[复制链接]
发表于 2012-7-12 07:48:29 | 显示全部楼层 |阅读模式
生成验证码的代码
[C#] 纯文本查看 复制代码
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class YanZhengMa : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //  首先调用RndNum函数生成4位数字的验证码
        string str_ValidateCode = RndNum(4);
        //  然后把它保存在用于验证的Session里边
        Session["imageCode"] = str_ValidateCode;
        //调用CreateImage函数,把验证码绘制到图片中
        CreateImage(str_ValidateCode);
    }
    private string RndNum(int VcodeNum)
    {
        string Vchar = "0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p" +
         ",q,r,s,t,u,v,w,x,y,z";
        string[] VcArray = Vchar.Split(new Char[] { ',' });
        string VNum = "";
        //int temp = -1;
        Random rand = new Random();
        for (int i = 1; i < VcodeNum + 1; i++)
        {

            int t = rand.Next(36);

            VNum += VcArray[t];
        }
        return VNum;
    }
    //生成随机颜色

    private Color GetRandomColor()
    {
        Random RandomNum_First = new Random((int)DateTime.Now.Ticks);
        //  对于C#的随机数,没什么好说的
        System.Threading.Thread.Sleep(RandomNum_First.Next(50));
        Random RandomNum_Sencond = new Random((int)DateTime.Now.Ticks);
        //  为了在白色背景上显示,尽量生成深色
        int int_Red = RandomNum_First.Next(256);
        int int_Green = RandomNum_Sencond.Next(256);
        int int_Blue = (int_Red + int_Green > 400) ? 0 : 400 - int_Red - int_Green;
        int_Blue = (int_Blue > 255) ? 255 : int_Blue;
        return Color.FromArgb(int_Red, int_Green, int_Blue);
    }
    public void CreateImage(string str_ValidateCode)
    {
        int int_ImageWidth = str_ValidateCode.Length * 13;
        Random newRandom = new Random();
        //  图高20px
        Bitmap theBitmap = new Bitmap(int_ImageWidth, 20);
        Graphics theGraphics = Graphics.FromImage(theBitmap);
        //  白色背景
        theGraphics.Clear(Color.White);
        //  灰色边框
        theGraphics.DrawRectangle(new Pen(Color.LightGray, 1), 0, 0, int_ImageWidth - 1, 19);
        //  10pt的字体
        Font theFont = new Font("Arial", 10);
        for (int int_index = 0; int_index < str_ValidateCode.Length; int_index++)
        {
            string str_char = str_ValidateCode.Substring(int_index, 1);
            Brush newBrush = new SolidBrush(GetRandomColor());
            Point thePos = new Point(int_index * 13 + 1 + newRandom.Next(3), 1 + newRandom.Next(3));
            theGraphics.DrawString(str_char, theFont, newBrush, thePos);
        }
        //  将生成的图片发回客户端
        MemoryStream ms = new MemoryStream();
        theBitmap.Save(ms, ImageFormat.Jpeg);

        Response.ClearContent(); //需要输出图象信息 要修改HTTP头 
        Response.ContentType = "image/Png";
        Response.BinaryWrite(ms.ToArray());
        theGraphics.Dispose();
        theBitmap.Dispose();
        Response.End();
    }

}

使用的代码如下所示
[HTML] 纯文本查看 复制代码
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="CRM._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
<script type ="text/javascript">
    function UpdateImage()
    {
        var n=Math.random();
        document.getElementById("image").src="System/CheckCode.aspx?r="+n;
    }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
     <img id ="image" src ="System/CheckCode.aspx" />
       <a href ="javascript:UpdateImage()">看不清啊</a>
    </div>
    </form>
</body>
</html>


1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
发表于 2013-1-5 09:44:18 | 显示全部楼层
顶一下
发表于 2014-7-13 07:09:52 | 显示全部楼层
受教了,学习中……
您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|手机版|小黑屋|手机版|联系我们|关于我们|广告合作|苏飞论坛 ( 豫ICP备18043678号-2)

GMT+8, 2024-5-3 13:13

© 2014-2021

快速回复 返回顶部 返回列表