base64編碼、解碼函數(vbs)

發布時間:2007年03月07日      浏覽次數:2886 次
base64編碼、解碼函數
--------------------------------------------------------------------------------
great 于 2/1/2002 9:31:08 AM 加貼在 Joy ASP ←返回版面:
這是我(wǒ)(wǒ)看完幾個base64編碼、解碼函數後自己改寫的。
因爲,在中(zhōng)文操作系統的VBscript中(zhōng),使用的是unicode字符集,所以
很多base64編碼、解碼函數在理論上是正确的,但實際不能運行!
我(wǒ)(wǒ)加寫了幾個Unicode與Ansi編碼轉換的函數,現貼出來,請大(dà)家執教!
文件名稱base64test.asp
<%
sBASE_64_CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
sBASE_64_CHARACTERS = strUnicode2Ansi(sBASE_64_CHARACTERS)
Function strUnicodeLen(asContents)
'計算unicode字符串的Ansi編碼的長度
asContents1="a"&asContents
len1=len(asContents1)
k=0
for i=1 to len1
asc1=asc(mid(asContents1,i,1))
if asc1<0 then asc1=65536+asc1
if asc1>255 then
 k=k+2
else
 k=k+1
end if
next
strUnicodeLen=k-1
End Function
Function strUnicode2Ansi(asContents)
'将Unicode編碼的字符串,轉換成Ansi編碼的字符串
strUnicode2Ansi=""
len1=len(asContents)
for i=1 to len1
varchar=mid(asContents,i,1)
varasc=asc(varchar)
if varasc<0 then varasc=varasc+65536
if varasc>255 then
 varHex=Hex(varasc)
 varlow=left(varHex,2)
 varhigh=right(varHex,2)
 strUnicode2Ansi=strUnicode2Ansi & chrb("&H" & varlow ) & chrb("&H" & varhigh )
else
 strUnicode2Ansi=strUnicode2Ansi & chrb(varasc)
end if
 next
End function
Function strAnsi2Unicode(asContents)
'将Ansi編碼的字符串,轉換成Unicode編碼的字符串
strAnsi2Unicode = ""
len1=lenb(asContents)
if len1=0 then exit function
for i=1 to len1
varchar=midb(asContents,i,1)
varasc=ascb(varchar)
if varasc > 127then
 strAnsi2Unicode = strAnsi2Unicode & chr(ascw(midb(asContents,i+1,1) & varchar))
 i=i+1
else
 strAnsi2Unicode = strAnsi2Unicode & chr(varasc)
end if
next
End function
Function Base64encode(asContents)
'将Ansi編碼的字符串進行Base64編碼
'asContents應當是ANSI編碼的字符串(二進制的字符串也可以)
Dim lnPosition
Dim lsResult
Dim Char1
Dim Char2
Dim Char3
Dim Char4
Dim Byte1
Dim Byte2
Dim Byte3
Dim SaveBits1
Dim SaveBits2
Dim lsGroupBinary
Dim lsGroup64
Dim m4,len1,len2
len1=Lenb(asContents)
if len1<1 then
 Base64encode=""
 exit Function
end if
m3=Len1 Mod 3
If M3 > 0 Then asContents = asContents & String(3-M3, chrb(0))
'補足位數是爲了便于計算
IF m3 > 0 THEN
 len1=len1+(3-m3)
 len2=len1-3
else
 len2=len1
end if
lsResult = ""
For lnPosition = 1 To len2 Step 3
lsGroup64 = ""
lsGroupBinary = Midb(asContents, lnPosition, 3)
Byte1 = Ascb(Midb(lsGroupBinary, 1, 1)): SaveBits1 = Byte1 And 3
Byte2 = Ascb(Midb(lsGroupBinary, 2, 1)): SaveBits2 = Byte2 And 15
Byte3 = Ascb(Midb(lsGroupBinary, 3, 1))
Char1 = Midb(sBASE_64_CHARACTERS, ((Byte1 And 252) \ 4) + 1, 1)
Char2 = Midb(sBASE_64_CHARACTERS, (((Byte2 And 240) \ 16) Or (SaveBits1 * 16) And &HFF) + 1, 1)
Char3 = Midb(sBASE_64_CHARACTERS, (((Byte3 And 192) \ 64) Or (SaveBits2 * 4) And &HFF) + 1, 1)
Char4 = Midb(sBASE_64_CHARACTERS, (Byte3 And 63) + 1, 1)
lsGroup64 = Char1 & Char2 & Char3 & Char4

lsResult = lsResult & lsGroup64
Next
'處理最後剩餘的幾個字符
if M3 > 0then
lsGroup64 = ""
lsGroupBinary = Midb(asContents, len2+1, 3)
Byte1 = Ascb(Midb(lsGroupBinary, 1, 1)): SaveBits1 = Byte1 And 3
Byte2 = Ascb(Midb(lsGroupBinary, 2, 1)): SaveBits2 = Byte2 And 15
Byte3 = Ascb(Midb(lsGroupBinary, 3, 1))
Char1 = Midb(sBASE_64_CHARACTERS, ((Byte1 And 252) \ 4) + 1, 1)
Char2 = Midb(sBASE_64_CHARACTERS, (((Byte2 And 240) \ 16) Or (SaveBits1 * 16) And &HFF) + 1, 1)
Char3 = Midb(sBASE_64_CHARACTERS, (((Byte3 And 192) \ 64) Or (SaveBits2 * 4) And &HFF) + 1, 1)
if M3=1 then
 lsGroup64 = Char1 & Char2 & ChrB(61) & ChrB(61) '用=号補足位數
else
 lsGroup64 = Char1 & Char2 & Char3 & ChrB(61)'用=号補足位數
end if

lsResult = lsResult & lsGroup64
end if
Base64encode = lsResult
End Function
Function Base64decode(asContents)
'将Base64編碼字符串轉換成Ansi編碼的字符串
'asContents應當也是ANSI編碼的字符串(二進制的字符串也可以)
Dim lsResult
Dim lnPosition
Dim lsGroup64, lsGroupBinary
Dim Char1, Char2, Char3, Char4
Dim Byte1, Byte2, Byte3
Dim M4,len1,len2
len1= Lenb(asContents)
M4 = len1 Mod 4
if len1 < 1 or M4 > 0 then
 '字符串長度應當是4的倍數
 Base64decode = ""
 exit Function
end if

'判斷最後一(yī)位是不是 = 号
'判斷倒數第二位是不是 = 号
'這裏m4表示最後剩餘的需要單獨處理的字符個數
if midb(asContents, len1, 1) = chrb(61) then m4=3
if midb(asContents, len1-1, 1) = chrb(61) then m4=2
if m4 = 0 then
 len2=len1
else
 len2=len1-4
end if
For lnPosition = 1 To Len2 Step 4
lsGroupBinary = ""
lsGroup64 = Midb(asContents, lnPosition, 4)
Char1 = InStrb(sBASE_64_CHARACTERS, Midb(lsGroup64, 1, 1)) - 1
Char2 = InStrb(sBASE_64_CHARACTERS, Midb(lsGroup64, 2, 1)) - 1
Char3 = InStrb(sBASE_64_CHARACTERS, Midb(lsGroup64, 3, 1)) - 1
Char4 = InStrb(sBASE_64_CHARACTERS, Midb(lsGroup64, 4, 1)) - 1
Byte1 = Chrb(((Char2 And 48) \ 16) Or (Char1 * 4) And &HFF)
Byte2 = lsGroupBinary & Chrb(((Char3 And 60) \ 4) Or (Char2 * 16) And &HFF)
Byte3 = Chrb((((Char3 And 3) * 64) And &HFF) Or (Char4 And 63))
lsGroupBinary = Byte1 & Byte2 & Byte3

lsResult = lsResult & lsGroupBinary
Next
'處理最後剩餘的幾個字符
if M4 > 0 then
lsGroupBinary = ""
lsGroup64 = Midb(asContents, len2+1, m4) & chrB(65) 'chr(65)=A,轉換成值爲0
if M4=2 then'補足4位,是爲了便于計算
lsGroup64 = lsGroup64 & chrB(65)
end if
Char1 = InStrb(sBASE_64_CHARACTERS, Midb(lsGroup64, 1, 1)) - 1
Char2 = InStrb(sBASE_64_CHARACTERS, Midb(lsGroup64, 2, 1)) - 1
Char3 = InStrb(sBASE_64_CHARACTERS, Midb(lsGroup64, 3, 1)) - 1
Char4 = InStrb(sBASE_64_CHARACTERS, Midb(lsGroup64, 4, 1)) - 1
Byte1 = Chrb(((Char2 And 48) \ 16) Or (Char1 * 4) And &HFF)
Byte2 = lsGroupBinary & Chrb(((Char3 And 60) \ 4) Or (Char2 * 16) And &HFF)
Byte3 = Chrb((((Char3 And 3) * 64) And &HFF) Or (Char4 And 63))

if M4=2 then
 lsGroupBinary = Byte1
elseif M4=3 then
 lsGroupBinary = Byte1 & Byte2
end if

lsResult = lsResult & lsGroupBinary
end if
Base64decode = lsResult
End Function
'---------------------------------------------------------------------------------
response.write strUnicode2Ansi("abc我(wǒ)(wǒ)們d我(wǒ)(wǒ)們") & "<hr>"
response.write strAnsi2Unicode(Base64encode(strUnicode2Ansi("abc我(wǒ)(wǒ)們d我(wǒ)(wǒ)們"))) & "<hr>"
response.write strAnsi2Unicode(Base64encode(strUnicode2Ansi("abc我(wǒ)(wǒ)們d我(wǒ)(wǒ)們a"))) & "<hr>"
response.write strAnsi2Unicode(Base64encode(strUnicode2Ansi("abc我(wǒ)(wǒ)們d我(wǒ)(wǒ)們ab"))) & "<hr>"
response.write strAnsi2Unicode(Base64encode(strUnicode2Ansi("abc我(wǒ)(wǒ)們d我(wǒ)(wǒ)們abc"))) & "<hr>"
response.write strAnsi2Unicode(Base64decode(Base64encode(strUnicode2Ansi("abc我(wǒ)(wǒ)們d我(wǒ)(wǒ)們")))) & "<hr>"
response.write strAnsi2Unicode(Base64decode(Base64encode(strUnicode2Ansi("abc我(wǒ)(wǒ)們d我(wǒ)(wǒ)們a")))) & "<hr>"
response.write strAnsi2Unicode(Base64decode(Base64encode(strUnicode2Ansi("abc我(wǒ)(wǒ)們d我(wǒ)(wǒ)們ab")))) & "<hr>"
response.write strAnsi2Unicode(Base64decode(Base64encode(strUnicode2Ansi("abc我(wǒ)(wǒ)們d我(wǒ)(wǒ)們abc")))) & "<hr>"
sdecode="Sm1haWy1xNb30qqyzsr9wdCx7Q0K1/fV36O6dG9ueTEzMiAgMjAwMC0wNS0yNNXixqq45bz+sbvU"
sdecode=sdecode & "xLbBPT0xNTK0zg0KIA0KDQqhoaGh1/fV3yBb0ru/y10gDQqhoaGhIA0KoaGhoQ0KoaGhocu1w/ej"
sdecode=sdecode & "urj8tuC1xNDFz6LH67LOv7xKbWFpbMu1w/ejrM7Sz+vV4tLRvq3X47m708O1xMHLoaMNCqGhoaEN"
sdecode=sdecode & "CqGhoaHT9rW9zsrM4sfrsrvSqs7KztKjrNLyzqq3/s7xxvfUrdLyo6zO0r32vfbKudPDQ0RPTlRT"
sdecode=sdecode & "t6LQxaOsU29ycnkuLi4NCqGhoaENCqGhoaGjqDGjqUJvZHmjqNDFvP7V/c7Eo6kgOiDX1rf7tK4N"
sdecode=sdecode & "CqGhoaENCqGhoaHI56O6Sk1haWwuQm9keSA9ICLV4sDvv8nS1MrH08O7p8zu0LS1xLHttaXE2sjd"
sdecode=sdecode & "o6y/ydLUyKHX1EZyb22hoyINCqGhoaENCqGhoaENCqGhoaGjqDKjqUNoYXJzZXSjqNfWt/u8r6Os"
sdecode=sdecode & "yLHKoc6qIlVTLUFTQ0lJIqOpIDog19a3+7SuDQqhoaGhDQqhoaGhyOejukpNYWlsLkNoYXJzZXQg"
sdecode=sdecode & "PSAiVVMtQVNDSUkiIA0KoaGhoQ0KoaGhoaOoM6OpQ29udGVudFRyYW5zZmVyRW5jb2RpbmcgOiDX"
sdecode=sdecode & "1rf7tK4NCqGhoaENCqGhoaHWuLaoxNrI3bSry83KsbXEseDC67e9yr2jrMixyqHKxyJRdW90ZWQt"
sdecode=sdecode & "UHJpbnRhYmxlIiANCqGhoaHI56O6Sk1haWwuQ29udGVudFRyYW5zZmVyRW5jb2RpbmcgPSAiYmFz"
sdecode=sdecode & "ZTY0Ig0KoaGhoQ0KoaGhoaOoNKOpQ29udGVudFR5cGWjqNDFvP61xGNvbnRlbnR5cGUuIMixyqHK"
sdecode=sdecode & "xyJ0ZXh0L3BsYWluIqOpIDog19a3+7SuDQqhoaGhDQqhoaGhyOe5+8Tj0tRIVE1MuPHKvbeiy83T"
sdecode=sdecode & "yrz+LCC4xM6qInRleHQvaHRtbCK8tL/JoaMgDQqhoaGhyOejukpNYWlsLkNvbnRlbnRUeXBlID0g"
sdecode=sdecode & "InRleHQvaHRtbCINCqGhoaENCqGhoaENCqGhoaGjqDWjqUVuY29kaW5nIDog19a3+7SuDQqhoaGh"
sdecode=sdecode & "DQqhoaGhyejWw7i9vP6x4MLrt73KvaOoyLHKocrHImJhc2U2NKOpoaMgv8nS1NGh1PHKudPDtcTK"
sdecode=sdecode & "xyJiYXNlNjQiLCAidXVlbmNvZGUiIG9yICJxdW90ZWQtcHJpbnRhYmxlIiANCqGhoaHI56O6Sk1h"
sdecode=sdecode & "aWwuRW5jb2RpbmcgPSAiYmFzZTY0Ig0KoaGhoQ0KoaGhoQ0KoaGhoaOoNqOpTG9no6hKbWFpbLS0"
sdecode=sdecode & "vai1xMjV1r6jrMewzOFsb2dpbmfK9NDUyejWw86qdHJ1ZaOsvPvPwsPmo6kgOiDX1rf7tK4NCqGh"
sdecode=sdecode & "oaENCqGhoaHI56O6yrnTw1Jlc3BvbnNlLldyaXRlKCBKTWFpbC5Mb2cgKdPvvuTB0LP2yNXWvtDF"
sdecode=sdecode & "z6Khow0KoaGhoQ0KoaGhoQ0KoaGhoaOoN6OpTG9nZ2luZ6Ooyse38cq508PI1da+o6kgOiCyvLb7"
sdecode=sdecode & "0M0NCqGhoaENCqGhoaHI56O6Sk1haWwuTG9nZ2luZyA9IHRydWUNCqGhoaENCqGhoaENCqGhoaGj"
sdecode=sdecode & "qDijqVJlY2lwaWVudHMgOiDX1rf7tK4NCqGhoaENCqGhoaHWu7bByvTQ1KOst7W72Mv509DK1bz+"
sdecode=sdecode & "yMsNCqGhoaHI56O6UmVzcG9uc2UuV3JpdGUoICIiICsgSk1haWwuUmVjaXBpZW50cyArICIiICk7"
sdecode=sdecode & "DQqhoaGhDQqhoaGhDQqhoaGho6g5o6lSZXBseVRvo6jWuLaosfC1xLvY0MW12Na3o6kgOiDX1rf7"
sdecode=sdecode & "tK4NCqGhoaENCqGhoaHI56O6Sk1haWwuUmVwbHlUbyA9ICJhbnlvdGhlckBtYWlsaG9zdC5jb20i"
sdecode=sdecode & "DQqhoaGhDQqhoaGhDQqhoaGho6gxMKOpU2VuZGVyo6ggt6K8/sjLtcTTyrz+tdjWt6OpIDog19a3"
sdecode=sdecode & "+7SuDQqhoaGhDQqhoaGhyOejukpNYWlsLlNlbmRlciA9ICJzZW5kZXJAbWFpbGhvc3QuY29tIg0K"
sdecode=sdecode & "oaGhoQ0KoaGhoQ0KoaGhoaOoMTGjqVNlbmRlck5hbWWjqLeivP7Iy7XE0NXD+6OpIDog19a3+7Su"
sdecode=sdecode & "DQqhoaGhDQqhoaGhyOejukpNYWlsLlNlbmRlck5hbWUgPSAi0ru/yyINCqGhoaENCqGhoaENCqGh"
sdecode=sdecode & "oaGjqDEyo6lTZXJ2ZXJBZGRyZXNzo6jTyrz+t/7O8cb3tcS12Na3o6kgOiDX1rf7tK4NCqGhoaEN"
response.write replace(strAnsi2Unicode(Base64decode(strUnicode2Ansi(sdecode))),chr(13)&chr(10),"<br>") & "<hr>"
response.end
%>
免責聲明:本站相關技術文章信息部分(fēn)來自網絡,目的主要是傳播更多信息,如果您認爲本站的某些信息侵犯了您的版權,請與我(wǒ)(wǒ)們聯系,我(wǒ)(wǒ)們會即時妥善的處理,謝謝合作!