欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

用asp实现无组件生成验证码的方法2种

程序员文章站 2023-11-17 21:42:10
无组件生成验证码-gif格式 复制代码 代码如下:<% option explicit ' 显示声明 class com_gi...
无组件生成验证码-gif格式
复制代码 代码如下:

<%
option explicit ' 显示声明

class com_gifcode_class
'''''''''''''''''''''''''''''''''''''''''''''
' author: layen support@ssaw.net 84815733(qq)
' thanks: laomi, laomiao, netrube
' 2006-01-02
'''''''''''''''''''''''''''''''''''''''''''''
public noisy, count, width, height, angle, offset, border

private graph(), margin(3)

private sub class_initialize()
randomize
noisy = 16 ' 干扰点出现的概率
count = 4 ' 字符数量
width = 80 ' 图片宽度
height = 20 ' 图片高度
angle = 2 ' 角度随机变化量
offset = 20 ' 偏移随机变化量
border = 1 ' 边框大小
end sub

public function create()

const ccharset = "123456789"

dim i, x, y

dim vvalidcode : vvalidcode = ""
dim vindex

redim graph(width-1, height-1)

for i = 0 to count - 1
vindex = int(rnd * len(ccharset))
vvalidcode = vvalidcode + mid(ccharset, vindex+1 , 1)
setdraw vindex, i
next

create = vvalidcode

end function

sub setdot(px, py)
if px * (width-px-1) >= 0 and py * (height-py-1) >= 0 then
graph(px, py) = 1
end if
end sub

public sub setdraw(pindex, pnumber)

' 字符数据
dim dotdata(8)
dotdata(0) = array(30, 15, 50, 1, 50, 100)
dotdata(1) = array(1 ,34 ,30 ,1 ,71, 1, 100, 34, 1, 100, 93, 100, 100, 86)
dotdata(2) = array(1, 1, 100, 1, 42, 42, 100, 70, 50, 100, 1, 70)
dotdata(3) = array(100, 73, 6, 73, 75, 6, 75, 100)
dotdata(4) = array(100, 1, 1, 1, 1, 50, 50, 35, 100, 55, 100, 80, 50, 100, 1, 95)
dotdata(5) = array(100, 20, 70, 1, 20, 1, 1, 30, 1, 80, 30, 100, 70, 100, 100, 80, 100, 60, 70, 50, 30, 50, 1, 60)
dotdata(6) = array(6, 26, 6, 6, 100, 6, 53, 100)
dotdata(7) = array(100, 30, 100, 20, 70, 1, 30, 1, 1, 20, 1, 30, 100, 70, 100, 80, 70, 100, 30, 100, 1, 80, 1, 70, 100, 30)
dotdata(8) = array(1, 80, 30, 100, 80, 100, 100, 70, 100, 20, 70, 1, 30, 1, 1, 20, 1, 40, 30, 50, 70, 50, 100, 40)

dim vextent : vextent = width / count
margin(0) = border + vextent * (rnd * offset) / 100 + margin(1)
margin(1) = vextent * (pnumber + 1) - border - vextent * (rnd * offset) / 100
margin(2) = border + height * (rnd * offset) / 100
margin(3) = height - border - height * (rnd * offset) / 100

dim vstartx, vendx, vstarty, vendy
dim vwidth, vheight, vdx, vdy, vdeltat

dim vangle, vlength

vwidth = int(margin(1) - margin(0))

vheight = int(margin(3) - margin(2))

' 起始坐标
vstartx = int((dotdata(pindex)(0)-1) * vwidth / 100)

vstarty = int((dotdata(pindex)(1)-1) * vheight / 100)

dim i, j
for i = 1 to ubound(dotdata(pindex), 1)/2

if dotdata(pindex)(2*i-2) <> 0 and dotdata(pindex)(2*i) <> 0 then

' 终点坐标
vendx = (dotdata(pindex)(2*i)-1) * vwidth / 100

vendy = (dotdata(pindex)(2*i+1)-1) * vheight / 100

' 横向差距
vdx = vendx - vstartx
' 纵向差距
vdy = vendy - vstarty

' 倾斜角度
if vdx = 0 then
vangle = sgn(vdy) * 3.14/2
else
vangle = atn(vdy / vdx)
end if

' 两坐标距离
if sin(vangle) = 0 then
vlength = vdx
else
vlength = vdy / sin(vangle)
end if

' 随机转动角度
vangle = vangle + (rnd - 0.5) * 2 * angle * 3.14 * 2 / 100

vdx = int(cos(vangle) * vlength)

vdy = int(sin(vangle) * vlength)

if abs(vdx) > abs(vdy) then vdeltat = abs(vdx) else vdeltat = abs(vdy)

for j = 1 to vdeltat
setdot margin(0) + vstartx + j * vdx / vdeltat, margin(2) + vstarty + j * vdy / vdeltat
next

vstartx = vstartx + vdx

vstarty = vstarty + vdy
end if
next
end sub

public sub output()

response.expires = -9999
response.addheader "pragma", "no-cache"
response.addheader "cache-ctrol", "no-cache"
response.contenttype = "image/gif"

' 文件类型
response.binarywrite chrb(asc("g")) & chrb(asc("i")) & chrb(asc("f"))
' 版本信息
response.binarywrite chrb(asc("8")) & chrb(asc("9")) & chrb(asc("a"))
' 逻辑屏幕宽度
response.binarywrite chrb(width mod 256) & chrb((width \ 256) mod 256)
' 逻辑屏幕高度
response.binarywrite chrb(height mod 256) & chrb((height \ 256) mod 256)

response.binarywrite chrb(128) & chrb(0) & chrb(0)
' 全局颜色列表
response.binarywrite chrb(255) & chrb(255) & chrb(255)

response.binarywrite chrb(0) & chrb(85) & chrb(255)

' 图象标识符
response.binarywrite chrb(asc(","))

response.binarywrite chrb(0) & chrb(0) & chrb(0) & chrb(0)
' 图象宽度
response.binarywrite chrb(width mod 256) & chrb((width \ 256) mod 256)
' 图象高度
response.binarywrite chrb(height mod 256) & chrb((height \ 256) mod 256)

response.binarywrite chrb(0) & chrb(7) & chrb(255)

dim x, y, i : i = 0
for y = 0 to height - 1
for x = 0 to width - 1
if rnd < noisy / 100 then
response.binarywrite chrb(1-graph(x, y))
else
if x * (x-width) = 0 or y * (y-height) = 0 then
response.binarywrite chrb(graph(x, y))
else
if graph(x-1, y) = 1 or graph(x, y) or graph(x, y-1) = 1 then
response.binarywrite chrb(1)
else
response.binarywrite chrb(0)
end if
end if
end if
if (y * width + x + 1) mod 126 = 0 then
response.binarywrite chrb(128)
i = i + 1
end if
if (y * width + x + i + 1) mod 255 = 0 then
if (width*height - y * width - x - 1) > 255 then
response.binarywrite chrb(255)
else
response.binarywrite chrb(width * height mod 255)
end if
end if
next
next
response.binarywrite chrb(128) & chrb(0) & chrb(129) & chrb(0) & chrb(59)
end sub
end class

dim mcode
set mcode = new com_gifcode_class
session("getcode") = mcode.create()
mcode.output()
set mcode = nothing
%>

无组件生成验证码-bmp格式
复制代码 代码如下:

call com_creatvalidcode("validcode")

sub com_creatvalidcode(psn)

     'author: layen
     'qq: 84815733
     'e-mail: support@ssaw.net

 

     ' 禁止缓存
     response.expires = -9999 
     response.addheader "pragma","no-cache"
     response.addheader "cache-ctrol","no-cache"
     response.contenttype = "image/bmp"

     randomize

     dim i, ii, iii

     const codds = 8 ' 杂点出现的机率
     const camount = 36 ' 文字数量
     const ccode = "0123456789abcdefghijklmnopqrstuvwxyz"

     ' 颜色的数据(字符,背景)
     dim vcolordata(1)
     vcolordata(0) = chrb(0) & chrb(0) & chrb(0)  ' 蓝0,绿0,红0(黑色)
     vcolordata(1) = chrb(250) & chrb(236) & chrb(211) ' 蓝250,绿236,红211(浅蓝色)

     ' 随机产生字符
     dim vcode(4), vcodes
     for i = 0 to 3
           vcode(i) = int(rnd * camount)
           vcodes = vcodes & mid(ccode, vcode(i) + 1, 1)
     next

     session(psn) = vcodes  '记录入session

     ' 字符的数据
     dim vnumberdata(35)
     vnumberdata(0) = "1110000111110111101111011110111101001011110100101111
010010111101001011110111101111011110111110000111"
     vnumberdata(1) = "1111011111110001111111110111111111011111111101111111
110111111111011111111101111111110111111100000111"
     vnumberdata(2) = "1110000111110111101111011110111111111011111111011111
111011111111011111111011111111011110111100000011"
     vnumberdata(3) = "1110000111110111101111011110111111110111111100111111
111101111111111011110111101111011110111110000111"
     vnumberdata(4) = "1111101111111110111111110011111110101111110110111111
011011111100000011111110111111111011111111000011"
     vnumberdata(5) = "1100000011110111111111011111111101000111110011101111
111110111111111011110111101111011110111110000111"
     vnumberdata(6) = "1111000111111011101111011111111101111111110100011111
001110111101111011110111101111011110111110000111"
     vnumberdata(7) = "1100000011110111011111011101111111101111111110111111
110111111111011111111101111111110111111111011111"
     vnumberdata(8) = "1110000111110111101111011110111101111011111000011111
101101111101111011110111101111011110111110000111"
     vnumberdata(9) = "1110001111110111011111011110111101111011110111001111
100010111111111011111111101111011101111110001111"
     vnumberdata(10) = "1111011111111101111111101011111110101111111010111111
101011111100000111110111011111011101111000100011"
     vnumberdata(11) = "1000000111110111101111011110111101110111110000111111
011101111101111011110111101111011110111000000111"
     vnumberdata(12) = "1110000011110111101110111110111011111111101111111110
111111111011111111101111101111011101111110001111"
     vnumberdata(13) = "10000011111101110111110111101111011110111101111011110
11110111101111011110111101111011101111000001111"
     vnumberdata(14) = "10000001111101111011110110111111011011111100001111110
11011111101101111110111111111011110111000000111"
     vnumberdata(15) = "10000001111101111011110110111111011011111100001111110
11011111101101111110111111111011111111000111111"
     vnumberdata(16) = "11100001111101110111101111011110111111111011111111101
11111111011100011101111011111011101111110001111"
     vnumberdata(17) = "10001000111101110111110111011111011101111100000111110
11101111101110111110111011111011101111000100011"
     vnumberdata(18) = "11000001111111011111111101111111110111111111011111111
10111111111011111111101111111110111111100000111"
     vnumberdata(19) = "11100000111111101111111110111111111011111111101111111
11011111111101111111110111110111011111000011111"
     vnumberdata(20) = "10001000111101110111110110111111010111111100011111110
10111111101101111110110111111011101111000100011"
     vnumberdata(21) = "10001111111101111111110111111111011111111101111111110
11111111101111111110111111111011110111000000011"
     vnumberdata(22) = "10001000111100100111110010011111001001111101010111110
10101111101010111110101011111010101111001010011"
     vnumberdata(23) = "10001000111100110111110011011111010101111101010111110
10101111101100111110110011111011001111000110111"
     vnumberdata(24) = "11100011111101110111101111101110111110111011111011101
11110111011111011101111101111011101111110001111"
     vnumberdata(25) = "10000001111101111011110111101111011110111100000111110
11111111101111111110111111111011111111000111111"
     vnumberdata(26) = "11100011111101110111101111101110111110111011111011101
11110111011111011101001101111011001111110001011"
     vnumberdata(27) = "10000011111101110111110111011111011101111100001111110
10111111101101111110110111111011101111000110011"
     vnumberdata(28) = "11100000111101111011110111101111011111111110011111111
11001111111111011110111101111011110111100000111"
     vnumberdata(29) = "10000000111011011011111101111111110111111111011111111
10111111111011111111101111111110111111110001111"
     vnumberdata(30) = "10001000111101110111110111011111011101111101110111110
11101111101110111110111011111011101111110001111"
     vnumberdata(31) = "10001000111101110111110111011111011101111110101111111
01011111110101111111010111111110111111111011111"
     vnumberdata(32) = "10010100111101010111110101011111010101111101010111110
01001111110101111111010111111101011111110101111"
     vnumberdata(33) = "10001000111101110111111010111111101011111111011111111
10111111110101111111010111111011101111000100011"
     vnumberdata(34) = "10001000111101110111110111011111101011111110101111111
10111111111011111111101111111110111111110001111"
     vnumberdata(35) = "11000000111101110111111111011111111011111111101111111
10111111111011111111011111111101110111100000011"

     ' 输出图像文件头
     response.binarywrite chrb(66) & chrb(77) & chrb(230) & chrb(4) & chrb(0) & chrb(0) & chrb(0) & chrb(0) &_
           chrb(0) & chrb(0) & chrb(54) & chrb(0) & chrb(0) & chrb(0) & chrb(40) & chrb(0) &_
           chrb(0) & chrb(0) & chrb(40) & chrb(0) & chrb(0) & chrb(0) & chrb(10) & chrb(0) &_
           chrb(0) & chrb(0) & chrb(1) & chrb(0)

     ' 输出图像信息头
     response.binarywrite chrb(24) & chrb(0) & chrb(0) & chrb(0) & chrb(0) & chrb(0) & chrb(176) & chrb(4) &_
           chrb(0) & chrb(0) & chrb(18) & chrb(11) & chrb(0) & chrb(0) & chrb(18) & chrb(11) &_
           chrb(0) & chrb(0) & chrb(0) & chrb(0) & chrb(0) & chrb(0) & chrb(0) & chrb(0) &_
           chrb(0) & chrb(0)

     for i = 9 to 0 step -1  ' 历经所有行
           for ii = 0 to 3  ' 历经所有字
                 for iii = 1 to 10 ' 历经所有像素
                 ' 逐行、逐字、逐像素地输出图像数据
                       if rnd * 99 + 1 < codds then ' 随机生成杂点
                             response.binarywrite vcolordata(0)
                       else
                             response.binarywrite vcolordata(mid(vnumberdata(vcode(ii)), i * 10 + iii, 1))
                       end if
                 next
           next
     next
end sub