當前位置:首頁 » 操作系統 » 驗證碼生成源碼

驗證碼生成源碼

發布時間: 2022-12-15 16:42:47

❶ JSP無法顯示驗證碼~(懂的入)答對了我再加分!

匿名的提問,真不放心

function riwrite(){
var s = "<img src=/200909/login/imgcheck/img.jsp>";
document.getElementById("image").innerHTML=s;
}

----》》》

function riwrite(){
var s = "<img src=/200909/login/imgcheck/img.jsp?"+Math.random()+">";
document.getElementById("image").innerHTML=s;
}

❷ VB 如何實現隨機圖形驗證碼

新建工程,添加一個picturebox,一個textbox,兩個command button
Dim vCode As String

Private Sub Command1_Click()

drawvc
End Sub

Private Sub drawvc() '顯示校驗碼
Dim i, vc, px, py As Long
Dim r, g, b As Byte
Randomize '初始化隨機種子
'生成隨機校驗碼
vc = CLng(8999 * Rnd + 1000)
vCode = vc
'顯示校驗碼
Picture1.Cls
Picture1.Print vc
'添加噪點(防止自動圖像識別)
For i = 0 To 2000 '畫2000個噪點
'畫點隨機位置
px = CLng(Picture1.Width * Rnd)
py = CLng(Picture1.Height * Rnd)
'畫點隨機顏色
r = CByte(255 * Rnd)
g = CByte(255 * Rnd)
b = CByte(255 * Rnd)
Picture1.Line (px, py)-(px + 1, py + 1), RGB(r, g, b)
Next

End Sub

Private Sub Command2_Click()
If Text1.Text = vCode Then
MsgBox ("正確")
Else
MsgBox ("錯誤")
drawvc
End If
End Sub

Private Sub Form_Load()
Picture1.FontSize = 12
Picture1.FontBold = True
Picture1.AutoRedraw = True
drawvc
End Sub

❸ 驗證碼字型檔如何製作啊

很明顯能看出來,這些驗證碼是用java隨機生成並永久保存使用的,因為圖像比較簡單。 通過「071,636,164,567,201」能夠看出來,該網站的驗證碼數量可能有99兆個約0.099吉個, 就算你拿100人民幣可能也不會有人願意編寫源代碼,因為僅生成出所有組合至少要幾個月時間 網站的99兆個驗證碼只是冰山一角,網上流傳的「驗證碼字型檔」只能觀賞,實際破解驗證碼 的通用方法是分割法,把每個數字或者字母分割成上下兩個部分,然後分兩次識別,對於 復雜的驗證碼,需要把閥值調低,盡管這樣,我們民間製作的非盈利識別器的識別率也很低, 建議你不要再嘗試了,很浪費時間。我花了3個月,騰訊QQ注冊的驗證碼識別率還不到 十分之一。 給你另外一種方法,如果你打字速度比較快,可以用N個webbrowser,用TextBox把驗證碼放在一排, 然後人工輸入驗證碼,我個人認為這是最好的方法。 【以上為個人觀點,僅供參考】

python爬驗證碼

1.找地址

首先,我們要找到這個網站生成驗證碼的地址,這個地址我們可以通過查看他的源代碼來實現。

1.找地址

首先,我們要找到這個網站生成驗證碼的地址,這個地址我們可以通過查看他的源代碼來實現。

就以某大學教務網為例,這個教務網的模板很多學校都在採用:

我就截取表單的驗證碼部分即可。

<tdalign="center"rowspan="3">
<imgid="imgCode"src="../sys/ValidateCode.aspx"
onclick="changeValidateCode(this)"alt="單擊可更換圖片!"
style="CURSOR:pointer;">
<br>看不清,則單擊圖片!
</td>123456123456

這里就可以知道,地址就是../sys/ValidateCode.aspx

組合一下地址就是http://jwmis.lmu.cn/sys/ValidateCode.aspx

也就是我們等一下要用到的地址了。

我們可以查看一下那個網頁。

2.處理圖片

去查看了一下那個地址

果不其然,都是亂碼,因為驗證碼分為兩種。

1)直接處理成JPG/GIF/PNG或者其他格式,然後直接讀取到一個圖片地址。

2)接收用戶觸發,然後生成,再直接處理成圖像,不讀取到一個圖片地址。

我們這里是第二種,我們要自己來讀取他,到本地,再手動輸入驗證碼。

#-*-coding:utf-8-*-
importurllib2
#驗證碼的處理#
#驗證碼生成頁面的地址#
im_url='http://jwmis.lmu.cn/sys/ValidateCode.aspx'
#讀取驗證碼圖片#
im_data=urllib2.urlopen(im_url).read()
#打開一個Code.PNG文件在D盤,沒有的話自動生成#
f=open('d:\Code.png','wb')
#寫入圖片內容#
f.write(im_data)
#關閉文件#
f.close()

這里包括兩個部分:

1)打開那個生成驗證碼圖片的頁面,讀取

2)將讀取到的內容,保存成圖片,下載到本地

我們這里的地址是可以隨便寫的,保存在你想保存的地方。

到這里我們就完成了驗證碼的一小部分。

by–LoDog

希望能幫到你!

❺ 驗證碼 代碼

你在java發這個問題!我就把java 的驗證碼源碼給你。

<tr>
<td><font color="#FFFFFF">驗證碼:</font></td>
<td width="22%"><input type="text" name="randomcode" class='input' size='4' maxlength="8" style="width:50px;height:18px" onKeyDown="if (event.keyCode==13)submitLogin();">
</td>
<td width="48%"><img src="./showradomcode.jsp"></td>
</tr>

文件二:showradomcode.jsp
<%@ page contentType="image/jpeg" import="java.awt.image.*,javax.imageio.*" %><jsp:useBean id="rc" class="com.hanweb.sso.ldap.util.RandomChar" scope="page"/><%
//設置頁面不緩存
//response.reset();
response.setContentType("image/jpeg");
response.setHeader("Pragma","No-cache");
response.setHeader("Cache-Control","no-cache");
response.setDateHeader("Expires", 0);
//在內存中創建圖象寬60 高18 4個字元 0條干擾線
BufferedImage image = rc.getRandomImage(58, 18, 4, 1);
//設置其session值
session.setAttribute("jcmsrandomchar", rc.getRandomString());
session.setMaxInactiveInterval(100000000);
ImageIO.write(image, "JPEG", response.getOutputStream());
%>

文件三:RandomChar.java
package com.hanweb.sso.ldap.util;
/**
* Title: 隨機碼圖形生成程序的處理 Description:
* 為了加強網路安全,防止黑課工具對系統的攻擊,現在通用隨機碼來解決這個問題,因為每一次產生的隨機碼是隨機的,所以使用窮舉法破解密碼的工具就無能為力了
* Copyright: Copyright (c) 2004-2010 Company: SDLDAP
*
* @author LiChanglai
* @version 1.0
*/
import java.util.*;
import java.awt.*;
import java.awt.image.*;
public class RandomChar {
/**
* 給定范圍獲得隨機顏色
*
* @param fc
* int 參數1
* @param bc
* int 參數2
* @return Color 返回的color對象
*/
private String strRandomString; // 生成的隨機數
/**
* 取其生成的隨機數
*
* @return String
*/
public String getRandomString() {
return strRandomString;
}
/**
* 設置其生成的隨數
*
* @param randomString
* String
*/
public void setRandomString(String randomString) {
this.strRandomString = randomString;
}
/**
* 取其某一范圍的color
*
* @param fc
* int 范圍參數1
* @param bc
* int 范圍參數2
* @return Color
*/
private Color getRandColor(int fc, int bc) {
// 取其隨機顏色
Random random = new Random();
if (fc > 255) {
fc = 255;
}
if (bc > 255) {
bc = 255;
}
int r = fc + random.nextInt(bc - fc);
int g = fc + random.nextInt(bc - fc);
int b = fc + random.nextInt(bc - fc);
return new Color(r, g, b);
}
/**
* 用於生成其隨機數
*
* @param randomCount
* int 隨機數總長度
* @return String 返回的隨機數
*/
private String getRandomString(int nRandomCount) {
/*
* String[] a = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A",
* "B", "C", "D", "E", "F", "G", "H", "I", "G", "K", "L", "M", "N", "O",
* "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "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[] a = { "a", "b", "c", "d", "e", "f", "g", "h", "g", "k", "m",
"n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" };
String strRand = "";
int nRand;
// 取其隨機數的總數
int LengthOfRandom = a.length;
// 生成隨機類
Random random = new Random();
// 循環取隨機取其整數,並將其整數所對應的結果進行累加
for (int i = 0; i < nRandomCount; i++) {
nRand = random.nextInt(LengthOfRandom);
strRand += a[nRand];
}
// 返回累加後的結果
return strRand;
}
/**
* 生成其隨機的圖形
*
* @param width
* int 生成圖形的寬度
* @param height
* int 生成圖形的高度
* @param nLengthOfRandom
* int 隨機碼的個數
* @param nDisturbLineCount
* int 干擾線的條數
* @return BufferedImage
*/
public BufferedImage getRandomImage(int nWidth, int nHeight,
int nLengthOfRandom, int nDisturbLineCount)
{
// 在內存中創建圖象
BufferedImage image = new BufferedImage(nWidth, nHeight,
BufferedImage.TYPE_INT_RGB);
// 獲取圖形上下文
Graphics g = image.getGraphics();
String strRand = "";
nLengthOfRandom = (nLengthOfRandom <= 0) ? 6 : nLengthOfRandom;
nDisturbLineCount = (nDisturbLineCount <= 0) ? 0 : nDisturbLineCount;
// 生成隨機類
Random random = new Random();
// 設定背景色
g.setColor(getRandColor(200, 250));
g.fillRect(0, 0, nWidth, nHeight);
// 設定字體
g.setFont(new Font("Times New Roman", Font.PLAIN, 16));
// 畫邊框
g.setColor(new Color(255, 255, 255));
g.drawRect(0, 0, nWidth - 1, nHeight - 1);
// 隨機產生nDisturbLineCount條干擾線,使圖象中的認證碼不易被其它程序探測到
g.setColor(getRandColor(160, 200));
for (int i = 0; i < nDisturbLineCount; i++) {
int x = random.nextInt(nWidth);
int y = random.nextInt(nHeight);
int xl = random.nextInt(12);
int yl = random.nextInt(12);
g.drawLine(x, y, x + xl, y + yl);
}
// 取隨機產生的認證碼(6位)
strRand = this.getRandomString(nLengthOfRandom);
// 設置其屬性randomString的值
this.strRandomString = strRand;
for (int i = 0; i < nLengthOfRandom; i++) {
// 將認證碼顯示到圖象中
g.setColor(new Color(20 + random.nextInt(110), 20 + random
.nextInt(110), 20 + random.nextInt(110)));
// 調用函數出來的顏色相同,可能是因為種子太接近,所以只能直接生成
g.drawString(strRand.substring(i, i + 1), 13 * i + 5, 14);
}
// 圖象生效
g.dispose();
return image;
}
}

❻ 如何在易語言源碼加入網路驗證碼

你好,首先你取驗證碼,當然是要做某種網頁操作,不然你取驗證碼干什麼?
如果是網頁操作,那麼就用post例子來說,你取驗證碼到圖片框,你輸入驗證碼後就進行post操作,根據返回的內容就可以判斷驗證碼是否正確了啊

如果是本地操作,那你沒必要取網路上的驗證碼的,你完全可以自己生成驗證碼圖片,這種源碼例子很多的

滿意請採納

❼ jsp中顯示驗證碼的代碼怎麼寫

importjava.awt.Color;
importjava.awt.Font;
importjava.awt.Graphics;
importjava.awt.image.BufferedImage;
importjava.io.IOException;
importjava.io.OutputStream;
importjava.util.Random;
importjavax.imageio.ImageIO;
/*生成驗證碼圖片
*/
publicclassMakeCertPic{
//驗證碼圖片中可以出現的字元集,可以根據需要修改
privatecharmapTable[]={
'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','0','1','2','3',
'4','5','6','7','8','9'
};
/*功能:生成彩色驗證碼圖片
參數wedth為生成圖片的寬度,參數height為生成圖片的高度,參數os為頁面的輸出流
*/
publicStringgetCertPic(intwidth,intheight,OutputStreamos){
if(width<=0)
width=60;
if(height<=0)
height=20;
BufferedImageimage=newBufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
//獲取圖形上下文
Graphicsg=image.getGraphics();
//設定背景顏色
g.setColor(newColor(0xDCDCDC));
g.fillRect(0,0,width,height);
//畫邊框
g.setColor(Color.black);
g.drawRect(0,0,width-1,height-1);
//隨機產生的驗證碼
StringstrEnsure="";
//4代表4為驗證碼,如果要產生更多位的驗證碼,則加大數值
for(inti=0;i<4;++i){
strEnsure+=mapTable[(int)(mapTable.length*Math.random())];
}
//將認證碼顯示到圖像中,如果要生成更多位的驗證碼,增加drawString語句
g.setColor(Color.black);
g.setFont(newFont("AtlanticInline",Font.PLAIN,18));
Stringstr=strEnsure.substring(0,1);
g.drawString(str,8,17);
str=strEnsure.substring(1,2);
g.drawString(str,20,15);
str=strEnsure.substring(2,3);
g.drawString(str,35,18);
str=strEnsure.substring(3,4);
g.drawString(str,45,15);
//隨機產生15個干擾點
Randomrand=newRandom();
for(inti=0;i<10;i++){
intx=rand.nextInt(width);
inty=rand.nextInt(height);
g.drawOval(x,y,1,1);
}
//釋放圖形上下文
g.dispose();
try{
//輸出圖形到頁面
ImageIO.write(image,"JPEG",os);

}catch(IOExceptione){
return"";
}
returnstrEnsure;
}
}


makeCertPic.jsp頁面用於調用生成驗證碼圖片的JavaBean,並在客戶端顯示,源代碼如下:
<%@pagecontentType="image/jpeg"%><%@pagelanguage="java"pageEncoding="utf-8"%><jsp:useBeanid="image"scope="page"class="securityCode.pic.MakeCertPic"/><%
Stringstr=image.getCertPic(0,0,response.getOutputStream());
//將驗證碼存入session中
session.setAttribute("certCode",str);
%>


下邊是登錄頁面:
<%@pagelanguage="java"contentType="text/html;charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDHTML4.01Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<metahttp-equiv="Content-Type"content="text/html;charset=UTF-8">
<title>驗證碼測試登錄頁面</title>
<scripttype="text/javascript">
functionchangeimg()
{

varmyimg=document.getElementById("code");
now=newDate();
myimg.src="makeCertPic.jsp?code="+now.getTime();
}
</script>
</head>
<body>
<center>
<formaction="loginCheck.jsp"method="post"/>
用戶名:<inputtype="text"name="username"/><br>
密&nbsp;&nbsp;碼:<inputtype="password"name="password"/><br>
&nbsp;驗證碼:<inputtype="text"name="certCode"/>
<imgid="code"src="makeCertPic.jsp"><ahref="javascript:changeimg()">看不清,換一張</a><br>
<inputtype="submit"value="登錄"/>
</form>
</center>
</body>
</html>

❽ 如何利用Python做簡單的驗證碼識別

1摘要

驗證碼是目前互聯網上非常常見也是非常重要的一個事物,充當著很多系統的防火牆功能,但是隨時OCR技術的發展,驗證碼暴露出來的安全問題也越來越嚴峻。本文介紹了一套字元驗證碼識別的完整流程,對於驗證碼安全和OCR識別技術都有一定的借鑒意義。

然後經過了一年的時間,筆者又研究和get到了一種更強大的基於CNN卷積神經網路的直接端到端的驗證識別技術(文章不是我的,然後我把源碼整理了下,介紹和源碼在這裡面):

基於python語言的tensorflow的『端到端』的字元型驗證碼識別源碼整理(github源碼分享)

2關鍵詞

關鍵詞:安全,字元圖片,驗證碼識別,OCR,Python,SVM,PIL

3免責聲明

本文研究所用素材來自於某舊Web框架的網站完全對外公開的公共圖片資源。

本文只做了該網站對外公開的公共圖片資源進行了爬取,並未越權做任何多餘操作。

本文在書寫相關報告的時候已經隱去漏洞網站的身份信息。

本文作者已經通知網站相關人員此系統漏洞,並積極向新系統轉移。

本報告的主要目的也僅是用於OCR交流學習和引起大家對驗證安全的警覺。

4引言

關於驗證碼的非技術部分的介紹,可以參考以前寫的一篇科普類的文章:

互聯網安全防火牆(1)--網路驗證碼的科普

裡面對驗證碼的種類,使用場景,作用,主要的識別技術等等進行了講解,然而並沒有涉及到任何技術內容。本章內容則作為它的技術補充來給出相應的識別的解決方案,讓讀者對驗證碼的功能及安全性問題有更深刻的認識。

5基本工具

要達到本文的目的,只需要簡單的編程知識即可,因為現在的機器學習領域的蓬勃發展,已經有很多封裝好的開源解決方案來進行機器學習。普通程序員已經不需要了解復雜的數學原理,即可以實現對這些工具的應用了。

主要開發環境:

  • python3.5

  • python SDK版本

  • PIL

  • 圖片處理庫

  • libsvm

  • 開源的svm機器學習庫

  • 關於環境的安裝,不是本文的重點,故略去。

    6基本流程

    一般情況下,對於字元型驗證碼的識別流程如下:

  • 准備原始圖片素材

  • 圖片預處理

  • 圖片字元切割

  • 圖片尺寸歸一化

  • 圖片字元標記

  • 字元圖片特徵提取

  • 生成特徵和標記對應的訓練數據集

  • 訓練特徵標記數據生成識別模型

  • 使用識別模型預測新的未知圖片集

  • 達到根據「圖片」就能返回識別正確的字元集的目標

  • 7素材准備

    7.1素材選擇

    由於本文是以初級的學習研究目的為主,要求「有代表性,但又不會太難」,所以就直接在網上找個比較有代表性的簡單的字元型驗證碼(感覺像在找漏洞一樣)。

    最後在一個比較舊的網站(估計是幾十年前的網站框架)找到了這個驗證碼圖片。

    原始圖:

  • def get_feature(img): """

  • 獲取指定圖片的特徵值,

  • 1. 按照每排的像素點,高度為10,則有10個維度,然後為6列,總共16個維度

  • :param img_path:

  • :return:一個維度為10(高度)的列表 """


  • width, height = img.size


  • pixel_cnt_list = []

  • height = 10 for y in range(height):

  • pix_cnt_x = 0 for x in range(width): if img.getpixel((x, y)) == 0: # 黑色點

  • pix_cnt_x += 1


  • pixel_cnt_list.append(pix_cnt_x) for x in range(width):

  • pix_cnt_y = 0 for y in range(height): if img.getpixel((x, y)) == 0: # 黑色點

  • pix_cnt_y += 1


  • pixel_cnt_list.append(pix_cnt_y) return pixel_cnt_list

  • 然後就將圖片素材特徵化,按照libSVM指定的格式生成一組帶特徵值和標記值的向量文

❾ c# Winform 實現登錄界面驗證碼功能(文末附源碼)

閑來無事,最近自己發現自己的驗證碼功能還沒有寫過。於是就寫下了這篇文章。

界面就比較丑了,一個picturebox,一個textbox,一個button按鈕主要想的是先把功能實現了,萬一以後業務上需要使用呢。

實現以後的功能圖

在文本框中輸入對應文字,點擊確定來驗證,正確時候如圖所示

如果驗證失敗,沒有提示,直接更新驗證碼,當然需要使用的時候根據業務邏輯來就是了,這個就比較簡單了。

第一:生成驗證碼字元串,用到的是Random隨機函數

第二:將該字元串畫在picturebox中

第三點擊圖片,刷新驗證碼

第四驗證驗證碼不區分大小寫

或者區分大小寫

此時完成

源碼:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

namespace suijima

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

        //驗證碼的長度

        private const int iVerifyCodeLength = 6;

        //驗證碼

        private String strVerifyCode = "";

        //匹配字元的臨時變數

        string strTemp = "";

        private void btnUpdate_Click(object sender, EventArgs e)

        {

            UpdateVerifyCode();

        }

        private void Form1_Load(object sender, EventArgs e)

        {

            UpdateVerifyCode();

        }

        //更新驗證碼

        private void UpdateVerifyCode()

        {

            strVerifyCode = CreateRandomCode(iVerifyCodeLength);

            if(strVerifyCode=="")

            {

                return;

            }

            strTemp = strVerifyCode;

            CreateImage(strVerifyCode);

        }

        //生成驗證碼字元串

        private string CreateRandomCode(int iLength)

        {

            int rand;

            char code;

            string randomCode = String.Empty;

            //生成一定長度的驗證碼

            System.Random random = new Random();

            for (int i = 0; i < iLength; i++)

            {

                rand = random.Next();

                if (rand % 3 == 0)

                {

                    code = (char)('A' + (char)(rand % 26));

                }

                else

                {

                    code = (char)('0' + (char)(rand % 10));

                }

                randomCode += code.ToString();

            }

            return randomCode;

        }

        ///  創建驗證碼圖片

        private void CreateImage(string strVerifyCode)

        {

            try

            {

                int iRandAngle = 45;    //隨機轉動角度

                int iMapWidth = (int)(strVerifyCode.Length * 21);

                Bitmap map = new Bitmap(iMapWidth, 28);    //創建圖片背景

                Graphics graph = Graphics.FromImage(map);

                graph.Clear(Color.AliceBlue);//清除畫面,填充背景

                graph.DrawRectangle(new Pen(Color.Black, 0), 0, 0, map.Width - 1, map.Height - 1);//畫一個邊框

                graph.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;//模式

                Random rand = new Random();

                //背景噪點生成

                Pen blackPen = new Pen(Color.LightGray, 0);

                for (int i = 0; i < 50; i++)

                {

                    int x = rand.Next(0, map.Width);

                    int y = rand.Next(0, map.Height);

                    graph.DrawRectangle(blackPen, x, y, 1, 1);

                }

                //驗證碼旋轉,防止機器識別

                char[] chars = strVerifyCode.ToCharArray();//拆散字元串成單字元數組

                //文字距中

                StringFormat format = new StringFormat(StringFormatFlags.NoClip);

                format.Alignment = StringAlignment.Center;

                format.LineAlignment = StringAlignment.Center;

                //定義顏色

                Color[] c = { Color.Black, Color.Red, Color.DarkBlue, Color.Green,

Color.Orange, Color.Brown, Color.DarkCyan, Color.Purple };

                //定義字體

                string[] font = { "Verdana", "Microsoft Sans Serif", "Comic Sans MS", "Arial", "宋體" };

                for (int i = 0; i < chars.Length; i++)

                {

                    int cindex = rand.Next(7);

                    int findex = rand.Next(5); Font f = new System.Drawing.Font(font[findex], 13, System.Drawing.FontStyle.Bold);//字體樣式(參數2為字體大小)

                    Brush b = new System.Drawing.SolidBrush(c[cindex]);

                    Point dot = new Point(16, 16);

                    float angle = rand.Next(-iRandAngle, iRandAngle);//轉動的度數

                    graph.TranslateTransform(dot.X, dot.Y);//移動游標到指定位置

                    graph.RotateTransform(angle);

                    graph.DrawString(chars[i].ToString(), f, b, 1, 1, format);

                    graph.RotateTransform(-angle);//轉回去

                    graph.TranslateTransform(2, -dot.Y);//移動游標到指定位置

                }

                pictureBox1.Image = map;

            }

            catch (ArgumentException)

            {

                MessageBox.Show("創建圖片錯誤。");

            }

        }

        private void button1_Click(object sender, EventArgs e)

        {

            //驗證大小寫

                char[] ch1 = textBox1.Text.ToCharArray();

                char[] ch2 = strTemp.ToCharArray();

                int nCount = 0;

                for (int i = 0; i < strTemp.Length;i++ )

                {

                    if((ch1[i]>='a'&&ch1[i]<='z')||(ch1[i]>='A'&&ch1[i]<='Z'))

                    {

                        if (ch1[i] - 32 == ch2[i] || ch1[i] + 32 == ch2[i])

                        {

                            nCount++;

                        }

                    }

                    else

                    {

                        if (ch1[i]==ch2[i])

                        {

                            nCount++;

                        }

                    }

                }

                if (nCount==strTemp.Length)

                {

                    MessageBox.Show("驗證通過");

                }

                else

                {

                    UpdateVerifyCode();

                    textBox1.Text = "";

                }

            ////不能驗證大小寫

            //if(textBox1.Text==strTemp)

            //{

            //    MessageBox.Show("驗證通過");

            //}

            //else

            //{

            //    UpdateVerifyCode();

            //    textBox1.Text = "";

            //}

        }

        /// <summary>

        /// 圖片點擊事件

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        private void pictureBox1_Click(object sender, EventArgs e)

        {

            UpdateVerifyCode();

        }

    }

}

❿ 網站驗證碼怎麼編寫

還是從最簡單的例子說起,來了解一下驗證碼的基本思路。

第一個例子,在顯示表單的同時,生成一個4位的隨機數做為驗證碼,利用session傳遞該驗證碼,在數據處理頁面,比對用戶輸入的驗證碼與session中的值是否一致。順便說一句,我曾見過有糊塗的仁兄利用hidden類型的input控制項傳遞驗證碼,孰不知,通過查看頁面源代碼,該數據是暴露無遺的。還有password型的input中的數據也一樣。

<%
''''''''**********************************
''''''''* NAME:post.asp *
''''''''* CODE:netops(www.netop.cc) *
''''''''* USE:驗證碼示例一:數字型 *
''''''''* TIME:2005.7 *
''''''''**********************************
Response.Buffer = true
Dim CheckCode

Response.Write "<html><body>"
''''''''======***表單提交後:***======
if Request.ServerVariables("REQUEST_METHOD")="POST" then
Dim founderr,callform,msg
founderr = false ''''''''是否有錯誤發生
callform = false ''''''''是否調用表單
msg = "" ''''''''提示信息

''''''''==============驗證碼驗證開始===========
dim sessionCode
sessionCode = session("chkCode")
session("chkCode") = ""
CheckCode = trim(Request.Form("chkcode"))
if CheckCode = "" then
msg = msg + "<li>請填寫驗證碼"
founderr = true
callform = true
elseif cstr(CheckCode) <> cstr(sessionCode) then
msg = msg + "<li>驗證碼不正確"
founderr = true
callform = true
end if
''''''''==================驗證碼驗證結束============

if founderr = true then
call message("500")
if callform = true then call myform()
else
msg = "<li>操作成功!"
call message("500")
end if

''''''''======***頁面初始化(表單提交前)***======
else
CheckCode = ""
call myform()
end if

Response.Write "</body></html>"

REM 子過程,定義表單
Sub myform()
Response.Write "<table width=500 border=1 cellspacing=0 cellpadding=5 align=center>"&_
"<form name=form1 method=post action=post.asp>"&_
"<tr height=30><td rowspan=2 width=100 align=center>驗證碼</td>"&_
"<td><input type=text name=chkcode size=10>"&_
getChkCode()&"</td></tr>"&_
"<tr height=30><td>請填寫文本框右側的驗證碼</td></tr>"&_
"<tr height=30><td colspan=2 align=center>"&_
"<input type=submit name=Submit value=提交></td></tr>"&_
"</form></table>"
end Sub

Rem 子函數,生成驗證碼(四位隨機數)
function getChkCode()
dim ranNum
randomize
ranNum=int(9000*rnd)+1000
session("chkCode") = ranNum
getChkCode = ranNum
end function

Rem 提示信息
sub message(w)
Response.Write "<table width="&w&" border=1 cellspacing=0 cellpadding=5 align=center>"&_
"<tr height=30><td>提示信息</td></tr>"&_
"<tr valign=top><td style=""color:red;"">"&msg&"</td></tr></table>"
end sub
%>

以上代碼展示生成驗證碼的一般思路,生成一個四位隨機數作為驗證碼,這是最簡單,同時也是最不安全的一種方法。
或許你想到了,可以以每位數字對應一張圖片,生成圖片型的驗證碼,就象圖片型計數器那樣處理。這並不是一個好想法,它跟數值型的驗證碼沒有本質上的區別,並不能提高安全性。
下面,介紹如何生成圖片型的驗證碼。
首先,可以通過ASP生成xbm型的驗證碼,這的的確確是一張xbm格式的圖片,而且,你可以任意設置圖片的大小。為了簡單起見,我們仍以數字為例。
可以把生成驗證碼的代碼獨立出來,命名為checkcode.asp:

<%
''''''''**********************************************
''''''''* NAME:checkcode.asp *
''''''''* CODE:netops(www.netop.cc) *
''''''''* USE:生成xbm格式的驗證碼 *
''''''''* TIME:2005.7 *
''''''''**********************************************
on error resume next
dim i
dim countdata
countdata="1234567890"
dim rou,chkcode,chklen
chkcode=""
chklen = 4
randomize
for i=1 to 4
rou = int(rnd*10)
chkcode = chkcode + cstr(rou)
next

dim strDigits
strDigits = Array(_
"0","0x3c","0x66","0x66","0x66","0x66","0x66","0x66","0x66","0x66","0x3c",_
"1","0x30","0x38","0x30","0x30","0x30","0x30","0x30","0x30","0x30","0x30",_
"2","0x3c","0x66","0x60","0x60","0x30","0x18","0x0c","0x06","0x06","0x7e",_
"3","0x3c","0x66","0x60","0x60","0x38","0x60","0x60","0x60","0x66","0x3c",_
"4","0x30","0x30","0x38","0x38","0x34","0x34","0x32","0x7e","0x30","0x78",_
"5","0x7e","0x06","0x06","0x06","0x3e","0x60","0x60","0x60","0x66","0x3c",_
"6","0x38","0x0c","0x06","0x06","0x3e","0x66","0x66","0x66","0x66","0x3c",_
"7","0x7e","0x66","0x60","0x60","0x30","0x30","0x18","0x18","0x0c","0x0c",_
"8","0x3c","0x66","0x66","0x66","0x3c","0x66","0x66","0x66","0x66","0x3c",_
"9","0x3c","0x66","0x66","0x66","0x66","0x7c","0x60","0x60","0x30","0x1c")

dim iCharWidth,iCharHeight,theBit,theNum,iRow,k,theOffset
dim imageStr
imageStr = ""

iCharWidth = 8
iCharHeight= 10*1
Response.ContentType ="image/x-xbitmap"
Response.Expires =0
Response.Write "#define counter_width "&iCharWidth*chklen&chr(13) & chr(10)
Response.Write "#define counter_height "&iCharHeight&chr(13) & chr(10)
Response.Write "static unsigned char counter_bits[]={"
for iRow=0 to iCharHeight-1
for i=1 to chklen
theBit=mid(chkcode,i,1)
k=0
do while k<ubound(strDigits)
if strDigits(k) = theBit then exit do
k=k+iCharHeight+1
loop
if k>=ubound(strDigits) then k=0
theOffset = k + 1
imageStr = imageStr + (strDigits(theOffset+iRow))&","
next
next

imageStr = left(imageStr,(len(imageStr)-1))
Response.Write imageStr

Response.Write "};"
session("chkCode") = chkcode
%>

在post.asp中,定義表單時,相應的代碼改為:

REM 子過程,定義表單
Sub myform()
Response.Write "<table width=500 border=1 cellspacing=0 cellpadding=5 align=center>"&_
"<form name=form1 method=post action=default.asp>"&_
"<tr height=30><td rowspan=2 width=100 align=center>驗證碼</td>"&_
"<td><input type=text name=chkcode size=10>"&_
"<img src=checkcode.asp width=32 height=10 border=0></td></tr>"&_
"<tr height=30><td>請填寫文本框右側的驗證碼</td></tr>"&_
"<tr height=30><td colspan=2 align=center>"&_
"<input type=submit name=Submit value=提交></td></tr>"&_
"</form></table>"
end Sub

以上就是最簡單的xbm型的驗證碼。但是這種格式的驗證碼,windows xp sp2的用戶無法看到,原因是xp sp2取消了對xbm格式文件的支持。不過,可以通過修改注冊表解決這個問題:打開注冊表,找到HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\Security,用右鍵增加一個 dword 值,改名為 BlockXBM,其值為 00000000 。改好後,重新打開游覽器就可以了。也可以用文件編輯器編輯一文件,內容如下:

REGEDIT4

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\Security]
"BlockXBM"=dword:00000000

將該文件保存為擴展名為reg的注冊表文件,然後雙擊該文件即可。
除了xbm格式的驗證碼,也可以利用Adodb.Stream對象,開發出更復雜的BMP型驗證碼,可以從http://www.netop.cc/downloads/bmpchkcode.rar得到相關程序。利用該程序,可以生成數字、字母混合,彩色,且有彩色背景的驗證碼。
對於租用空間的朋友來說,使用第三方組件受到很大限制。如果擁有自己的伺服器,可以試一下利用ASP繪圖組件shotgraph生成驗證碼,該驗證碼是GIF格式的,具有很廣泛的通用性。該組件可從http://www.netop.cc/downloads/shotgraph.zip得到。壓縮包里有詳細的示常式序和說明文檔。
總之,生成驗證碼的辦法很多,現在有很多第三方組件可以幫助你生成各種樣式的驗證碼,上網找找看吧。

熱點內容
左旋螺紋數控編程實例 發布:2025-05-16 00:11:49 瀏覽:10
安卓游戲舊版本從哪個軟體下載 發布:2025-05-16 00:00:20 瀏覽:329
連接聚類演算法 發布:2025-05-15 23:55:09 瀏覽:978
工資演算法單休 發布:2025-05-15 23:52:30 瀏覽:819
超凡先鋒配置不行怎麼辦 發布:2025-05-15 23:27:54 瀏覽:532
win7取消加密 發布:2025-05-15 23:26:37 瀏覽:472
不用internet打開ftp 發布:2025-05-15 23:06:00 瀏覽:154
sql字元串取數字 發布:2025-05-15 22:57:45 瀏覽:125
推薦編程課 發布:2025-05-15 22:34:12 瀏覽:618
表拒絕訪問 發布:2025-05-15 22:29:37 瀏覽:978