Capt Cha
Capt Cha
<%
'ASP Security Image Generator v4.0 - 13/July/2008
'Generate images to make a CAPTCHA test
' 2006-2007 Emir Tzl. All rights reserved.
'http://www.tipstricks.org
'This program is
'modify it under
'as published by
'of the License,
&8",",") ,_
split("10,#10,&1,*6#4,*5#4,*5#3,*4#3,*3#4,*3#3,*2#3,*1#4,#4,&1,&1",",") ,_
split("10,*3#4*3,*1#8*1,*1#3*2#3*1,#3*4#3,&4,&4,&4,&4,&4,&4,&3,&2,&1",",") ,_
split("9,*3#3*3,&1,#6*3,&3,*3#3*3,&5,&5,&5,&5,&5,&5,#9,&12",",") ,_
split("10,*1#6*3,#8*2,#2*3#4*1,#1*5#3*1,*6#3*1,&5,*5#3*2,*4#4*2,*3#4*3,*2#4*4,*1
#4*5,#10,&12",",") ,_
split("11,*1#8*2,#10*1,#3*5#3,#1*7#3,*7#3*1,*3#6*2,*3#7*1,*7#4,*8#3,&4,#3*4#4,&2
,*1#7*3",",") ,_
split("12,*6#4*2,*5#5*2,&2,*4#2*1#3*2,*3#3*1#3*2,*2#3*2#3*2,*1#3*3#3*2,#3*4#3*2,
#12,&9,*7#3*2,&11,&11",",") ,_
split("11,*1#10,&1,*1#3*7,&3,*1#8*2,*1#9*1,*7#4,*8#3,&8,#1*7#3,#3*4#3*1,#10*1,*1
#7*3",",") ,_
split("11,*4#6*1,*2#8*1,*1#4*6,*1#3*7,#3*1#5*2,#10*1,#3*4#4,#3*5#3,&8,&8,*1#3*3#
3*1,*1#9*1,*3#5*3",",") ,_
split("11,#11,&1,*7#4,*7#3*1,*6#4*1,*6#3*2,*5#3*3,*4#4*3,*4#3*4,*3#4*4,*3#3*5,*2
#3*6,*1#4*6",",") ,_
split("11,*2#7*2,*1#9*1,#3*4#4,#3*5#3,#4*3#3*1,*1#8*2,&1,*1#3*1#5*1,&4,&4,#4*3#4
,&2,*2#6*3",",") ,_
split("11,*3#5*3,*1#9*1,*1#3*3#3*1,#3*5#3,&4,&4,#4*4#3,*1#10,*2#5*1#3,*7#3*1,*6#
4*1,*1#8*2,*1#6*4",",") _
)'Previous row must end with _
'#Begin ColorMap
const BmpColorMap = "dffeff000c851700eceeee006c363600da644a00"
ColorMap = Array(_
split("00,01,01",",") ,_
split("02,03,03",",") ,_
split("00,04,04",",") _
)'End ColorMap
'#Auto calculated variables
dim ImageWidth, ImageHeight, arrTextWidth(), TextHeight, LeftMargin, arrTopMargi
n(), CursorPos
dim BmpEndLine, BColor, TColor, NColor
dim i, j, k, x, y
'#Editable consts and variables
dim Bitmap(25,130) '[Height,Width]
const CodeLength = 6 'Secure code length (Max:8)
const CodeType = 0 '0[Random numbers], 1[Random chars and numbers], 2[Fake word]
const CharTracking = 2 'Set the tracking between two characters
const RndTopMargin = true 'Randomize top margin every character
const NoiseEffect = 2 '0[none], 1[sketch], 2[random foreground lines], 3[random
background lines], 4[1 and 3 (Recommed maximum NoiseLine=4)]
const NoiseLine = 7 'Low values make easy OCR, high values decrease readability
const MinLineLength = 6 'Minimum noise line length
const SessionName = "ASPCAPTCHA" 'Where store your secure code
'#Subroutines and functions
function CreateGUID(valLength)
if CodeType = 1 then
strValid = "A0B1C2D3E4F5G6H7I8J9K8L7M6N5O4P3Q2R1S0T1U2V3W4X5Y6Z7
"
else
strValid = "0516273849"
end if
tmpGUID = vbNullString
tmpChr = vbNullString
Randomize(Timer)
end function
sub WriteCanvas(byval valChr, byval valTopMargin)
dim i, j, k, curPos, tmpChr, arrChrMap, strPixMap, drawPixel, pixRepeat
'find char map
arrChrMap = GetCharMap(valChr)
if UBound(arrChrMap) < 1 then
exit sub
end if
'write char
for i=1 to UBound(arrChrMap)
'get pixel map active line
strPixMap = arrChrMap(i)
if Left(strPixMap,1) = "&" then
j = Mid(strPixMap,2)
if (IsNumeric(j) = true) then
strPixMap = arrChrMap(CInt(j))
else
strPixMap = vbNullString
end if
end if
strPixMap = Trim(strPixMap)
'drawing pixel
curPos = CursorPos
drawPixel = false
pixRepeat = vbNullString
for j=1 to Len(strPixMap)
tmpChr = Mid(strPixMap,j,1)
if (IsNumeric(tmpChr) = true) and (j < Len(strPixMap)) t
hen
pixRepeat = pixRepeat & tmpChr
else
'end pixel map?
if IsNumeric(tmpChr) = true then
pixRepeat = pixRepeat & tmpChr
end if
'draw pixel
if (drawPixel = true) and (IsNumeric(pixRepeat)
= true) then
for k=1 to CInt(pixRepeat)
curPos = curPos + 1
Bitmap((valTopMargin + i),curPos
) = TColor
next
elseif IsNumeric(pixRepeat) = true then
curPos = curPos + CInt(pixRepeat)
end if
'what is new command?
if tmpChr = "#" then
drawPixel = true
else
drawPixel = false
end if
pixRepeat = vbNullString
end if
next
next
end sub
sub PrepareBitmap(valSecureCode)
dim i, j
'image dimensions
ImageWidth = UBound(Bitmap,2)
ImageHeight = UBound(Bitmap,1)
'char and text width
redim arrTextWidth(CodeLength)
arrTextWidth(0) = 0
for i=1 to CodeLength
arrTextWidth(i) = CInt(GetCharMap(Mid(secureCode,i,1))(0))
arrTextWidth(0) = arrTextWidth(0) + arrTextWidth(i)
next
arrTextWidth(0) = arrTextWidth(0) + ((CodeLength - 1) * CharTracking)
'text height
TextHeight = CInt(FontMap(0)(0))
'left margin
LeftMargin = Round((ImageWidth - arrTextWidth(0)) / 2)
'top margin
redim arrTopMargin(CodeLength)
arrTopMargin(0) = Round((ImageHeight - TextHeight) / 2)
if RndTopMargin = true then
for i=1 to CodeLength
arrTopMargin(i) = RndInterval(Int(arrTopMargin(0) / 2),(
arrTopMargin(0) + Round(arrTopMargin(0) / 2)))
next
else
for i=1 to CodeLength
arrTopMargin(i) = arrTopMargin(0)
next
end if
'color selection
i = RndInterval(0,UBound(ColorMap))
BColor = ColorMap(i)(0)
NColor = ColorMap(i)(1)
TColor = ColorMap(i)(2)
'Apply background effect
if NoiseEffect = 3 then
AddNoise()
end if
'write text
for i=1 to CodeLength
'calculate cursor pos
CursorPos = 0
for j=(i-1) to 1 step -1
CursorPos = CursorPos + arrTextWidth(j) + CharTracking
next
CursorPos = LeftMargin + CursorPos
'write active char
WriteCanvas Mid(secureCode,i,1),arrTopMargin(i)
next
end sub
sub DrawLine(x0, y0, x1, y1, valClr)
'Reference from Donald Hearn and M. Pauline Baker, Computer Graphics C V
ersion
dim m, b, dx, dy
if (NoiseEffect = 4) and (Bitmap(y0,x0) = TColor) then
clrNoise = vbNullString
else
clrNoise = valClr
end if
Bitmap(y0,x0) = clrNoise
dx = x1 - x0
dy = y1 - y0
if Abs(dx) > Abs(dy) then
m = (dy / dx)
b = y0 - (m * x0)
if dx < 0 then
dx = -1
else
dx = 1
end if
do while x0 <> x1
x0 = x0 + dx
if (NoiseEffect = 4) and (Bitmap(Round((m * x0) + b),x0)
= TColor) then
clrNoise = vbNullString
else
clrNoise = valClr
end if
Bitmap(Round((m * x0) + b),x0) = clrNoise
loop
elseif dy <> 0 then
m = (dx / dy)
b = x0 - (m * y0)
if dy < 0 then
dy = -1
else
dy = 1
end if
do while y0 <> y1
y0 = y0 + dy
if (NoiseEffect = 4) and (Bitmap(y0,Round((m * y0) + b))
= TColor) then
clrNoise = vbNullString
else
clrNoise = valClr
end if
Bitmap(y0,Round((m * y0) + b)) = clrNoise
loop
end if
end sub
sub AddNoise()
dim median, i, j, x0, y0, x1, y1, dx, dy, dxy
if NoiseEffect = 1 then
clrNoise = vbNullString
else
clrNoise = NColor
end if
for i=1 to
x0
y0
x1
y1
NoiseLine
= RndInterval(1,ImageWidth)
= RndInterval(1,ImageHeight)
= RndInterval(1,ImageWidth)
= RndInterval(1,ImageHeight)
end if
next
SendHex(BmpEndLine)
next
Response.Flush
end sub
%>
<%
'#Generate captcha
if CodeType < 2 then
secureCode = CreateGUID(CodeLength)
else
secureCode = FakeWord(CodeLength)
end if
Session(SessionName) = secureCode
PrepareBitmap(secureCode)
if (NoiseEffect > 0) and (NoiseEffect <> 3) then
AddNoise()
end if
SendBitmap()
%>