昨天有人问如何用asp进行base64加密,这个就是


所属类别:Asp

文章作者:佚名

特别推荐:免费发布信息 承包关键词~~抢爆了!HOT!


<%OPTION EXPLICITconst BASE_64_MAP_INIT ="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"dim nl' zero based arraysdim Base64EncMap(63)dim Base64DecMap(127)' must be called before using anything elsePUBLIC SUB initCodecs()' init varsnl = "" & chr(13) & chr(10)' setup base 64dim max, idxmax = len(BASE_64_MAP_INIT)for idx = 0 to max - 1' one based stringBase64EncMap(idx) = mid(BASE_64_MAP_INIT, idx + 1, 1)nextfor idx = 0 to max - 1Base64DecMap(ASC(Base64EncMap(idx))) = idxnextEND SUB' encode base 64 encoded stringPUBLIC 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 + 3loop' check for stragglersif 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' decode base 64 encoded stringPUBLIC FUNCTION base64Decode(scrambled)if len(scrambled) = 0 thenbase64Decode = ""exit functionend if' ignore paddingdim 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) AND15) )ret = ret & chr( ((third * 64) AND 255) +(fourth AND 63) )ndx = ndx + 4loop' check for stragglers, will be 2 or 3 charactersif 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) AND15) )end ifend ifbase64Decode = retEND FUNCTION' initializecall 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%> 关闭本页

相关信息

· 一分钟的人生困境

· 一样的Ping不一样的使用

· 自定义文档库之---自定义文档库右键菜单

· 嵌入式Linux开发及移植的学习建议








....

4074 45020