ASP加密解密函数
当前位置:点晴教程→知识管理交流
→『 技术文档交流 』
<% [br] sbase_64_characters = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789+/" [br] sbase_64_characters = strunicode2ansi(sbase_64_characters) [br] [br] function strunicodelen(ascontents) [br] ''计算unicode字符串的ansi编码的长度 [br] ascontents1="a"&ascontents [br] len1=len(ascontents1) [br] k=0 [br] for i=1 to len1 [br] asc1=asc(mid(ascontents1,i,1)) [br] if asc1<0 then asc1=65536+asc1 [br] if asc1>255 then [br] k=k+2 [br] else [br] k=k+1 [br] end if [br] next [br] strunicodelen=k-1 [br] end function [br] [br] function strunicode2ansi(ascontents) [br] ''将unicode编码的字符串,转换成ansi编码的字符串 [br] strunicode2ansi="" [br] len1=len(ascontents) [br] for i=1 to len1 [br] varchar=mid(ascontents,i,1) [br] varasc=asc(varchar) [br] if varasc<0 then varasc=varasc+65536 [br] if varasc>255 then [br] varhex=hex(varasc) [br] varlow=left(varhex,2) [br] varhigh=right(varhex,2) [br] strunicode2ansi=strunicode2ansi & chrb("&h" & varlow ) & chrb("&h" & varhigh ) [br] else [br] strunicode2ansi=strunicode2ansi & chrb(varasc) [br] end if [br] next [br] end function [br] [br] function stransi2unicode(ascontents) [br] ''将ansi编码的字符串,转换成unicode编码的字符串 [br] stransi2unicode = "" [br] len1=lenb(ascontents) [br] if len1=0 then exit function [br] for i=1 to len1 [br] varchar=midb(ascontents,i,1) [br] varasc=ascb(varchar) [br] if varasc > 127 then [br] stransi2unicode = stransi2unicode & chr(ascw(midb(ascontents,i+1,1) & varchar)) [br] i=i+1 [br] else [br] stransi2unicode = stransi2unicode & chr(varasc) [br] end if [br] next [br] end function [br] [br] function base64encode(ascontents) [br] ''将ansi编码的字符串进行base64编码 [br] ''ascontents应当是ansi编码的字符串(二进制的字符串也可以) [br] dim lnposition [br] dim lsresult [br] dim char1 [br] dim char2 [br] dim char3 [br] dim char4 [br] dim byte1 [br] dim byte2 [br] dim byte3 [br] dim savebits1 [br] dim savebits2 [br] dim lsgroupbinary [br] dim lsgroup64 [br] dim m4,len1,len2 [br] [br] len1=lenb(ascontents) [br] if len1<1 then [br] base64encode="" [br] exit function [br] end if [br] [br] m3=len1 mod 3 [br] if m3 > 0 then ascontents = ascontents & string(3-m3, chrb(0)) [br] ''补足位数是为了便于计算 [br] [br] if m3 > 0 then [br] len1=len1+(3-m3) [br] len2=len1-3 [br] else [br] len2=len1 [br] end if [br] [br] lsresult = "" [br] [br] for lnposition = 1 to len2 step 3 [br] lsgroup64 = "" [br] lsgroupbinary = midb(ascontents, lnposition, 3) [br] [br] byte1 = ascb(midb(lsgroupbinary, 1, 1)): savebits1 = byte1 and 3 [br] byte2 = ascb(midb(lsgroupbinary, 2, 1)): savebits2 = byte2 and 15 [br] byte3 = ascb(midb(lsgroupbinary, 3, 1)) [br] [br] char1 = midb(sbase_64_characters, ((byte1 and 252) \ 4) + 1, 1) [br] char2 = midb(sbase_64_characters, (((byte2 and 240) \ 16) or (savebits1 * 16) and &hff) + 1, 1) [br] char3 = midb(sbase_64_characters, (((byte3 and 192) \ 64) or (savebits2 * 4) and &hff) + 1, 1) [br] char4 = midb(sbase_64_characters, (byte3 and 63) + 1, 1) [br] lsgroup64 = char1 & char2 & char3 & char4 [br] [br] lsresult = lsresult & lsgroup64 [br] next [br] [br] ''处理最后剩余的几个字符 [br] if m3 > 0 then [br] lsgroup64 = "" [br] lsgroupbinary = midb(ascontents, len2+1, 3) [br] [br] byte1 = ascb(midb(lsgroupbinary, 1, 1)): savebits1 = byte1 and 3 [br] byte2 = ascb(midb(lsgroupbinary, 2, 1)): savebits2 = byte2 and 15 [br] byte3 = ascb(midb(lsgroupbinary, 3, 1)) [br] [br] char1 = midb(sbase_64_characters, ((byte1 and 252) \ 4) + 1, 1) [br] char2 = midb(sbase_64_characters, (((byte2 and 240) \ 16) or (savebits1 * 16) and &hff) + 1, 1) [br] char3 = midb(sbase_64_characters, (((byte3 and 192) \ 64) or (savebits2 * 4) and &hff) + 1, 1) [br] [br] if m3=1 then [br] lsgroup64 = char1 & char2 & chrb(61) & chrb(61) ''用=号补足位数 [br] else [br] lsgroup64 = char1 & char2 & char3 & chrb(61) ''用=号补足位数 [br] end if [br] [br] lsresult = lsresult & lsgroup64 [br] end if [br] [br] base64encode = lsresult [br] [br] end function [br] [br] [br] function base64decode(ascontents) [br] ''将base64编码字符串转换成ansi编码的字符串 [br] ''ascontents应当也是ansi编码的字符串(二进制的字符串也可以) [br] dim lsresult [br] dim lnposition [br] dim lsgroup64, lsgroupbinary [br] dim char1, char2, char3, char4 [br] dim byte1, byte2, byte3 [br] dim m4,len1,len2 [br] [br] len1= lenb(ascontents) [br] m4 = len1 mod 4 [br] [br] if len1 < 1 or m4 > 0 then [br] ''字符串长度应当是4的倍数 [br] base64decode = "" [br] exit function [br] end if [br] [br] ''判断最后一位是不是 = 号 [br] ''判断倒数第二位是不是 = 号 [br] ''这里m4表示最后剩余的需要单独处理的字符个数 [br] if midb(ascontents, len1, 1) = chrb(61) then m4=3 [br] if midb(ascontents, len1-1, 1) = chrb(61) then m4=2 [br] [br] if m4 = 0 then [br] len2=len1 [br] else [br] len2=len1-4 [br] end if [br] [br] for lnposition = 1 to len2 step 4 [br] lsgroupbinary = "" [br] lsgroup64 = midb(ascontents, lnposition, 4) [br] char1 = instrb(sbase_64_characters, midb(lsgroup64, 1, 1)) - 1 [br] char2 = instrb(sbase_64_characters, midb(lsgroup64, 2, 1)) - 1 [br] char3 = instrb(sbase_64_characters, midb(lsgroup64, 3, 1)) - 1 [br] char4 = instrb(sbase_64_characters, midb(lsgroup64, 4, 1)) - 1 [br] byte1 = chrb(((char2 and 48) \ 16) or (char1 * 4) and &hff) [br] byte2 = lsgroupbinary & chrb(((char3 and 60) \ 4) or (char2 * 16) and &hff) [br] byte3 = chrb((((char3 and 3) * 64) and &hff) or (char4 and 63)) [br] lsgroupbinary = byte1 & byte2 & byte3 [br] [br] lsresult = lsresult & lsgroupbinary [br] next [br] [br] ''处理最后剩余的几个字符 [br] if m4 > 0 then [br] lsgroupbinary = "" [br] lsgroup64 = midb(ascontents, len2+1, m4) & chrb(65) ''chr(65)=a,转换成值为0 [br] if m4=2 then ''补足4位,是为了便于计算 [br] lsgroup64 = lsgroup64 & chrb(65) [br] end if [br] char1 = instrb(sbase_64_characters, midb(lsgroup64, 1, 1)) - 1 [br] char2 = instrb(sbase_64_characters, midb(lsgroup64, 2, 1)) - 1 [br] char3 = instrb(sbase_64_characters, midb(lsgroup64, 3, 1)) - 1 [br] char4 = instrb(sbase_64_characters, midb(lsgroup64, 4, 1)) - 1 [br] byte1 = chrb(((char2 and 48) \ 16) or (char1 * 4) and &hff) [br] byte2 = lsgroupbinary & chrb(((char3 and 60) \ 4) or (char2 * 16) and &hff) [br] byte3 = chrb((((char3 and 3) * 64) and &hff) or (char4 and 63)) [br] [br] if m4=2 then [br] lsgroupbinary = byte1 [br] elseif m4=3 then [br] lsgroupbinary = byte1 & byte2 [br] end if [br] [br] lsresult = lsresult & lsgroupbinary [br] end if [br] [br] base64decode = lsresult [br] [br] end function %>
该文章在 2010/5/8 15:45:47 编辑过 |
关键字查询
相关文章
正在查询... |