ASP加密解密函數(shù)
當(dāng)前位置:點(diǎn)晴教程→知識(shí)管理交流
→『 技術(shù)文檔交流 』
<% [br] sbase_64_characters = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789+/" [br] sbase_64_characters = strunicode2ansi(sbase_64_characters) [br] [br] function strunicodelen(ascontents) [br] ''計(jì)算unicode字符串的ansi編碼的長(zhǎng)度 [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編碼的字符串,轉(zhuǎn)換成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編碼的字符串,轉(zhuǎn)換成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編碼的字符串進(jìn)行base64編碼 [br] ''ascontents應(yīng)當(dāng)是ansi編碼的字符串(二進(jìn)制的字符串也可以) [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] ''補(bǔ)足位數(shù)是為了便于計(jì)算 [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] ''處理最后剩余的幾個(gè)字符 [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) ''用=號(hào)補(bǔ)足位數(shù) [br] else [br] lsgroup64 = char1 & char2 & char3 & chrb(61) ''用=號(hào)補(bǔ)足位數(shù) [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編碼字符串轉(zhuǎn)換成ansi編碼的字符串 [br] ''ascontents應(yīng)當(dāng)也是ansi編碼的字符串(二進(jìn)制的字符串也可以) [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] ''字符串長(zhǎng)度應(yīng)當(dāng)是4的倍數(shù) [br] base64decode = "" [br] exit function [br] end if [br] [br] ''判斷最后一位是不是 = 號(hào) [br] ''判斷倒數(shù)第二位是不是 = 號(hào) [br] ''這里m4表示最后剩余的需要單獨(dú)處理的字符個(gè)數(shù) [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] ''處理最后剩余的幾個(gè)字符 [br] if m4 > 0 then [br] lsgroupbinary = "" [br] lsgroup64 = midb(ascontents, len2+1, m4) & chrb(65) ''chr(65)=a,轉(zhuǎn)換成值為0 [br] if m4=2 then ''補(bǔ)足4位,是為了便于計(jì)算 [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 編輯過(guò) |
關(guān)鍵字查詢
相關(guān)文章
正在查詢... |