原来的代码是这样的,但它对中文编码解码之后不对:
Const sBASE_64_CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
Function Base64encode(ByVal asContents)
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
If Len(asContents) Mod 3 > 0 then
asContents = asContents &
String(3 - (Len(asContents) Mod 3), " ")
lsResult = ""
For lnPosition = 1 To Len(asContents) Step 3
lsGroup64 = ""
lsGroupBinary = Mid(asContents, lnPosition, 3)
Byte1 = Asc(Mid(lsGroupBinary, 1, 1)): SaveBits1 = Byte1 And 3
Byte2 = Asc(Mid(lsGroupBinary, 2, 1)): SaveBits2 = Byte2 And 15
Byte3 = Asc(Mid(lsGroupBinary, 3, 1))
Char1 = Mid(sBASE_64_CHARACTERS, ((Byte1 And 252) / 4) + 1, 1)
Char2 = Mid(sBASE_64_CHARACTERS, (((Byte2 And 240) / 16) Or (SaveBits1 * 16) And &HFF) + 1, 1)
Char3 = Mid(sBASE_64_CHARACTERS, (((Byte3 And 192) / 64) Or (SaveBits2 * 4) And &HFF) + 1, 1)
Char4 = Mid(sBASE_64_CHARACTERS, (Byte3 And 63) + 1, 1)
lsGroup64 = Char1 &
Char2 &
Char3 &
Char4
lsResult = lsResult + lsGroup64
Next
Base64encode = lsResult
End Function
Function Base64Decode(base64String)
Const Base64CodeBase = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
Dim dataLength, Out, groupbegin
dataLength = Len(base64String)
Out = ""
If dataLength Mod 4 <> 0 then
Err.Raise 1, "Base64Decode", "Bad Base64 string."
Exit Function
End If
For groupbegin
= 1 To dataLength Step 4
Dim numDataBytes, CharCounter, thisChar, thisData, groupData
numDataBytes = 3
groupData = 0
For CharCounter = 0 To 3
thisChar = Mid(base64String, groupbegin
+ CharCounter, 1)
If thisChar = "=" then
numDataBytes = numDataBytes - 1
thisData = 0
else
thisData = InStr(Base64CodeBase, thisChar) - 1
End If
If thisData=-1 then
Err.Raise 2, "Base64Decode", "Bad character In Base64 string."
Exit Function
End If
groupData = 64 * groupData + thisData
Next
Dim OneChar
For CharCounter = 1 To numDataBytes
Select Case CharCounter
Case 1: OneChar = groupData / 65536
Case 2: OneChar = (groupData And 65535) / 256
Case 3: OneChar = (groupData And 255)
End Select
Out = Out &
Chr(OneChar)
Next
Next
Base64Decode = Out
End Function