ASP验证码的实现原理及源码(2)

作者:扬子 来源:tingfo.net 时间:2007-10-02 12:14:00 

验证码验证页面例子:chk.asp


<% 
if trim(request.form("pwds"))<>trim(session("pwdt")) then 
%> 
输入错误: 应该为:<%=session("pwdt")%>,可你输入的是:<%=request.form("pwds")%> 
<% 
else 
%> 
输入正确 
<%end if%>

 count.asp



<!--#include file="num.asp"--> 
<% 
’### To encrypt/decrypt include this code in your page  
’### strMyEncryptedString = EncryptString(strString) 
’### strMyDecryptedString = DeCryptString(strMyEncryptedString) 
’### You are free to use this code as long as credits remain in place 
’### also if you improve this code let me know. 
Private Function EncryptString(strString) 
’#################################################################### 
’### Crypt Function (C) 2001 by Slavic Kozyuk grindkore@yahoo.com ### 
’### Arguments: strString <--- String you wish to encrypt ### 
’### Output: Encrypted HEX string ### 
’#################################################################### 
Dim CharHexSet, intStringLen, strTemp, strRAW, i, intKey, intOffSet 
Randomize Timer 
intKey = Round((RND * 1000000) + 1000000) ’##### Key Bitsize 
intOffSet = Round((RND * 1000000) + 1000000) ’##### KeyOffSet Bitsize 
If IsNull(strString) = False Then 
strRAW = strString 
intStringLen = Len(strRAW) 
For i = 0 to intStringLen - 1 
strTemp = Left(strRAW, 1) 
strRAW = Right(strRAW, Len(strRAW) - 1) 
CharHexSet = CharHexSet & Hex(Asc(strTemp) * intKey)& Hex(intKey) 
Next 
EncryptString = CharHexSet & "|" & Hex(intOffSet + intKey) & "|" & Hex(intOffSet) 
Else 
EncryptString = "" 
End If 
End Function 
Private Function DeCryptString(strCryptString) 
’#################################################################### 
’### Crypt Function (C) 2001 by Slavic Kozyuk grindkore@yahoo.com ### 
’### Arguments: Encrypted HEX stringt ### 
’### Output: Decrypted ASCII string ### 
’#################################################################### 
’### Note this function uses HexConv() and get_hxno() functions ### 
’### so make sure they are not removed ### 
’#################################################################### 
Dim strRAW, arHexCharSet, i, intKey, intOffSet, strRawKey, strHexCrypData 
strRawKey = Right(strCryptString, Len(strCryptString) - InStr(strCryptString, "|")) 
intOffSet = Right(strRawKey, Len(strRawKey) - InStr(strRawKey,"|")) 
intKey = HexConv(Left(strRawKey, InStr(strRawKey, "|") - 1)) - HexConv(intOffSet) 
strHexCrypData = Left(strCryptString, Len(strCryptString) - (Len(strRawKey) + 1)) 
arHexCharSet = Split(strHexCrypData, Hex(intKey)) 
For i=0 to UBound(arHexCharSet) 
strRAW = strRAW & Chr(HexConv(arHexCharSet(i))/intKey) 
Next 
DeCryptString = strRAW 
End Function 
Private Function HexConv(hexVar) 
Dim hxx, hxx_var, multiply  
IF hexVar <> "" THEN 
hexVar = UCASE(hexVar) 
hexVar = StrReverse(hexVar) 
DIM hx() 
REDIM hx(LEN(hexVar)) 
hxx = 0 
hxx_var = 0 
FOR hxx = 1 TO LEN(hexVar) 
IF multiply = "" THEN multiply = 1 
hx(hxx) = mid(hexVar,hxx,1) 
hxx_var = (get_hxno(hx(hxx)) * multiply) + hxx_var 
multiply = (multiply * 16) 
NEXT 
hexVar = hxx_var 
HexConv = hexVar 
END IF 
End Function 
Private Function get_hxno(ghx) 
If ghx = "A" Then 
ghx = 10 
ElseIf ghx = "B" Then 
ghx = 11 
ElseIf ghx = "C" Then 
ghx = 12 
ElseIf ghx = "D" Then 
ghx = 13 
ElseIf ghx = "E" Then 
ghx = 14 
ElseIf ghx = "F" Then 
ghx = 15 
End If 
get_hxno = ghx 
End Function 
%> 
<% 
Dim Image 
Dim Width, Height 
Dim num 
Dim digtal 
Dim Length 
Dim sort 
Length = 4 ’自定计数器长度 
Redim sort( Length ) 
num=cint(DeCryptString(request.querystring("sksid"))) 
digital = "" 
For I = 1 To Length -Len( num ) ’补0 
digital = digital & "0" 
Next 
For I = 1 To Len( num ) 
digital = digital & Mid( num, I, 1 ) 
Next 
For I = 1 To Len( digital ) 
sort(I) = Mid( digital, I, 1 ) 
Next 
Width = 8 * Len( digital ) ’图像的宽度 
Height = 10 ’图像的高度,在本例中为固定值 
Response.ContentType="image/x-xbitmap" 
hc=chr(13) & chr(10)  
Image = "#define counter_width " & Width & hc 
Image = Image & "#define counter_height " & Height & hc 
Image = Image & "static unsigned char counter_bits[]={" & hc 
For I = 1 To Height 
For J = 1 To Length 
Image = Image & a(sort(J),I) & "," 
Next 
Next 
Image = Left( Image, Len( Image ) - 1 ) ’去掉最后一个逗号 
Image = Image & "};" & hc 
%> 
<% 
Response.Write Image 
%>


标签:验证码,原理
0
投稿

猜你喜欢

  • 在ASP中如何使用类class

    2007-09-16 17:17:00
  • SQL Server 2008中的MERGE(不仅仅是合并)

    2010-10-15 14:16:00
  • 函数式JavaScript编程指南

    2007-12-08 20:39:00
  • SQL 2008 FileStream数据类型

    2008-10-28 21:07:00
  • [原创][分享]数字格式化转换

    2011-07-04 12:20:15
  • 装了 Access 2003 安全更新 (KB981716) 之后 Access 打不开

    2010-12-09 19:59:00
  • 用VB生成DLL封装ASP代码一个例子:连接access数据库等

    2008-04-07 13:06:00
  • 典型的三行二列居中高度自适应css布局

    2008-02-22 16:02:00
  • 怎么写好一份图形界面设计师简历

    2009-04-16 13:10:00
  • CSS样式和JavaScript脚本是否放置于外部文件的探讨

    2008-08-08 12:39:00
  • 原创一个AJAX类

    2008-07-24 13:29:00
  • 最新屏蔽百度快照的方法

    2009-07-06 14:37:00
  • Asp Object 之:AddHeader

    2008-05-05 12:58:00
  • sql语句查询数据库中的表名/列名/主键/自动增长值实例

    2012-07-11 15:28:58
  • 2008北京奥运会倒计时js代码

    2008-01-22 18:18:00
  • 一个div层打开显示效果 js

    2008-05-19 12:35:00
  • 搞定MySQL数据库中文模糊检索问题

    2007-09-17 12:36:00
  • 详解phpMyAdmin的安装和配置

    2007-06-15 09:56:00
  • 弹出对话框,点击跳出一个可拖动的层(对话框)

    2009-09-07 12:56:00
  • MySQL的命令行提示符及其表达的意思

    2008-03-17 13:34:00
  • asp之家 网络编程 m.aspxhome.com