ASP验证码的实现原理及源码

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

共4个页面:form.asp; chk.asp; num.asp; count.asp,得到一个随即数字。加密解密后成成XBM图片,利用 session 判断。

      form.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 
%> 
<% 
randomize 
num = int(7999*rnd+2000) ’计数器的值 
num2 = EncryptString(num) 
session("pwdt")=num 
%> 
<form action="chk.asp" method=post> 
请输入验证码: <input type="text" name="pwds"> 
<img src="count.asp?sksid=<%=num2%>"> <input type=submit value=提交> 
</form>


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

猜你喜欢

  • MYSQL数据库实用学习资料之常用命令集合

    2009-03-06 18:12:00
  • ASP--IP字段截取

    2009-05-26 15:47:00
  • LZ77 算法的JS实现

    2010-04-11 22:32:00
  • escape,encodeURI,encodeURIComponent函数比较

    2008-01-27 11:19:00
  • 网页栅格系统研究:蛋糕的切法

    2008-10-24 17:07:00
  • 如何把图片也存到数据库中去?

    2009-11-06 13:56:00
  • 制作设置404页面

    2009-07-03 13:01:00
  • ASP开发的WAP格式简易邮件系统实例

    2008-06-10 17:00:00
  • js实时获得服务器上时间

    2008-11-25 13:55:00
  • 用 onerror 获取错误信息 js Debug

    2008-11-03 19:08:00
  • 适宜做简单搜索的MySQL数据库全文索引

    2009-01-04 13:11:00
  • Logo 设计准则[译]

    2009-07-22 21:05:00
  • Dreamweaver实现flash透明背景

    2008-05-04 09:35:00
  • 黄相如:如何做好用户体验

    2008-06-04 17:34:00
  • 从零开始写jQuery框架

    2008-12-24 13:37:00
  • 用实例分析如何整理SQL Server输入数据

    2009-01-20 15:16:00
  • 网购中的商品评价与口碑传播-译

    2010-07-09 13:44:00
  • ASP处理XSLT转换XML的实现

    2008-10-20 18:37:00
  • FF和IE之间7个JavaScript的差异[译]

    2009-05-04 18:19:00
  • SQL Server各种日期计算方法

    2008-09-11 21:47:00
  • asp之家 网络编程 m.aspxhome.com