欢迎光临爱时尚女性网
 

ASP Base64加密解密源码

2012-6-15 编辑:knwz 来源:爱时尚女性网 阅读次数:1150
  导读: ...
<%
燨PTION EXPLICIT
燾onst BASE_64_MAP_INIT = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
燿im newline
燿im Base64EncMap(63)
燿im Base64DecMap(127)
'初始化函数
燩UBLIC SUB initCodecs()
' 初始化变量
newline = "

" & chr(13) & chr(10)
dim max, idx
爉ax = len(BASE_64_MAP_INIT)
for idx = 0 to max - 1
燘ase64EncMap(idx) = mid(BASE_64_MAP_INIT, idx + 1, 1)
next
for idx = 0 to max - 1
燘ase64DecMap(ASC(Base64EncMap(idx))) = idx
next
燛ND SUB
'Base64加密函数
燩UBLIC FUNCTION base64Encode(plain)
if len(plain) = 0 then
燽ase64Encode = ""
爀xit function
end if
dim ret, ndx, by3, first, second, third
by3 = (len(plain) \ 3) * 3
ndx = 1
do while ndx <= by3
爁irst = asc(mid(plain, ndx+0, 1))
爏econd = asc(mid(plain, ndx+1, 1))
爐hird = asc(mid(plain, ndx+2, 1))
爎et = ret & Base64EncMap( (first \ 4) AND 63 )
爎et = ret & Base64EncMap( ((first * 16) AND 48) + ((second \ 16) AND 15 ) )
爎et = ret & Base64EncMap( ((second * 4) AND 60) + ((third \ 64) AND 3 ) )
爎et = ret & Base64EncMap( third AND 63)
爊dx = ndx + 3
loop
if by3 < len(plain) then
爁irst = asc(mid(plain, ndx+0, 1))
爎et = ret & Base64EncMap( (first \ 4) AND 63 )
爄f (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) )
爀lse
ret = ret & Base64EncMap( (first * 16) AND 48)
ret = ret '& "="
爀nd if
爎et = ret '& "="
end if
base64Encode = ret
燛ND FUNCTION
'Base64解密函数
燩UBLIC FUNCTION base64Decode(scrambled)
if len(scrambled) = 0 then
燽ase64Decode = ""
爀xit function
end if
dim realLen
realLen = len(scrambled)
do while mid(scrambled, realLen, 1) = "="
爎ealLen = realLen - 1
loop
dim ret, ndx, by4, first, second, third, fourth
ret = ""
by4 = (realLen \ 4) * 4
ndx = 1
do while ndx <= by4
爁irst = Base64DecMap(asc(mid(scrambled, ndx+0, 1)))
爏econd = Base64DecMap(asc(mid(scrambled, ndx+1, 1)))
爐hird = Base64DecMap(asc(mid(scrambled, ndx+2, 1)))
爁ourth = Base64DecMap(asc(mid(scrambled, ndx+3, 1)))
爎et = ret & chr( ((first * 4) AND 255) + ((second \ 16) AND 3))
爎et = ret & chr( ((second * 16) AND 255) + ((third \ 4) AND 15))
爎et = ret & chr( ((third * 64) AND 255) + (fourth AND 63))
爊dx = ndx + 4
loop
if ndx < realLen then
爁irst = Base64DecMap(asc(mid(scrambled, ndx+0, 1)))
爏econd = Base64DecMap(asc(mid(scrambled, ndx+1, 1)))
爎et = ret & chr( ((first * 4) AND 255) + ((second \ 16) AND 3))
爄f realLen MOD 4 = 3 then
third = Base64DecMap(asc(mid(scrambled,ndx+2,1)))
ret = ret & chr( ((second * 16) AND 255) + ((third \ 4) AND 15))
爀nd if
end if
base64Decode = ret
燛ND FUNCTION
' 初始化
燾all initCodecs
' 测试代码
dim inp, encode
inp = "1234567890"
encode = base64Encode(inp)
response.write "加密前为:" & inp & newline
response.write "加密后为:" & encode & newline
response.write "解密后为:" & base64Decode(encode) & newline
%>


文章出自:爱时尚女性网www.aspjc.com,尊重版权是美德,转载请保留原地址,感谢合作!

 
 
链接交换请联系:QQ:790646582 首页链接要求百度快照在一周以内,不符合以上要求的各站,我们将定期把友情连接转入内页,谢谢合作。
Copyright @ 2012-2015 爱时尚女性网 保留所有权利吉ICP备14005127号-1 服务QQ:175529508 e-mail:zk8312@163.com
本站部分资源来自网友上传,如果无意之中侵犯了您的版权,请联系本站,本站将在3个工作日内删除。