Base64算法函数VB6

前端之家收集整理的这篇文章主要介绍了Base64算法函数VB6前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

比网上搜索来的短一些

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,(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,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,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,1)) - 1) \ 4
                End If
            Case 2
                Result(I) = (Result(I) And 3) * 64 + InStr(Base64Table,1)) - 1
        End Select
    Next
    Base64Decode = StrConv(Result,vbUnicode)
End Function
原文链接:https://www.f2er.com/vb/257253.html

猜你在找的VB相关文章