博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
由数字与字母组成的验证码的实现
阅读量:4649 次
发布时间:2019-06-09

本文共 3562 字,大约阅读时间需要 11 分钟。

一、软件中使用验证码的作用

  现在软件中的很多地方有用到验证码,比如注册账号、登录的时候等等。验证码的使用是为了防止“爆破”既暴力破解,那什么是暴力破解呢,暴力破解就是利用程序,按照一定的规律往表单中输入数据,逐个测试。验证码 是随机产生的同时它不仅仅是单纯的图片或数字,只有输入的验证码正确之后才会去后台数据库做验证,从而防止暴力破解。

二、示例讲解

  创建一个ASP.NET程序,单独添加一个Web页面用于生成验证码,相关代码如下:

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.Drawing;using System.IO;using System.Drawing.Imaging;namespace UI{    public partial class ValidateCodeCopy : System.Web.UI.Page    {        protected void Page_Load(object sender, EventArgs e)        {            if(!IsPostBack)            {                //生成一个四位的随机码                string validateCode = RndNum(4);                //将随机码保存到session中                Session["ValidateCode"] = validateCode;                CreateImage(validateCode);                        }                    }        ///         /// 生成一个随机码        ///         /// 随机码的位数        /// 
public string RndNum(int VCodeNum ) { string VNUM = ""; 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[]{
','}); Random rd = new Random(); for (int i = 0; i <= VCodeNum - 1;i++) { int t = rd.Next(36); VNUM += VcArray[t]; } return VNUM; } /// /// 生成随机颜色 /// ///
public Color GetRandomColor() { Random Rd_First = new Random((int)DateTime.Now.Ticks); Random Rd_Second = new Random((int)DateTime.Now.Ticks); //生成三个随机数 int red = Rd_First.Next(256); int Green = Rd_Second.Next(256); int blue = (red + Green) > 400 ? 0 : 400 - red + Green; blue = blue > 255 ? 255 : blue; return Color.FromArgb(red,Green, blue); } /// /// 将随机码绘制到图像,写入输出流 /// /// 随机码 public void CreateImage(string str_ValidateCode) { int int_imageWidth = str_ValidateCode.Length * 20; Bitmap thebitmap = new Bitmap(int_imageWidth,40); Random rd = new Random(); Graphics Gr = Graphics.FromImage(thebitmap); Gr.Clear(Color.White); Gr.DrawRectangle(new Pen(Color.LightGray, 1),0,0,int_imageWidth-1,39); Font font = new Font("Arial",16); 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 * 20 + 1 + rd.Next(3), 3+ rd.Next(3)); Gr.DrawString(str_char, font, newBrush, thePos); } MemoryStream ms = new MemoryStream(); thebitmap.Save(ms, ImageFormat.Jpeg); Response.ClearContent(); Response.ContentType = "image/jpeg"; Response.BinaryWrite(ms.ToArray()); Gr.Dispose(); thebitmap.Dispose(); Response.End(); } }}

另外添加一张web页面 用于获取验证码,其前端HTML代码如下:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Test.aspx.cs" Inherits="UI.Test" %>
看不清,换一张?

这样一个简单的验证码就实现了。

                                                                                                                       不积跬步无以至千里

 

 

转载于:https://www.cnblogs.com/YanYongSong/p/4959286.html

你可能感兴趣的文章
APUE 12.7 取消选项
查看>>
思杰20140522
查看>>
02、MySQL—数据库基本操作
查看>>
H5学习之旅-H5的表单(11)
查看>>
np.random的随机数函数
查看>>
HTML5文件拖拽
查看>>
第一个CUDA程序
查看>>
CentOS 6.9/7通过yum安装指定版本的Redis
查看>>
Android中RelativeLayout各个属性的含义
查看>>
PhoneGap API帮助文档翻译—Capture (采集)
查看>>
微信公众平台开发(45)食物营养及热量查询
查看>>
微信公众平台开发(58)自定义菜单
查看>>
FTP上传下载使用ASCII与binary的区别
查看>>
我的网站,书籍收藏
查看>>
单例模式(Singleton)
查看>>
使用jsonEditor打造一个复杂json编辑器
查看>>
oracle基础学习---------1
查看>>
运行Android Studio总是未发现设备
查看>>
CacheManager操作缓存
查看>>
poj 2723 2-SAT问题
查看>>