<%<br /> OPTION EXPLICIT<br /> const BASE_64_MAP_INIT = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"<br /> dim newline<br /> dim Base64EncMap(63)<br /> dim Base64DecMap(127)<br /> '初始化函数<br /> PUBLIC SUB initCodecs()<br /> ' 初始化变量<br /> newline = "<P>" & chr(13) & chr(10)<br /> dim max, idx<br /> max = len(BASE_64_MAP_INIT)<br /> for idx = 0 to max - 1<br /> Base64EncMap(idx) = mid(BASE_64_MAP_INIT, idx + 1, 1)<br /> next<br /> for idx = 0 to max - 1<br /> Base64DecMap(ASC(Base64EncMap(idx))) = idx<br /> next<br /> END SUB<br /> 'Base64加密函数<br /> PUBLIC FUNCTION base64Encode(plain)<br /> if len(plain) = 0 then<br /> base64Encode = ""<br /> exit function<br /> end if<br /> dim ret, ndx, by3, first, second, third<br /> by3 = (len(plain) \ 3) * 3<br /> ndx = 1<br /> do while ndx <= by3<br /> first = asc(mid(plain, ndx+0, 1))<br /> second = asc(mid(plain, ndx+1, 1))<br /> third = asc(mid(plain, ndx+2, 1))<br /> ret = ret & Base64EncMap( (first \ 4) AND 63 )<br /> ret = ret & Base64EncMap( ((first * 16) AND 48) + ((second \ 16) AND 15 ) )<br /> ret = ret & Base64EncMap( ((second * 4) AND 60) + ((third \ 64) AND 3 ) )<br /> ret = ret & Base64EncMap( third AND 63)<br /> ndx = ndx + 3<br /> loop<br /> if by3 < len(plain) then<br /> first = asc(mid(plain, ndx+0, 1))<br /> ret = ret & Base64EncMap( (first \ 4) AND 63 )<br /> if (len(plain) MOD 3 ) = 2 then<br /> second = asc(mid(plain, ndx+1, 1))<br /> ret = ret & Base64EncMap( ((first * 16) AND 48) + ((second \ 16) AND 15 ) )<br /> ret = ret & Base64EncMap( ((second * 4) AND 60) )<br /> else<br /> ret = ret & Base64EncMap( (first * 16) AND 48)<br /> ret = ret '& "="<br /> end if<br /> ret = ret '& "="<br /> end if<br /> base64Encode = ret<br /> END FUNCTION<br /> 'Base64解密函数<br /> PUBLIC FUNCTION base64Decode(scrambled)<br /> if len(scrambled) = 0 then<br /> base64Decode = ""<br /> exit function<br /> end if<br /> dim realLen<br /> realLen = len(scrambled)<br /> do while mid(scrambled, realLen, 1) = "="<br /> realLen = realLen - 1<br /> loop<br /> dim ret, ndx, by4, first, second, third, fourth<br /> ret = ""<br /> by4 = (realLen \ 4) * 4<br /> ndx = 1<br /> do while ndx <= by4<br /> first = Base64DecMap(asc(mid(scrambled, ndx+0, 1)))<br /> second = Base64DecMap(asc(mid(scrambled, ndx+1, 1)))<br /> third = Base64DecMap(asc(mid(scrambled, ndx+2, 1)))<br /> fourth = Base64DecMap(asc(mid(scrambled, ndx+3, 1)))<br /> ret = ret & chr( ((first * 4) AND 255) + ((second \ 16) AND 3))<br /> ret = ret & chr( ((second * 16) AND 255) + ((third \ 4) AND 15))<br /> ret = ret & chr( ((third * 64) AND 255) + (fourth AND 63))<br /> ndx = ndx + 4<br /> loop<br /> if ndx < realLen then<br /> first = Base64DecMap(asc(mid(scrambled, ndx+0, 1)))<br /> second = Base64DecMap(asc(mid(scrambled, ndx+1, 1)))<br /> ret = ret & chr( ((first * 4) AND 255) + ((second \ 16) AND 3))<br /> if realLen MOD 4 = 3 then<br /> third = Base64DecMap(asc(mid(scrambled,ndx+2,1)))<br /> ret = ret & chr( ((second * 16) AND 255) + ((third \ 4) AND 15))<br /> end if<br /> end if<br /> base64Decode = ret<br /> END FUNCTION<br />' 初始化<br /> call initCodecs<br />' 测试代码<br /> dim inp, encode<br /> inp = "1234567890"<br /> encode = base64Encode(inp)<br /> response.write "加密前为:" & inp & newline<br /> response.write "加密后为:" & encode & newline<br /> response.write "解密后为:" & base64Decode(encode) & newline<br />%><br /> |
|