DESPERATE! Need B64 Enc/Dec Routines.

I am in DESPERATE need of encoding and decoding B64 strings.

Please help me as soon as possible!
mikieacAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
mcriderConnect With a Mentor Commented:
Here you go!  Stick this code in a module and you can call Base64Decode and Base64Encode.

Cheers!



THE CODE:

Public Function Base64Decode(Source As String) As String
    Dim iVal As Long
    Dim jVal As Long
    Dim kVal As Long
    Dim B64 As Variant
    Dim sIndex As Long
    Dim BinString As String
    Dim Bin As String
    Dim Bch As Integer
    Dim Bstring As String
    Dim Bout As String
   
    sIndex = 0
    B64 = Array("000000", "000001", "000010", "000011", "000100", "000101", "000110", "000111", _
                "001000", "001001", "001010", "001011", "001100", "001101", "001110", "001111", _
                "010000", "010001", "010010", "010011", "010100", "010101", "010110", "010111", _
                "011000", "011001", "011010", "011011", "011100", "011101", "011110", "011111", _
                "100000", "100001", "100010", "100011", "100100", "100101", "100110", "100111", _
                "101000", "101001", "101010", "101011", "101100", "101101", "101110", "101111", _
                "110000", "110001", "110010", "110011", "110100", "110101", "110110", "110111", _
                "111000", "111001", "111010", "111011", "111100", "111101", "111110", "111111")
    For iVal = 1 To Len(Source) - 1
        Bch = Asc(Mid$(Source, iVal, 1))
        Select Case Bch
            Case 65 To 90:  BinString = BinString + B64(Bch - 65)
            Case 97 To 122: BinString = BinString + B64(Bch - 71)
            Case 48 To 57:  BinString = BinString + B64(Bch + 4)
            Case 43:        BinString = BinString + B64(62)
            Case 47:        BinString = BinString + B64(63)
        End Select
    Next iVal
    Bch = Asc(Right$(Source, 1))
    Select Case Bch
        Case 65 To 90:  Bstring = B64(Bch - 65)
        Case 97 To 122: Bstring = B64(Bch - 71)
        Case 48 To 57:  Bstring = B64(Bch + 4)
        Case 43:        Bstring = B64(62)
        Case 47:        Bstring = B64(63)
    End Select
   
    jVal = 8 - (Len(BinString) Mod 8)
    BinString = BinString + Right$(Bstring, jVal)
    kVal = Len(BinString)
    For jVal = 1 To kVal Step 8
        Bout = Bout + Chr$(BinDec(Mid(BinString, jVal, 8)))
    Next jVal
    Base64Decode = Bout
End Function
Public Function Base64Encode(Source As String) As String
    Dim iVal As Long, jVal As Long, kVal As Long, DecVal As Byte
    Dim BinString As String, Temp As String
    Dim B64 As Variant
   
    B64 = Array("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", _
                "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", _
                "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", _
                "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", _
                "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "+", "/")
    For iVal = 1 To Len(Source)
        DecVal = Asc(Mid(Source, iVal, 1))
        For jVal = 7 To 0 Step -1
            If DecVal And (2 ^ jVal) Then
                BinString = BinString & "1"
            Else
                BinString = BinString & "0"
            End If
        Next jVal
    Next iVal
    kVal = Len(BinString)
    For jVal = 1 To kVal Step 6
        Temp = Temp & B64(BinDec(Mid(BinString, jVal, 6)))
    Next jVal
Base64Encode = Temp
End Function

0
 
mikieacAuthor Commented:
THANK-YOU THANK-YOU THANK-YOU!
You are a lifesaver!
0
 
mcriderCommented:
You're welcome!

Thanks for the points!
0
 
mikieacAuthor Commented:
Your code works great! I LOVE YOU!!
0
 
vettrangerCommented:
bought this question ...
0
All Courses

From novice to tech pro — start learning today.