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, 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


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值