核心代码:
<% ' OPTION EXPLICITconst BASE_64_MAP_INIT = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"dim newlinedim Base64EncMap(63)dim Base64DecMap(127)'初始化函数PUBLIC SUB initCodecs()' 初始化变量newline = "<P>" & chr(13) & chr(10)dim max, idxmax = len(BASE_64_MAP_INIT)for idx = 0 to max - 1Base64EncMap(idx) = mid(BASE_64_MAP_INIT, idx + 1, 1)nextfor idx = 0 to max - 1Base64DecMap(ASC(Base64EncMap(idx))) = idxnextEND SUB'Base64加密函数PUBLIC FUNCTION base64Encode(plain)if len(plain) = 0 thenbase64Encode = ""exit functionend ifdim ret, ndx, by3, first, second, thirdby3 = (len(plain) / 3) * 3ndx = 1do while ndx <= by3first = 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 + 3loopif by3 < len(plain) thenfirst = asc(mid(plain, ndx+0, 1))ret = ret & Base64EncMap( (first / 4) AND 63 )if (len(plain) MOD 3 ) = 2 thensecond = asc(mid(plain, ndx+1, 1))ret = ret & Base64EncMap( ((first * 16) AND 48) + ((second / 16) AND 15 ) )ret = ret & Base64EncMap( ((second * 4) AND 60) )elseret = ret & Base64EncMap( (first * 16) AND 48)ret = ret '& "="end ifret = ret '& "="end ifbase64Encode = retEND FUNCTION'Base64解密函数PUBLIC FUNCTION base64Decode(scrambled)if len(scrambled) = 0 thenbase64Decode = ""exit functionend ifdim realLenrealLen = len(scrambled)do while mid(scrambled, realLen, 1) = "="realLen = realLen - 1loopdim ret, ndx, by4, first, second, third, fourthret = ""by4 = (realLen / 4) * 4ndx = 1do while ndx <= by4first = 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 + 4loopif ndx < realLen thenfirst = 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 thenthird = Base64DecMap(asc(mid(scrambled,ndx+2,1)))ret = ret & chr( ((second * 16) AND 255) + ((third / 4) AND 15))end ifend ifbase64Decode = retEND FUNCTION%>
使用方法:
' 初始化 call initCodecsResponse.Write(base64Encode("脚本之家之我要加密的字符串"))Response.Write(base64Decode("bWFyczIwMTAwMjIw0"))
新闻热点
疑难解答