'功能说明:将StrIn进行base64编码,返回值为结果
Public Function Base64EncodeStr(ByVal StrIn As String) As String
Dim mAllByteIn() As Byte, mAllByteOut() As Byte
Dim mInByte(2) As Byte, mOutByte(3) As Byte
Dim myByte As Byte
Dim i As Integer, LineLen As Integer, j As Integer, m As Integer, n As Integer
n = 3: m = 0
mAllByteIn() = StrConv(StrIn, vbFromUnicode)
For i = 0 To UBound(mAllByteIn()) Step 3
j = 0
Do While j < 3
mInByte(j) = mAllByteIn(i + j)
j = j + 1
If i + j > UBound(mAllByteIn()) Then Exit Do
Loop
Base64EncodeByte mInByte, mOutByte, j
ReDim Preserve mAllByteOut(n)
For j = 0 To 3
mAllByteOut(n - 3 + j) = mOutByte(j)
Next j
n = n + 4
m = m + 4
If m > 70 Then
m = 0
n = n + 2
ReDim Preserve mAllByteOut(n)
mAllByteOut(n - 2) = &HD
mAllByteOut(n - 1) = &HA
End If
Next i
Base64EncodeStr = StrConv(mAllByteOut(), vbUnicode)
End Function
Private Sub Base64EncodeByte(mInByte() As Byte, mOutByte() As Byte, Num As Integer)
Dim tByte As Byte
Dim i As Integer
If Num = 1 Then
mInByte(1) = 0
mInByte(2) = 0
ElseIf Num = 2 Then
mInByte(2) = 0
End If
tByte = mInByte(0) And &HFC
mOutByte(0) = tByte / 4
tByte = ((mInByte(0) And &H3) * 16) + (mInByte(1) And &HF0) / 16
mOutByte(1) = tByte
tByte = ((mInByte(1) And &HF) * 4) + ((mInByte(2) And &HC0) / 64)
mOutByte(2) = tByte
tByte = (mInByte(2) And &H3F)
mOutByte(3) = tByte
For i = 0 To 3
If mOutByte(i) >= 0 And mOutByte(i) <= 25 Then
mOutByte(i) = mOutByte(i) + Asc("A")
ElseIf mOutByte(i) >= 26 And mOutByte(i) <= 51 Then
mOutByte(i) = mOutByte(i) - 26 + Asc("a")
ElseIf mOutByte(i) >= 52 And mOutByte(i) <= 61 Then
mOutByte(i) = mOutByte(i) - 52 + Asc("0")
ElseIf mOutByte(i) = 62 Then
mOutByte(i) = Asc("+")
Else
mOutByte(i) = Asc("/")
End If
Next i
If Num = 1 Then
mOutByte(2) = Asc("=")
mOutByte(3) = Asc("=")
ElseIf Num = 2 Then
mOutByte(3) = Asc("=")
End If
End Sub