base64是一种编码标准,也就是三个字节转换成四个字节之后发送,OUTLOOK,FOXMAIL接收到邮件之后再把BASE64解码

解决方案 »

  1.   

    base64是一种编码标准,也就是三个字节转换成四个字节之后发送,OUTLOOK,FOXMAIL接收到邮件之后再把BASE64解码
      

  2.   

    Option Explicit
    Option Base 0Private aDecTab(255) As Integer
    Private Const sEncTab As String = _
        "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"Public Function EncodeStr64(sInput As String) As String
    ' Return radix64 encoding of string of binary values
    ' Does not insert CRLFs. Just returns one long string,
    ' so it's up to the user to add line breaks or other formatting.
    ' Version 3: Use Mid$() function instead of appending
        Dim sOutput As String, sLast As String
        Dim b(2) As Byte
        Dim j As Integer
        Dim i As Long, nLen As Long, nQuants As Long
        Dim iIndex As Long
        
        nLen = Len(sInput)
        nQuants = nLen \ 3
        sOutput = String(nQuants * 4, " ")
        iIndex = 0
        ' Now start reading in 3 bytes at a time
        For i = 0 To nQuants - 1
            For j = 0 To 2
               b(j) = Asc(Mid(sInput, (i * 3) + j + 1, 1))
            Next
            Mid$(sOutput, iIndex + 1, 4) = EncodeQuantum(b)
            iIndex = iIndex + 4
        Next
        
        ' Cope with odd bytes
        Select Case nLen Mod 3
        Case 0
            sLast = ""
        Case 1
            b(0) = Asc(Mid(sInput, nLen, 1))
            b(1) = 0
            b(2) = 0
            sLast = EncodeQuantum(b)
            ' Replace last 2 with =
            sLast = Left(sLast, 2) & "=="
        Case 2
            b(0) = Asc(Mid(sInput, nLen - 1, 1))
            b(1) = Asc(Mid(sInput, nLen, 1))
            b(2) = 0
            sLast = EncodeQuantum(b)
            ' Replace last with =
            sLast = Left(sLast, 3) & "="
        End Select
        
        EncodeStr64 = sOutput & sLast
    End FunctionPublic Function DecodeStr64(sEncoded As String) As String
    ' Return string of decoded binary values given radix64 string
    ' Ignores any chars not in the 64-char subset
    ' Version 3: Use Mid$() function instead of appending
        Dim sDecoded As String
        Dim d(3) As Byte
        Dim c As Byte
        Dim di As Integer
        Dim i As Long
        Dim nLen As Long
        Dim iIndex As Long
        
        nLen = Len(sEncoded)
        sDecoded = String((nLen \ 4) * 3, " ")
        iIndex = 0
        di = 0
        Call MakeDecTab
        ' Read in each char in trun
        For i = 1 To Len(sEncoded)
            c = CByte(Asc(Mid(sEncoded, i, 1)))
            c = aDecTab(c)
            If c >= 0 Then
                d(di) = c
                di = di + 1
                If di = 4 Then
                    Mid$(sDecoded, iIndex + 1, 3) = DecodeQuantum(d)
                    iIndex = iIndex + 3
                    If d(3) = 64 Then
                        sDecoded = Left(sDecoded, Len(sDecoded) - 1)
                        iIndex = iIndex - 1
                    End If
                    If d(2) = 64 Then
                        sDecoded = Left(sDecoded, Len(sDecoded) - 1)
                        iIndex = iIndex - 1
                    End If
                    di = 0
                End If
            End If
        Next i
        
        DecodeStr64 = sDecoded
    End FunctionPrivate Function EncodeQuantum(b() As Byte) As String
        Dim sOutput As String
        Dim c As Integer
        
        sOutput = ""
        c = SHR2(b(0)) And &H3F
        sOutput = sOutput & Mid(sEncTab, c + 1, 1)
        c = SHL4(b(0) And &H3) Or (SHR4(b(1)) And &HF)
        sOutput = sOutput & Mid(sEncTab, c + 1, 1)
        c = SHL2(b(1) And &HF) Or (SHR6(b(2)) And &H3)
        sOutput = sOutput & Mid(sEncTab, c + 1, 1)
        c = b(2) And &H3F
        sOutput = sOutput & Mid(sEncTab, c + 1, 1)
        
        EncodeQuantum = sOutput
        
    End FunctionPrivate Function DecodeQuantum(d() As Byte) As String
        Dim sOutput As String
        Dim c As Long
        
        sOutput = ""
        c = SHL2(d(0)) Or (SHR4(d(1)) And &H3)
        sOutput = sOutput & Chr$(c)
        c = SHL4(d(1) And &HF) Or (SHR2(d(2)) And &HF)
        sOutput = sOutput & Chr$(c)
        c = SHL6(d(2) And &H3) Or d(3)
        sOutput = sOutput & Chr$(c)
        
        DecodeQuantum = sOutput
        
    End FunctionPrivate Function MakeDecTab()
    ' Set up Radix 64 decoding table
        Dim t As Integer
        Dim c As Integer    For c = 0 To 255
            aDecTab(c) = -1
        Next
      
        t = 0
        For c = Asc("A") To Asc("Z")
            aDecTab(c) = t
            t = t + 1
        Next
      
        For c = Asc("a") To Asc("z")
            aDecTab(c) = t
            t = t + 1
        Next
        
        For c = Asc("0") To Asc("9")
            aDecTab(c) = t
            t = t + 1
        Next
        
        c = Asc("+")
        aDecTab(c) = t
        t = t + 1
        
        c = Asc("/")
        aDecTab(c) = t
        t = t + 1
        
        c = Asc("=")    ' flag for the byte-deleting char
        aDecTab(c) = t  ' should be 64End Function' Version 3: ShiftLeft and ShiftRight functions improved.
    Public Function SHL2(ByVal bytValue As Byte) As Byte
    ' Shift 8-bit value to left by 2 bits
    ' i.e. VB equivalent of "bytValue << 2" in C
        SHL2 = (bytValue * &H4) And &HFF
    End FunctionPublic Function SHL4(ByVal bytValue As Byte) As Byte
    ' Shift 8-bit value to left by 4 bits
    ' i.e. VB equivalent of "bytValue << 4" in C
        SHL4 = (bytValue * &H10) And &HFF
    End FunctionPublic Function SHL6(ByVal bytValue As Byte) As Byte
    ' Shift 8-bit value to left by 6 bits
    ' i.e. VB equivalent of "bytValue << 6" in C
        SHL6 = (bytValue * &H40) And &HFF
    End FunctionPublic Function SHR2(ByVal bytValue As Byte) As Byte
    ' Shift 8-bit value to right by 2 bits
    ' i.e. VB equivalent of "bytValue >> 2" in C
        SHR2 = bytValue \ &H4
    End FunctionPublic Function SHR4(ByVal bytValue As Byte) As Byte
    ' Shift 8-bit value to right by 4 bits
    ' i.e. VB equivalent of "bytValue >> 4" in C
        SHR4 = bytValue \ &H10
    End FunctionPublic Function SHR6(ByVal bytValue As Byte) As Byte
    ' Shift 8-bit value to right by 6 bits
    ' i.e. VB equivalent of "bytValue >> 6" in C
        SHR6 = bytValue \ &H40
    End Function
      

  3.   

    可以去这里看一看,有完整的演示代码,绝对实用:
    http://www.aslike.net