bigeagle 于 2000-11-9 10:48:18 加贴在 joy asp ↑:
<%
option explicit
const base_64_map_init =
"abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789+/"
dim nl
zero based arrays
dim base64encmap(63)
dim base64decmap(127)
must be called before using anything else
public sub initcodecs()
init vars
nl = "<p>" & chr(13) & chr(10)
setup base 64
dim max, idx
max = len(base_64_map_init)
for idx = 0 to max – 1
one based string
base64encmap(idx) = mid(base_64_map_init, idx + 1, 1)
next
for idx = 0 to max – 1
base64decmap(asc(base64encmap(idx))) = idx
next
end sub
encode base 64 encoded string
public function base64encode(plain)
if len(plain) = 0 then
base64encode = ""
exit function
end if
dim ret, ndx, by3, first, second, third
by3 = (len(plain) \ 3) * 3
ndx = 1
do while ndx <= by3
first = asc(mid(plain, ndx+0, 1))
second = asc(mid(plain, ndx+1, 1))
third = asc(mid(plain, ndx+2, 1))
ret = ret & base64encmap( (first \ 4) and 63 )
ret = ret & base64encmap( ((first * 16) and 48) + ((second \ 16)
and 15 ) )
ret = ret & base64encmap( ((second * 4) and 60) + ((third \ 64)
and 3 ) )
ret = ret & base64encmap( third and 63)
ndx = ndx + 3
loop
check for stragglers
if by3 < len(plain) then
first = asc(mid(plain, ndx+0, 1))
ret = ret & base64encmap( (first \ 4) and 63 )
if (len(plain) mod 3 ) = 2 then
second = asc(mid(plain, ndx+1, 1))
ret = ret & base64encmap( ((first * 16) and 48) +
((second \
16) and 15 ) )
ret = ret & base64encmap( ((second * 4) and 60) )
else
ret = ret & base64encmap( (first * 16) and 48)
ret = ret & "="
end if
ret = ret & "="
end if
base64encode = ret
end function
decode base 64 encoded string
public function base64decode(scrambled)
if len(scrambled) = 0 then
base64decode = ""
exit function
end if
ignore padding
dim reallen
reallen = len(scrambled)
do while mid(scrambled, reallen, 1) = "="
reallen = reallen – 1
loop
dim ret, ndx, by4, first, second, third, fourth
ret = ""
by4 = (reallen \ 4) * 4
ndx = 1
do while ndx <= by4
first = base64decmap(asc(mid(scrambled, ndx+0, 1)))
second = base64decmap(asc(mid(scrambled, ndx+1, 1)))
third = base64decmap(asc(mid(scrambled, ndx+2, 1)))
fourth = base64decmap(asc(mid(scrambled, ndx+3, 1)))
ret = ret & chr( ((first * 4) and 255) + ((second \ 16) and 3)
)
ret = ret & chr( ((second * 16) and 255) + ((third \ 4) and
15) )
ret = ret & chr( ((third * 64) and 255) + (fourth and 63) )
ndx = ndx + 4
loop
check for stragglers, will be 2 or 3 characters
if ndx < reallen then
first = base64decmap(asc(mid(scrambled, ndx+0, 1)))
second = base64decmap(asc(mid(scrambled, ndx+1, 1)))
ret = ret & chr( ((first * 4) and 255) + ((second \ 16) and 3)
)
if reallen mod 4 = 3 then
third = base64decmap(asc(mid(scrambled,ndx+2,1)))
ret = ret & chr( ((second * 16) and 255) + ((third \ 4) and
15) )
end if
end if
base64decode = ret
end function
initialize
call initcodecs
testing code
dim inp, encode
inp = request.querystring("input")
encode = base64encode(inp)
response.write "encoded value = " & encode & nl
response.write "decoded value = " & base64decode(encode) & nl
%>
