比网上搜索来的短一些
Public Function Base64Encode(ByVal srcCode As String, Optional ByVal Base64Table As String = "")
Dim I As Integer, Result As String, Arr() As Byte
If Len(Base64Table) <> 64 Then
Base64Table = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
End If
Arr = StrConv(srcCode, vbFromUnicode)
For I = 0 To UBound(Arr)
Select Case I Mod 3
Case 0
Result = Result & Mid(Base64Table, Arr(I) \ 4 + 1, 1)
If I = UBound(Arr) Then
Result = Result & Mid(Base64Table, (Arr(I) And 3) * 16 + 1, 1)
End If
Case 1
Result = Result & Mid(Base64Table, (Arr(I - 1) And 3) * 16 + Arr(I) \ 16 + 1, 1)
If I = UBound(Arr) Then
Result = Result & Mid(Base64Table, (Arr(I) And 15) * 4 + 1, 1)
End If
Case 2
Result = Result & Mid(Base64Table, (Arr(I - 1) And 15) * 4 + Arr(I) \ 64 + 1, 1)
Result = Result & Mid(Base64Table, (Arr(I) And 63) + 1, 1)
End Select
Next
Base64Encode = Result
End Function
Public Function Base64Decode(ByVal srcCode As String, Optional ByVal Base64Table As String = "") As String
Dim I As Integer, C As Integer, Result() As Byte, Arr() As Byte
srcCode = Replace(srcCode, "=", "")
If Len(Base64Table) <> 64 Then
Base64Table = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
End If
For I = 1 To Len(srcCode)
If InStr(Base64Table, Mid(srcCode, I, 1)) = 0 Then Exit Function
Next
ReDim Result(Len(srcCode) * 3 \ 4 - 1)
For I = 0 To UBound(Result)
C = I * 4 \ 3 + 1
Result(I) = InStr(Base64Table, Mid(srcCode, C, 1)) - 1
Select Case I Mod 3
Case 0
Result(I) = Result(I) * 4
If C + 1 <= Len(srcCode) Then
Result(I) = Result(I) + (InStr(Base64Table, Mid(srcCode, C + 1, 1)) - 1) \ 16
End If
Case 1
Result(I) = (Result(I) And 15) * 16
If C + 1 <= Len(srcCode) Then
Result(I) = Result(I) + (InStr(Base64Table, Mid(srcCode, C + 1, 1)) - 1) \ 4
End If
Case 2
Result(I) = (Result(I) And 3) * 64 + InStr(Base64Table, Mid(srcCode, C + 1, 1)) - 1
End Select
Next
Base64Decode = StrConv(Result, vbUnicode)
End Function