Your test for equity will always fail if the length of the input string isn't divisible by 3. Reason being, the string is passed to the encoder ByRef and is padded with nulls so that it's length is divisible by 3. That means that the string that is actually encoded correctly, but when you look for equity later in the test code you aren't comparing against what was actually encoded. You can verify this by explicitly passing the strings to the encoding functions ByVal or passing it a copy of the string.
I would like to thank you for making me take a look at this again--there was an error that my testing didn't catch dealing with how the end padding key is placed when the encoded data ends with a CrLf. That just needed a little one-line fix.
Here's my test code, 10000 random length strings between 1 and 1000 characters.
VB Code:
Private Sub IntegrityBas()
Dim sOrig As String, sTest As String, sEnc As String, sDec As String, lRep As Long
Randomize
For lRep = 1 To 10000
sOrig = TestString(Int(1000 * Rnd + 1))
sTest = sOrig
sEnc = Encode64(sTest)
sDec = Decode64(sEnc)
If sDec <> sOrig Then
Debug.Print "Data integrity test failed!"
Exit Sub
End If
Next lRep
Debug.Print "Data integrity test passed!"
End Sub
Private Sub IntegrityCls()
Dim sOrig As String, sTest As String, sEnc As String, sDec As String, lRep As Long, oEnc As Base64
Randomize
Set oEnc = New Base64
For lRep = 1 To 10000
sOrig = TestString(Int(1000 * Rnd + 1))
sTest = sOrig
sEnc = oEnc.Encode(sTest)
sDec = oEnc.Decode(sEnc)
If sDec <> sOrig Then
Debug.Print "Data integrity test failed!"
Set oEnc = Nothing
Exit Sub
End If
Next lRep
Set oEnc = Nothing
Debug.Print "Data integrity test passed!"
End Sub
Private Function TestString(lLen As Long) As String
Dim bTemp() As Byte, lPos As Long
ReDim bTemp(lLen)
For lPos = LBound(bTemp) To UBound(bTemp)
bTemp(lPos) = Int((255 + 1) * Rnd)
Next lPos
TestString = StrConv(bTemp, vbUnicode)
End Function